Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python Basics/Day 4 Functions and Classes.ipynb
3074 views
Kernel: Python 3 (ipykernel)
## Function without any argument def session(): x=int(input("Number of people in session")) y=input("Name of session") session()
def details(name =" ", age=int): print("Name = %s Age = %d" %(name,age)) details("ashi",25) details("abc",24)
def details (name=" ",age=int,job_role=""): #print("Name is %s age is %d and job role is %s"%(name,age,job_role)) return name details("A",32,"AC")
#Create a funtion named cali , add,sub,mul,div choice=+(add),-(sub),mul(*),div(/) def calci(x,y): x=input("" +,-<*,/) if choice=="*":
#Functions with Arguments x=int(input("input first num= ")) y=int(input("input second num= ")) def cal(x,y): choice=input("Enter your choice( +,*,-,/)") if choice=="+": print("sum=",x+y) elif choice=="-": print("sub=",x-y) elif choice=="*": print("prod=",x*y) else: print("option not aviliable") cal(x,y)
x = int(input("Enter 1st no.: ")) y = int(input("Enter 2nd no.: ")) def calci(x,y): choice = input("Enter your choice i.e. +,-,*,/") if choice == "+": print("Sum = ",x+y) elif choice == "-": print("Sub = ",x-y) elif choice == "*": print("Mul = ",x*y) elif choice == "/" and y !=0: print("Div = ",x/y) else: print("Invalid choice") calci(x,y)

Return() is not mandatory in Python

  • To let a function return a value, use the return statement:

print(3+4) print(6+8) 4
def te(x,y): #print(x*y) return x te(8,7) te(4,5)

Different variable class can be passed within a function

def Emp(name,age,f): print(name,age,f,sep="\t") Emp("Ashi",2,6) Emp(1,2,3)
def dex(*emp): print("The oldest employee is: " + emp[-1]) dex("Abc","ab12","er3") dex("A","B") dex ("ram","sam","lam","man")
def food(fruit): for i in fruit: print(i) fruits=["kiwi","apple","pie","abc"] food(fruits)

Practice Questions on Functions

Q Write a Python function to sum all the numbers in a list.

def sum(numbers): total = 0 for x in numbers: total += x return total #print(sum((12,34,55))) sum((12,12))

Q Write a Python funtion to check whether a given number is the given range

def test_range(n): if n in range(12,50): print( " %s is in the range"%str(n)) else : print("The number is outside the given range.") test_range(13)
def breakdown(): x= input("Enter a string") countupper = 0 countlower = 0 for i in x: if i.isupper(): countupper+=1 else: countlower+=1 print(countupper, countlower) breakdown()
### Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters def string_test(s): d={"UPPER_CASE":0, "LOWER_CASE":0,"SPACES_":0} for c in s: if c.isupper(): d["UPPER_CASE"]+=1 elif c.islower(): d["LOWER_CASE"]+=1 else: pass print ("Original String : ", s) print ("No. of Upper case characters : ", d["UPPER_CASE"]) print ("No. of Lower case Characters : ", d["LOWER_CASE"]) string_test("PRACTICE More")

lambda - one line function which can take any number of argumnets

Lamba Function

  • The lambda keyword is used to create anonymous functions

  • This function can have any number of arguments but only one expression, which is evaluated and returned.

  • One is free to use lambda functions wherever function objects are required.

  • You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.

z= lambda a,b,c:a*b/c z(8,9,3)
## Create a lamba function that returns cube of a number. z = lambda s:s*s*s z(9)
a=[2,4,-9,4,6,8,44,20,33] even_list = list(filter(lambda x:(x%2 == 0),a)) even_list
z=[1,2,3,4] x=list(map(lambda y:(y+2),z)) print(x)
##Lambda functions can take any number of arguments x = lambda a, b,c : a**2+b*3+c print(x(5, 6,7))

Write a Python program to sort a list of tuples using Lambda

subject_marks = [('English', 88), ('Science', 90), ('Maths', 97), ('Social sciences', 82)] print("Original list of tuples:") print(subject_marks) subject_marks.sort(key = lambda x: x[0]) print("\nSorting the List of Tuples:") print(subject_marks)

The power of lambda is better shown when you use them as an anonymous function inside another function.

def my(r): print(4*r) my(3)
def myfunc(n): print("The number:",n) return lambda a : n**a r = myfunc(2) print(r(3))
def mydata(n): return lambda a,b:(a+b)*n var = mydata(4) print(var(2,3))

