Unit 3
Unit 3
min([10,4,7,3,9]) 3
max([10,4,7,3,9]) 10
sum([10,4,7,3,9]) 33
import math
math.sqrt()
import calendar
print(calendar.month(2024,12))
>>>20
>>>8 (used default value since arguments not supplied)
What is meant by variable length arguments?
Sometimes you might need to pass more arguments than have already been specified. Variable-
Length arguments can be defined using asterisk (*).
def sum(x,y,z):
print("sum of three nos :", x+y+z)
sum(5,10,15,20,25)
>>> TypeError: sum() takes 3 positional arguments but 5 were given
eg
a=input() # a will be a string
b=int(a) # string converted to integer
These lines can be composed into single line as below
b=int(input())
return x*fact(x-1)
n=int(input(“ enter a number ”)
print(fact(n))
>>> enter a number 5
>>> 120
Suppose n=4
fact(4) 1st call
4*fact(3) 2nd call
4*3*fact(2) 3rd call
4*3*2*fact(1) 4th call
4*3*2*1 return from 4th call
4*3*2 return from 3rd call
4*6
24
def fibo(n):
If n<=2:
return 1
return fibo(n-1)+fibo(n-2) # we have two base class fibo(1) and fibo(2)
fibo(4)
fibo(3)+fibo(2)
fibo(2)+fibo(1)+fibo(2)
def recurse()
recurse()
def loc():
y = "local"
print(y)
loc()
print(y)
>>>local
>>> NameError: name 'y' is not defined
c = 1 # global variable
def add():
print(c)
add()
>>> 1
c = 1 # global variable
def add():
c = c + 2 # Modify a global variable make it local
print(c)
add()
>>> UnboundLocal Error: local variable 'c' referenced before assignment
Without using the global keyword we cannot modify the global variable inside the function
but we can only access the global variable.
x = 0 # global variable
def add():
global x
x=x+5
print ("Inside", x)
add()
print ("Outside", x)
>>>Inside 5
>>>Outside 5
x=5
def loc():
x = 10
print ("local x:", x)
loc()
print ("global x:", x)
Output:
local x: 10
global x: 5
STRINGS
Define Strings
In python string is a sequence of characters, numbers and special characters enclosed in single
or double quotes. Multilines string can be enclosed using triple quotes
S1=’python’
S2=”python”
S3=’”””python
Is
Fun”””
F=”computer”
F[0] c
F[2] m
F[7] r
F[len(F)-1] r
F[-1] r
F[-3] t
F[8] IndexError
word=”Computer”
for i in word:
print(i, end=” “) # please note the usage of end=” “ in print command
Computer
i=0
while i<len(word):
print(word[i], end= “ “)
i+=1
Computer
word=” Computer”
word[0:5] “Comp”
word[:3] “Com”
word[4:] “uter”
word[3:6] “put”
word[:] “Computer”
word=”Computer”
word[0]=’c’ TypeError
upper() : take a string and return a new string with all upper characters
word=”Computer”
x=word.upper()
print(x) “COMPUTER”
lower() : take a string and return a new string with all lower characters
word=”COMPUTER”
print(word.lower()) “computer”
word=”computer”
print(word.capitalize()) “Computer”
s=”hello world”
print(s.split()) [‘hello’,’world’]
s=”hello world”
print(s.find(“wo”) 6
print(s.find(“ma”) -1
print(s.find(“wo”,0,5) -1 #checking only from 0 to 5. Hence not found
length
a=”hello”
print(len(a)) 5 # find the length of string
import string
string.digits “0123456789”
string.ascii_letters “abc…xyzABC…XYZ”
string.ascii_lowercase “abcd….xyz”
string.ascii_uppercase “ABC….XYZ”
string.hexdigits “0123456789abcdefABCDEF”
string.octdigits “01234567”
string.punctation
string.printable
string.whitespace