Example analysis of variable assignment operations in Python programming

  • 2020-06-15 09:18:29
  • OfStack

This article gives an example of variable assignment in Python programming. To share for your reference, specific as follows:


#coding=utf8
'''''
Python I'm going to use the equals sign (=) Make an assignment. 
Python Is not directly assigned to 1 A value is assigned to 1 A variable, 
 Instead, refer to the object ( Not value ) Assign values to variables. 
'''
# Assignment operator 
Int=12
Float=12.2
String="hello"
List=[1,2,"hell"]
Touple=(4,"hell")
Dictionary={'one':1,
      'two':2,
      }
'''''python Does not return a value. '''
#add=(Int=Int+2) # Incorrect assignment statement 
add=Int=Int+2 #python Supports chain assignment 
print add,Int
''''' Augmented assignment : The equal sign and 1 A combination of operators 1 Start and reassert the result to the left variable. '''
Int+=10
print "The Int+10=",Int
Float-=0.2
print "The Float-0.2=",Float
Int*=5
print "The Int *5=",Int
Int/=5
print "The Int/5=",Int
Int%=5
print "The Int%2=",Int
Int **=2
print "The Int **=",Int
Int<<=2# Two shift to the left 
print "The Int <<2=",Int
Int>>=2# Two moves to the right 
print "The Int>>2=",Int
Int &=10# Bitwise facies 
print "The Int &10=",Int
Int ^=3# According to the not 
print "The Int^3=",Int
Int |=3# According to the phase or 
print "The Int|3=",Int
#List add 
List+=['EWANG']
print "The List:",List
# Multiple assignments 
a=b=c=d=e=f=8
print a,b,c,d,e,f
''''' Multiple assignment: To assign multiple variables simultaneously .
 When assigned in this way, the objects on both sides of the equal sign are tuples .
 Usually tuples need parentheses () enclosed .
 Parentheses are optional and are recommended for code readability 
'''
x,y,z=4,8,"ewang" # For code readability, parentheses are recommended 
print x,y,z
(x,y,z)=(4,8,"ewang" )
print x,y,z
#Python The multi-variable assignment method can realize the value exchange of two variables without intermediate variables 
(x,y)=(y,x)
print x,y

More Python related content interested readers to view this site project: "Python introduction and advanced tutorial", "Python string skills summary", "Python list (list) skills summary", "Python coding skills summary", "Python data structure and algorithm tutorial", "Python function using techniques" and "Python file and directory skills summary"

I hope this article has been helpful in Python programming.


Related articles: