0% found this document useful (0 votes)
2 views6 pages

Notes 220625

The document provides an overview of various types of function arguments in Python, including positional, keyword, default, and variable-length arguments. It includes examples demonstrating how to define and call functions with these arguments, as well as the use of local, global, and nonlocal variables. Additionally, it lists several programming assignments related to string manipulation and mathematical computations.

Uploaded by

patilharshada974
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Notes 220625

The document provides an overview of various types of function arguments in Python, including positional, keyword, default, and variable-length arguments. It includes examples demonstrating how to define and call functions with these arguments, as well as the use of local, global, and nonlocal variables. Additionally, it lists several programming assignments related to string manipulation and mathematical computations.

Uploaded by

patilharshada974
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

# Function arguments

# 1. Positional arguments
# 2. Keyword arguments
# 3. Default arguments
# 4. Variable length arguments(*args)
# 5. Keyword Variable length arguments(**kwargs)
from platform import processor

def myhobbies(**kwargs):
print("numbers : ",kwargs)

myhobbies()
myhobbies(play='Cricket')
myhobbies(play='Cricket',watch='reels')
myhobbies(play='Cricket',watch='reels',homework='cleaning')

# Keyword Variable length arguments(**kwargs)


def my_mob(cno,model,ver,pr,**spec):
print(f" mob id : {cno} model {model} {ver} price:{pr} specification :{spec}")
my_mob(cno=1111,model="Vivo",ver="s45",pr=29999,RAM="8") # TypeError: my_mob() got
an unexpected keyword argument 'RAM'
my_mob(cno=2424,model="Oppo",ver=5,pr=44999,RAM='4gb',interal_mem="64gb",FC='50mpx'
)
my_mob(cno=9999,model="Redmi",ver="g25",pr=34999.99,RAM=8,memory=256,rear=16,front=
50,processor='octacore')
my_mob(model="Mi",ver="new",pr=77777.77,RAM=8,memory=256,cno=2222,rear=16,front=50,
processor='octacore')
my_mob(cno=5555,model="Jio",ver="2025",pr=29999,ram=8)
# Keyword Variable length arguments(**kwargs)
def my_mob(cno,model,ver,pr,*spec):
print(f" mob id : {cno} model {model} {ver} price:{pr} specification :{spec}")
# my_mob(cno=1111,model="Vivo",ver="s45",pr=29999,RAM="8") # TypeError: my_mob()
got an unexpected keyword argument 'RAM'

# Variable length arguments(*args)


def my_mob(cno,model,ver,pr,*spec):
print(f" mob id : {cno} model {model} {ver} price:{pr} specification :{spec}")
# my_mob(1111,"Vivo","s45",29999,"RAM 8","memory: 256")
# my_mob(5555,"Iphone",'A2',20000)
# my_mob(6666,"sumsung","s4",69999)
# my_mob(7777,'oppo','note7',49999,"Internal : 512")

# Variable length arguments(*args)


def matfunc(*args):
print("numbers : ",args)
# matfunc()
# matfunc(100)
# matfunc(10,20)
# matfunc(11,22,33,44,55,66,77)

# default arguments
# --> 1. default positional arguments
# --> 2. default keyword arguments
# if argument present --> given value
# if argument absent --> default value
# default argument should present at last
def my_display(empno,empnm,emob=99999999,eaddr="Pune",sal=10000.00,cnt='India'):
print(f"""
Employee number : {empno}
Name of employee : {empnm}
mobile number : {emob}
city : {eaddr}
salary: {sal}
country: {cnt}""")
# my_display(2006,'Rajaram','Beed')
# my_display(2002,'Ranjit',781678133,'Baramati',32452.33)
# my_display(2004,'Alian',445578133,'London',44452.33,'UK')
# my_display(2006,'Rajaram',999978133,'Beed')
# my_display(emob='4455332266',sal=55000.55,empnm='Hari',empno=1002) # keyword
arguments

# Keyword arguments
# no_of_parameters == no_of_arguments
def my_display(empno,empnm,emob,eaddr,sal):
print(f"""
Employee number : {empno}
Name of employee : {empnm}
mobile number : {emob}
city : {eaddr}
salary: {sal}""")

#
my_display(empno=1001,empnm='Shejal',emob='25131313',eaddr='Kolhapur',sal=241313.43
) # keyword arguments
# my_display(emob='4455332266',sal=55000.55,eaddr='Nagpur',empnm='Hari',empno=1002)
# keyword arguments
# Not required to maintain sequence
# key should written for arguments

