Lecture 05

In [1]:
def sayHello():
    print('Hello World!') # block belonging to the function
# End of function
In [2]:
sayHello() # call the function
Hello World!
In [4]:
def boogh():
    print('biib!')
def run(x0,dx):
    return dx+x0
In [16]:
def car(x,d,b=0):
    if b==1:
        boogh()
    r=run(x,d)
    return r
In [17]:
car(0,2,1)
biib!
Out[17]:
2
In [18]:
def printMax(a, b):
    if a > b:
        print(a,'is maximum')
    else:
        print(b,'is maximum')
    return
In [20]:
printMax(4, 4) # directly give literal values
4 is maximum
In [21]:
x = 5.2
y = 5.1
printMax(x, y) # give variables as arguments
5.2 is maximum
In [27]:
printMax(1,3) # directly give literal values
3 is maximum
In [28]:
def printMax(a, b):
    if a > b:
        print(a,'is maximum')
    elif a<b:
        print(b,'is maximum')
    else:
        print(a,'=',b)
    return
In [30]:
printMax(3,3)
3 = 3
In [38]:
def max_in_list(l1):
    max=0
    for i in range(0,len(l1),1):
        if l1[i]>max:
            max=l1[i]
    print(max, 'is maximum')
    return max
In [39]:
l=[0,2,8,6,7,5]
o=max_in_list(l)
8 is maximum
In [40]:
o
Out[40]:
8
In [43]:
ch=True
a=0
while ch:
    print(a)
    a=a+1
    if a==5:
        ch=False
0
1
2
3
4
In [44]:
number = 23
running = True
while running:
    guess = int(input('Enter an integer : '))
    if guess == number:
        print('Congratulations, you guessed it.')
        running = False # this causes the while loop to stop
    elif guess < number:
        print('No, it is a little higher than that.')
    else:
        print('No, it is a little lower than that.')
else:
    print('The while loop is over.')
    # Do anything else you want to do here
Enter an integer : 20
No, it is a little higher than that.
Enter an integer : 24
No, it is a little lower than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
In [50]:
def s(a):
    return a**0.5
In [56]:
l=5
i=1
x=8
l1=[]
while i<=l:
    x=s(x)
    l1.append(x)
    i=i+1
In [53]:
print(x)
1.0671404006768235
In [63]:
l=[9,8,0,5]
In [64]:
l.pop(2)
Out[64]:
0
In [65]:
l
Out[65]:
[9, 8, 5]
In [66]:
import numpy as np
In [68]:
np.sqrt?