Friday, May 14, 2021

Variables

Tutorials 
---------

 

Splitting long varables 

>>> l = ("abcdefghijklmnopqrs" 

...      "tuvqxyz") 

>>> l 

'abcdefghijklmnopqrstuvqxyz' 

>>>  

Ways of swapping 

# Why Python Is Great: 

# In-place value swapping 

 

# Let's say we want to swap 

# the values of a and b... 

a = 23 

b = 42 

 

# The "classic" way to do it 

# with a temporary variable: 

tmp = a 

a = b 

b = tmp 

 

# Python also lets us 

# use this short-hand: 

a, b = b, a 

"is" vs "==" 

>>> a = [1, 2, 3] 

>>> b = a 

 

>>> a is b 

True 

>>> a == b 

True 

 

>>> c = list(a) 

 

>>> a == c 

True 

>>> a is c 

False 

 

• "is" expressions evaluate to True if two  

#   variables point to the same object 

 

• "==" evaluates to True if the objects  

#   referred to by the variables are equal 

Listing global and local variables 

>>> globals() 

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 'apple'} 

>>> locals() 

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 'apple'} 

>>>  

 

No comments:

Post a Comment