Interchange contents of variables? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
How would I interchange the contents of variables A and B?
temp = A
B = A
A = temp
The following code doesn't work. I'm not sure how to fix it. I must not understand python syntax or variables correctly.

Python provides particularly elegant syntax for swapping variables:
A, B = B, A

Erm...
temp = A
A = B
B = temp

Related

Use Sum() function with lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is the correct syntax for using Sum() function in python for lists?
Say we have a list called list_1. When I tried these two syntax I get the same result:
total_1 = Sum(list_1)
total_2 = list_1.Sum()
What is the best syntax for that?
To sum a list of integers or floats, the correct method is:
list_1 = [1, 2, 3]
total = sum(list_1)
print(total)
which results in the output:
6

How to pass Lists as arguments using for loop in a function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def conversion(putlist):
if(len(putlist)>len(set(putlist))):
putlist = set(putlist)
putlist = list(putlist)
print("{}have duplicates".format(putlist))
else:
putlist = set(putlist)
putlist = list(putlist)
print(putlist)
for i in range[brent, camden, redbridge, southwark]:
conversion(i)
error
1
Neglect the indentation
I want to pass 4 lists as arguments to function conversion() with the help of for loop so I ain't have to pass the arguments 4 times.
Replace:
for i in range[brent, camden, redbridge, southwark]: <-------getting error
with
for i in [brent, camden, redbridge, southwark]:
https://www.w3schools.com/python/ref_func_range.asp
range - generator, this function makes a list for you
You are not needed in this function if you already have a list
wiki page about generator: https://wiki.python.org/moin/Generators
for i in [brent, camden, redbridge, southwark]:
conversion(*i)

how to unpack a tuple in this form ('abc', [20, 20])? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want this to be unpacked in two variable for examaple.
something like..
a,*b = ('abc', [20,20])
i want a to contain 'abc' and b to hold the list [20,20].
Simply:
a,b = 'abc', [20,20]
If you removed the asterix, your code would have worked
a, b = ('abc', [20,20])
It is that simple. Afterwards:
a is 'abc'
b is [20,20]

Sort each string inside list - Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to sort each string inside a list. For example, if I have the list ['bca', 'fed'] then I want my function to return ['abc', 'def'].
Currently trying to do this like so:
lines = [''.join.(sorted(e)) for e in lines]
What's wrong with this syntax?
Just remove the redundant dot (.):
lines = [''.join(sorted(e)) for e in lines]

Python in proccess of geting to pig game [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Issue occurred right after I added the and x= "J1234": I don't know what's going on.
original = ""
if len(original) > 0 and x = "J123":
raw_input("Enter a word:")
original = raw_input('word')
print "original"
else:
print "empty"
x.isalpha()
So, you're getting a SyntaxError. To fix this, change:
x = "J123":
to...
x == "J123":
In Python the former is for setting values and the latter is for checking them. Therefore when you use this the wrong way round the syntax is incorrect.

Categories

Resources