Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python core/Day 3.ipynb
3074 views
Kernel: Python 3
def test(): print("HI All")
#Functions def demo(x,y): print(x+y) return(y) demo(21,3)#will return the value that is allowed here it is X

Return() is not mandatory in Python

def cal(x,y,z): print("sum =",x+y+z) print("mul=",x*y*z) cal(3,2,4) cal(8,9,6) cal(7,6,7)

Different variable class can be passed within a function

def Emp(name=" ",age=10,f=0.0): name=input("Enter your name") age = int(input("Enter your age=")) exp= float(input("Experince=")) print("Name=%s age=%d Exp=%f"%(name,age,exp)) Emp()
def data(name="",age=12,Gender="F"): print("Name=%s age=%d Gender=%s"%(name,age,Gender)) data("ROY",12,"M") data("A",3,"F")

Use math module for mathemathical functions

import math x=int(input("Enter any number =")) math.factorial(x) math.exp(x) math.pow(x,4) math.acosh(x) math.pi math.radians(60) math.sin(1.04719) math.log(100) math.log10(100) math.sqrt(x) round(math.sqrt(x),3)
Enter any number =45
6.708

- 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)

- Q Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters

d={"UPPER_CASE":0, "LOWER_CASE":0} d["UPPER_CASE"]+=2 d
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")

Lamba Function

ambda 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.

x=lambda a : a*3 print(x(5))
a=[1,3,4,6,23,44,56,78,90,54,60] list_odd = list(filter(lambda x:(x %6==0),a)) print(list_odd)
##Lambda functions can take any number of arguments x = lambda a, b : a * b print(x(5, 6))

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[1]) 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)
12
def myfunc(n): print("The number:",n) return lambda a : n**a r = myfunc(6) print(r(4))
The number: 6 1296

def mydata(n):

return lambda a,b:(a+b)*n var = mydata(4) print(var(2,3))

Classes in Python &OO programming

class Program1: "HI" print(Program1)#Tell about status of class. print(60*"*")
class Program2: name="RAM" age = 29 print("NAME is %s and age is %d"%(Program2.name,Program2.age)) print(60*"*")
#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*"*")
### Class and Function class X: "Classses and functions" myroll= 9 def student(self): print('This is Science Class') ob = X() print(X.student) print(ob.student) ob.student() print(8*"*") 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__)