Assignment values in python [duplicate] - python

This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Swap 2 values of 2 variables without using a third variable; python
(5 answers)
Is there a standardized method to swap two variables in Python?
(8 answers)
Closed 5 years ago.
Why does
a = 3
b = 5
a,b = a + b,a
print a,b
-------
8 3
differ from
a = 3
b = 5
a = a + b
b = a
print a,b
------
8 8
Can someone explain how Python interprets the assignments differently? Does Python still keep the old value of a in the first paragraph of code, when a is assigned to a + b? (a= a+b)
Also, does these assignment properties change from Python 2 to Python 3?

Related

how to use Ternary operator in python. It works different for me [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 19 days ago.
can anyone tell me why this is so? In the below code you can see the output is different for the same lines of codes with different variables name. I am not able to understand the idea behind the ternary operator in python.
a = "0"
b = "1"
c = a=="0" and 1 or 2
print(c)
output = 1
category_name = "1"
outfsheetno = category_name=="1" and 0 or 1
print(outfsheetno)
output = 1

5*2=55 not 10! Why? [duplicate]

This question already has answers here:
multiplication of two arguments in python [duplicate]
(6 answers)
Getting issue while trying to multiply string and integer [duplicate]
(2 answers)
Closed 3 months ago.
I want to output 5 * 2 = 10 but python output is 55!
How do I resolve this problem?
a = 0
b = 2
a = input("a? :") #(get 5 as input)
c = a * b
print (c)
This is my code.
when I input a number it repeat same number I entered two times insterd of showing multipiy it. What do I have to do to solve this?
a is a string,
so it will be
'5'*2='55'
if you want 10, you need to cast a to int.
a=int(input())
here is the link to document
https://docs.python.org/3/library/functions.html#input

Formating python string with constants? [duplicate]

This question already has answers here:
Using variables in the format() function in Python
(3 answers)
Python string formatting on the fly
(2 answers)
Closed 4 months ago.
I'm using python formating to print some left aligned columns.
This nicely left alignes a, b and c:
out_string = '{:30} {:20} {:10}'.format(a, b, c)
Then I tried using constants, but failed with ValueError: Invalid format specifier:
ALIGNa = 30
ALIGNb = 20
ALIGNc = 10
out_string = '{:ALIGNa} {:ALIGNb} {:ALIGNc}'.format(a, b, c)
Why does it fail?

Why different reference is created for int object when passed to function in python? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Python : When is a variable passed by reference and when by value? [duplicate]
(6 answers)
Closed 3 years ago.
As per my knowledge the Python interpreter creates references from 0 to 256 for int objects, at the start itself. Here I got output 3,[44,2,3], for the list I understood, but why did the same thing not happen with the int object? I expected the output to be 1,[44,2,3]. Can anyone please explain this behavior to me? I use Python 3.
def change(var, lst):
var = 1
lst[0] = 44
k = 3
a = [1, 2, 3]
change(k, a)
print(k)
print(a)
Output:
3
[44,2,3]
Expected output:
1
[44,2,3]

How probing works in python dictionary? [duplicate]

This question already has answers here:
How Python dict stores key, value when collision occurs? [duplicate]
(2 answers)
How are Python's Built In Dictionaries Implemented?
(3 answers)
Why can a Python dict have multiple keys with the same hash?
(5 answers)
Closed 4 years ago.
my_dict = {}
my_dict["qwerty"] = "some_value"
my_dict[114378642] = "some_other_value"
The above code contains a python dictionary containing two keys, where the first key is of type string and the second key is of type integer. Though both keys are of different types it produces the same hash i.e,
hash("qwerty") = 114378642
hash(114378642) = 114378642
and hence,
hash("qwerty") == hash(114378642) #True
Couldn't get a proper answer until now,
Firstly, I was under an impression that "only two similar objects
produce the same hash".
Secondly, how a python dictionary performs collision recovery in
the above case?
Finally, what is the initial capacity and of a python dictionary
in the first line of code?
hash() produces the same result as the int value you input, no surprise here.
>>> for i in range(10) : print i, hash(i)
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Dictionaries use a different hashing to store their values.

Categories