Date Time Module

## Time Module: from time import time, ctime t = time() ctime(t)## for current time
import time current_local = time.localtime() current_local.tm_zone
import time a=time.strftime('%m-%y-%d', time.localtime()) t = time.strftime("%d/%m/%y|%H:%M:%S") print (a) print(t)
## %%time import time ## Provide delay in a program print("Hi") time.sleep(2) print("Welcome")
Hi Welcome
print(time.strftime("%y/%m/%d|%H:%M:%S"))
Tstr=time.strftime("%A/%B|%a/%b") # Day|MONTH print(Tstr) tstr=time.strftime("%H:%M:%p")# Current time IN AM/PM print(tstr) w=time.strftime("%z") print(w)
import datetime print("Current date and time: " , datetime.datetime.now()) print("Current year: ", datetime.date.today().strftime("%Y")) print("Month of year: ", datetime.date.today().strftime("%B"))#%b print("Week number of the year: ", datetime.date.today().strftime("%W")) print("Weekday of the week: ", datetime.date.today().strftime("%w")) print("Day of year: ", datetime.date.today().strftime("%j")) print("Day of the month : ", datetime.date.today().strftime("%d")) print("Day of week: ", datetime.date.today().strftime("%A"))
## Python code to check time of execution of a program # starting time start = time.time() # program body starts for i in range(10): print(i) # sleeping for 1 sec to get 10 sec runtime time.sleep(1) # program body ends # end time end = time.time() # total time taken print(f"Runtime of the program is {end - start}")

Classes in Python &OO programming

class Program1: "HI" #print(Program1)#Tell about status of class. Program1
__main__.Program1
class Program1: name="RAM" age = 29 print("NAME is %s and age is %d"%(Program1.name,Program1.age))
NAME is RAM and age is 29
# Create a class name mobile store, Price=0.0,Type="",Brand, Three class empdetails: name="RAM" age = 29 Emp1= empdetails() Emp2 = empdetails() Emp1.name ="A" Emp1.age = 9 Emp2.name = "B" Emp2.age = 32 #print(" %s %d "%(Emp1.name,Emp1.age)) #print(" %s %d "%(Emp2.name,Emp2.age)) for x in range(1,3): print(eval("Emp%d.name"%(x)), eval("Emp%d.age"%(x)))
A 9 B 32
x=6 eval("x**x*x+98+x")
#Class &Objects class Phone: Color=" " Price =00 Type=" " ob1=Phone()#Creation of objects and assinging it to class phone. ob2=Phone() ob1.Color="RED" ob1.Price=10000 ob1.Type="Smart" ob2.Color="Black" ob2.Price=12000 ob2.Type="Smart" print("This is a %s phone of %s color and its price is %drs"%(ob1.Type,ob1.Color,ob1.Price)) print("This is a %s phone of %s color and its price is %drs"%(ob2.Type,ob2.Color,ob2.Price)) print(50*"*")

- self is used to represent the instance of the class. With this keyword, you can access the attributes and methods class in python. it helps to bind attributes with the given arguments.

- In Python we have methods that make the intance to be passed automatically, but not received automatically.

class X: "Classses and functions" myroll= 9 def student(self): print('This is Science Class') rani = X() print(X.student) print(rani.student) rani.student()
<function X.student at 0x000001EB420864C0> <bound method X.student of <__main__.X object at 0x000001EB418EB220>> This is Science Class

Class name calci , compute sum, sub, divsion using a fucntion called finalo/p.

class Y: a = 9 b = 3.3 c = 23.4 d = 0.0 def details(r): r.c= r.a * r.b r.d = r.a/r.b t=round(r.d,3) print(r.c,t,sep=",") cal1=Y() cal2= Y() cal1.a=5 cal1.b=4.7 cal1.details()
23.5,1.064
### Class and Function class Shop: "Things in MYshop" Quantity = 10 Type = "" price=500.00 def bill(self): print("your bill for % s dress of %d quantity is %drs:"%(self.Type,self.Quantity,self.price)) coustmer1=Shop() coustmer1.Quantity=5 coustmer1.Type="saree" coustmer1.price=20000 coustmer1.bill() print(50*"*")
# dictionary from an class's fields. class dObj(): def __init__(self): self.x = 'age' self.y = 'name' self.z = 'Gender' def do(self): pass d = dObj() print(d.__dict__)