Boolean And None
print(False == 0) # True
print(True == 1) # True
print(True + True + True) # 3
print(True + False + False) # 1
print(None == 0) # False
print(None == []) # False
# bool(empty variable, None, 0, null) = False
print(bool("hi")) # True
print(bool(0)) # False
print(bool("")) # False
print(bool([])) # False
Is and In
print("a" in "apple") # True
for i in "apple":
print(i) # "a", "p", "p", "l", "e"
print(' ' is ' ') # True: string is immutable
print({} is {}) # False: dictionary is mutable
Return and Yield
def fun():
S = 0
for i in range(10):
S += i
return S
print(fun()) # 45
def fun():
S = 0
for i in range(10):
S += i
yield S
for i in fun():
print(i) # 0, 1, 3, 6, 10, 15, 21, 28, 36, 45
Local and Global variables
''' accessing local variable in global scope '''
def f():
s = "abc"
print("Hi", s) # Hi abc
f()
print(s) # NameError: name 's' is not defined
''' local variable with same name as that of global variable '''
def f():
s = "Hello"
print(s) # Hello
s = "Hi there"
f()
print(s) # Hi there
''' trying to access global variable in local scope '''
def f():
s += " there" # UnboundLocalError: local variable 's' referenced before assignment
print("Inside Function", s)
s = "Hi"
f()
''' accessing global variable in local scope using global keyword '''
def f():
global s
s += ' there'
print(s) # Hi there
s = "Hello"
print(s) # Hello
s = "Hi"
f()
print(s) # Hello
''' global keyword '''
a = 1
def f(): # Uses global because there is no local 'a'
print(a) # 1
def g(): # Variable 'a' is redefined as a local
a = 2
print(a) # 2
def h(): # Uses global keyword to modify global 'a'
global a
a = 3
print(a) # 3
print(a) # 1
f()
print(a) # 1
g()
print(a) # 1
h()
print(a) # 3
''' global in nested functions '''
def add():
x = 15
def change():
global x
x = 20
print(x) # 15
change()
print(x) # 15
add()
print(x) # 20
Global and nonlocal
# global variable
x = 15
# nonlocal keyword
def fun():
var1 = 25
def gun():
nonlocal var1 # tells python explicitly to use var1 initialize in fun()
global x # tells python explicitly to use x initialized globally
var1 = var1 + x
print(var1) # 40
gun()
fun()
== vs is
What is the difference between == and
is == compares the
values of both the operands and checks for value equality. Whereas the
is operator checks whether both the operands
refer to the same object or not (present in the same memory location)
list1 = []
list2 = []
list3=list1
print(list1 == list2) # True
print(list1 is list2) # False
print(list1 is list3) # True
list3 = list3 + list2
print(list1 is list3) # False
Remove duplicates from a list
What are some ways to remove duplicates from a list?
l1 = ["a", "b", "c", "a", 1, 3, 3, 5]
''' using a set '''
l2 = list(set(l1))
''' using keys in a dictionary '''
l3 = list(dict.fromkeys(l1))
Shallow copy
f = [1,2,3,4]
s = f
s.append(5)
print(s) # [1,2,3,4,5]
print(f) # [1,2,3,4,5]
Shuffle a list
from random import shuffle
f = [1,2,3,4]
shuffle(f) # shuffle values in f into a random order
Floor division
print(5//2) # 2
print(-5//2) # -3
print(5//-2) # -3
print(5.0//2) # 2.0
print(-5.0//2) # -3.0
print(5.0//-2) # -3.0
Multiply list and int
l1 = [1,2,3,4,5]
print(l1 * 2) # [1,2,3,4,5,1,2,3,4,5]
Multiple assignment
x, y, z = 1, 2
print(x, y, z) # ValueError: not enough values to unpack (expected 3, got 2)
nonlocal keyword
- The nonlocal keyword is used to work with variables inside nested functions, where the variable should not belong to the inner function
- Use nonlocal to declare that the variable is not local to the nested function, allowing you to modify the variable's value in the enclosing scope.
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("Inner:", x)
inner()
print("Outer:", x)
outer() # Inner: nonlocal \n Outer: nonlocal