# positional arguments
# no_of_parameters == no_of_arguments
def my_display(empno,empnm,emob,eaddr,sal):
print(f"""
Employee number : {empno}
Name of employee : {empnm}
mobile number : {emob}
city : {eaddr}
salary: {sal}""")

# my_display(101,"Sachin",78678411133,"Mumbai",4500.44) # positional arguments


# my_display("Rakesh",105,3176861,34781.43,"Pune") # positional arguments
# positional arguments --> sequence should be maintained
import sys
sys.exit(0)

def my_func():
print("I am very happy")
my_func()

def add(a,b):
ans = a+b
print(ans)
add(10,40)
# function variable
# local, global, nonlocal

a = 10
def outer():
global a
a = 22
def inner1():
a = 700
print(a)
a = 440
def inner2():
print(a)
print(a)
inner2()
print(a)
print(a)
inner1()
print(a)

print("start:",a) # 1 --> 10
outer() # 22,700,440,440,440,22
print("end",a) # end --> 22
import sys
sys.exit(0)

a = 10
def outer():
global a
a = 22
def inner1():
global a
print(a)
a = 440
def inner2():
print(a)
print(a)
inner2()
print(a)
print(a)
inner1()
print(a)

print("start:",a) # 1 --> 10
outer() # 22,22,440,440,440,440
print("end",a) # end --> 440
import sys
sys.exit(0)

a = 10
def outer():
a = 22
def inner1():
nonlocal a
print(a)
a = 440
def inner2():
print(a)
print(a)
inner2()
print(a)
print(a)
inner1()
print(a)

print("start:",a) # 1 --> 10
outer() # 22,22,440,440,440,440
print("end",a) # end --> 10
import sys
sys.exit(0)
a = 10
def outer():
print(a)
def inner1():
a = 44
def inner2():
print(a)
print(a)
inner2()
print(a)
inner1()

print("start:",a) # 1 --> 10
outer() # 10,44,44,44
print("end",a) # end --> 10
import sys
sys.exit(0)

a = 10
def outer():
a = 40
print("2:", a)
a = 77
print("3:", a)
a = 500
print("4:",a)

print("1:",a) # 1 --> 10
outer() # 2,3,4 --> 40,77,500
print("5:",a) # 5--> 10
import sys
sys.exit(0)

a = 10
def outer():
global a
print("2:", a)
a = 77
print("3:", a)
a = 500
print("4:",a)

print("1:",a) # 1 --> 10
outer() # 2,3,4 --> 10,77,500
print("5:",a) # 5--> 500
import sys
sys.exit(0)
# variable --> if local present --> local
# if not local --> global
a = 10
b = 20
def outer():
global a
a = 40
print("2:",a)
a = 500
c = 30
print("3:",a)

print("1:",a) # 1 --> 10
outer() # 2 --> 500
print("4:",a) # 3 --> 10
import sys
sys.exit(0)

a = 10
b = 20
def outer():
a = 40
print("2:",a)
a = 500
c = 30
print("3:",a)

print("1:",a) # 1 --> 10
outer() # 2 --> 500
print("4:",a) # 3 --> 10
import sys
sys.exit(0)

a = 10
def outer():
a = 500
print("2:",a)

print("1:",a) # 1 --> 10
outer() # 2 --> 500
print("3:",a) # 3 --> 10
import sys
sys.exit(0)
a = 10
def outer():
print("2:",a)
print("1:",a) # 1
outer() # 2
print("3:",a) # 3

Assginment
# 1 write a program to REVERSE the content of given string
# input --> welcome | ouput --> emoclew
# 2 write a program to find occurrences of each characters in string
# 3. Write a program for following requirement
# input --> abacacbe | output --> '3a2b2c1e'
# 4. write a program for following requirement
# input 'abacacbe' then output is 'a3b2c2e1'
# 5 write a program to find number of occurrences of each vowel in given string
# s = "Electromagnetics"
# 6 Reverse the given number
# 2754 --> 4572
# 7 Write a program which can compute the factorial of a given numbers
# 5! =5*4*3*2*1
# 8 Write a program for addition of digits of given number
# 2754 --> 18
# 9 Write a program that accepts sequence of lines as input and
# prints the lines after making all characters in the sentence capitalized.
# input --> Hello world Practice makes perfect
# output--> HELLO WORLD PRACTICE MAKES PERFECT

You might also like