"hello world this is just a sentence".split() # it splits in white spaces['hello', 'world', 'this', 'is', 'just', 'a', 'sentence']
Login to Lab using your credentials. There is a notebook with name 1-4.ipynb already created for you. Open that and use it for today’s training.
Shut down all previous notebooks.
['hello', 'world', 'this', 'is', 'just', 'a', 'sentence']
Problem
x = [1, 1, 1]
def appendzero(y):
y.append(0) # mehtods operate on the data of the object
appendzero(x)
print(x)
x = [1, 1, 1]
def appendzero(y):
y.append(0) # mehtods operate on the data of the object
appendzero(x)
print(x)[1, 1, 1, 0]
Problem
x = [1, 1, 1]
def appendzero(y): # y is already given as an argument
y = y + [0]
appendzero(x)
print(x)
x = [1, 1, 1]
def appendzero(y): # y is already given as an argument
y = y + [0] # beacasue of assignment operator, y is local so we willl not see any change in x
appendzero(x)
print(x)[1, 1, 1]
x = [1, 1, 1]
def appendzero(y):
y.append(0) # mehtods operate on the data of the object
appendzero(x) # this will link y to same place as x. if I call method og y , it will actually call of x
print(x)[1, 1, 1, 0]
31.400000000000002
cylinder_volume(10, 1) # this will result into different values... It si allowed to alter the position of argument314.0
Enter the radius in cm 1
Enter the height in cm 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[44], line 1 ----> 1 interactive_cyl_volume() Cell In[43], line 8, in interactive_cyl_volume() 6 def interactive_cyl_volume(): 7 r, h = take_inputs() ----> 8 return cylinder_volume(radius=r, height=h) Cell In[36], line 2, in cylinder_volume(radius, height) 1 def cylinder_volume(radius, height): # radius , height are calles as argument ----> 2 return 3.14*radius*radius *height TypeError: can't multiply sequence by non-int of type 'float'
def cylinder_volume(radius, height): # radius , height are calles as argument
return 3.14*radius*radius *height
def take_inputs():
radius = input("Enter the radius in cm")
height = input("Enter the height in cm")
return float(radius), float(height) # sequence is important
def interactive_cyl_volume():
r, h = take_inputs()
return cylinder_volume(radius=r, height=h) Cell In[58], line 1 func(b=1 , c=3, 2) ^ SyntaxError: positional argument follows keyword argument
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[59], line 1 ----> 1 func(b=1, c=3) TypeError: func() missing 1 required positional argument: 'a'
if a > b: # code block
print("a > b")
print("one more line")
elif "that" in s:
print("that in s")
else:
print("none of the conditions matched")none of the conditions matched
problem
Write a function maximum3 which finds miximum from three numbers
for name in scores: # it will go over all the keys from dictionary
# name is a loop variable which changes value in every iteartion
print(name, scores[name])Alex 20
Alice 24
Tom 23
Jerry 25
problem
Write a function product which finds product of all elements from a list. You well be given a list of numbers!
>>> product([3, 2, 4])
24
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[131], line 1 ----> 1 product([1, 2, 3, 5]) NameError: name 'product' is not defined
Let us find product of all these numbers manually
1, 2, 3, 4, 5, 6
1*1 = 1
1*2 = 2
2*3 = 6
6*4 = 24
24*5 = 120
120*6 = 720
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[148], line 1 ----> 1 product(1, 2, 34, 6) # every number here is taken as separate argument TypeError: product() takes 1 positional argument but 4 were given
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[149], line 1 ----> 1 words NameError: name 'words' is not defined