def even_odd(num):
    if num%2==0:
        print("number is even")
    else:
        print("number is odd")
        
even_odd(24)
number is even
def hello(name, age=29): # name  is positional argument and age is keyword argument
    print("my name is {} and age is {}".format(name, age))
hello('sid')
my name is sid and age is 29
hello('sid',20)
my name is sid and age is 20
def hello(*args, **kwargs):
    print(args)
    print(kwargs)
    
hello('sid','saxena',age=29,dob=2000)
('sid', 'saxena')
{'age': 29, 'dob': 2000}
lst=['sid','saxena']
dict_args={'age': 20 ,'dob':2000}
hello(*lst,**dict_args)
('sid', 'saxena')
{'age': 20, 'dob': 2000}
lst = [1,2,3,4,5,6,7]

def evenoddsum(lst):
    even_sum=0
    odd_sum=0
    for i in lst:
        if i%2==0:
            even_sum += i
        else:
            odd_sum += i
            
    return even_sum,odd_sum
evenoddsum(lst)
(12, 16)

Lambda functions

def addition(a,b):
    return a+b               # Single expression can only be converted
addition(4,5)
9
addition = lambda a,b:a+b
addition(5,6)
11
def even(num):
    if num%2==0:
        return True
even(24)
True
even1 = lambda num:num%2==0 
even1(12)
True
def add(x,y,z):
    return x+y+z
add(1,2,3)
6
three_add = lambda x,y,z:x+y+z
three_add(1,2,3)
6

Map Function

def even_odd(num):
    if num%2==0:
        return True
    else:
        return False
    
even_odd(23)
False
lst=[1,2,3,4,5,6,7,8] # apply same function on multiple values
map(even_odd,lst) # in order to instantiate convert it into a list ## memory is not intialised yet
<map at 0x2a398c7e248>
list(map(even_odd,lst))
[False, True, False, True, False, True, False, True]
list(map(lambda num:num%2==0,lst))
[False, True, False, True, False, True, False]

Filter function

 def even(num):
        if num%2==0:
            return True
lst=[1,2,3,4,5,6,7]
list(filter(even,lst)) # return elements which satisfy the condition
[2, 4, 6]
list(filter(lambda num:num%2==0,lst))
[2, 4, 6]

List Comprehension

provide a concise way to create lists, It consists of braces containing an expression followed by for clause, then zero or more for or if clauses

lst1=[]

def lst_square(lst):
    for i in lst:
        lst1.append(i*i)
    
    return lst1
lst_square([1,2,3,4,5,6,7])
[1, 4, 9, 16, 25, 36, 49]
lst=[1,2,3,4,5,6,7]

#list comprehension

[i*i for i in lst ]
[1, 4, 9, 16, 25, 36, 49]
list1=[i*i for i in lst ]
print(list1)
[1, 4, 9, 16, 25, 36, 49]
[i*i for i in lst if i%2==0]
[4, 16, 36]

String Formatting

print("hello")
hello
str="hello"

print(str)
hello
def greetings(name):
    return "hello {}".format(name)
greetings('sid')
'hello sid'
def welcome_email(firstname,lastname):
    return "welcome {}. is your last name is {}".format(firstname,lastname) # order can not be altered
welcome_email('sid','saxena')
'welcome sid. is your last name is saxena'
def welcome_email(name,age):
    return "welcome {name}. is your age is {age}".format(age=age,name=name) # now ordering can alter
welcome_email('sid',20)
'welcome sid. is your age is 20'
def welcome_email(name,age):
    return "welcome {name1}. is your age is {age1}".format(age1=age,name1=name)
welcome_email('sid',20)
'welcome sid. is your age is 20'

List Iterables vs Iterators

lst = [1,2,3,4,5,6,7] # this whole list is getting initialised in the memory 

for i in lst:
    print(i)
1
2
3
4
5
6
7
list1=iter(lst) #but in case of iterators whole listdose not get stored in memory it will get accessed only via next
list1  
<list_iterator at 0x2a3996beb08>
next(list1)
1
next(list1)
2
 for i in lst1:
        print(i)
1
4
9
16
25
36
49