Python-Intermediate
def even_odd(num):
if num%2==0:
print("number is even")
else:
print("number is odd")
even_odd(24)
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')
hello('sid',20)
def hello(*args, **kwargs):
print(args)
print(kwargs)
hello('sid','saxena',age=29,dob=2000)
lst=['sid','saxena']
dict_args={'age': 20 ,'dob':2000}
hello(*lst,**dict_args)
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)
def addition(a,b):
return a+b # Single expression can only be converted
addition(4,5)
addition = lambda a,b:a+b
addition(5,6)
def even(num):
if num%2==0:
return True
even(24)
even1 = lambda num:num%2==0
even1(12)
def add(x,y,z):
return x+y+z
add(1,2,3)
three_add = lambda x,y,z:x+y+z
three_add(1,2,3)
def even_odd(num):
if num%2==0:
return True
else:
return False
even_odd(23)
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
list(map(even_odd,lst))
list(map(lambda num:num%2==0,lst))
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
list(filter(lambda num:num%2==0,lst))
lst1=[]
def lst_square(lst):
for i in lst:
lst1.append(i*i)
return lst1
lst_square([1,2,3,4,5,6,7])
lst=[1,2,3,4,5,6,7]
#list comprehension
[i*i for i in lst ]
list1=[i*i for i in lst ]
print(list1)
[i*i for i in lst if i%2==0]
print("hello")
str="hello"
print(str)
def greetings(name):
return "hello {}".format(name)
greetings('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')
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)
def welcome_email(name,age):
return "welcome {name1}. is your age is {age1}".format(age1=age,name1=name)
welcome_email('sid',20)
lst = [1,2,3,4,5,6,7] # this whole list is getting initialised in the memory
for i in lst:
print(i)
list1=iter(lst) #but in case of iterators whole listdose not get stored in memory it will get accessed only via next
list1
next(list1)
next(list1)
for i in lst1:
print(i)