Funksiyalar
ushbu darsda chuquroq funksiyalarni o’rganamiz
funksiya nima
sodda namunalar
[1]:
def my_function():
print("Hello from a function")
print("Salom olam")
[2]:
my_function()
Hello from a function
Salom olam
Argumentlar
[4]:
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Emil Refsnes
Tobias Refsnes
Linus Refsnes
[5]:
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
Emil Refsnes
[8]:
# function with two arguments
def add_numbers(num1, num2, num3):
sum = num1 + num2 + num3
print("Sum: ", sum)
# function call with two values
add_numbers(5, 4, 5)
Sum: 14
return
kalit so’zi
[9]:
# function definition
def find_square(num):
result = num * num
return result
# function call
square = find_square(3)
print('Square:', square)
Square: 9
[11]:
# some more functions
def is_prime(n):
if n in [2, 3]:
return True
if (n == 1) or (n % 2 == 0):
return False
r = 3
while r * r <= n:
if n % r == 0:
return False
r += 2
return True
print(is_prime(78), is_prime(79))
False True
Bittadan ortiq qiymat qaytarish
[10]:
# function definition
def find_area_per(a, b):
area = a * b
p = 2 * (a + b)
return (area, p)
# function call
yuza, peremeter = find_area_per(3, 4)
print('Yuza: ', yuza)
print('Peremeter: ', peremeter)
Yuza: 12
Peremeter: 14
pass
kalit so’zi
[12]:
def future_function():
pass
# this will execute without any action or error
future_function()
Kelishuv qiymatlari
[16]:
# Python program to demonstrate
# default arguments
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)
# Driver code (We call myFun() with only
# argument)
myFun(10)
myFun(10, 100)
x: 10
y: 50
x: 10
y: 100
[18]:
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
I am from Sweden
I am from India
I am from Norway
I am from Brazil
Kalit so’zli qiymatlar
[19]:
# Python program to demonstrate Keyword Arguments
def student(firstname, lastname):
print(firstname, lastname)
# Keyword arguments
student(firstname='Geeks', lastname='Practice')
student(lastname='Practice', firstname='Geeks')
Geeks Practice
Geeks Practice
Majburiy qiymatlar
[21]:
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)
# You will get correct output because
# argument is given in order
print("Case-1:")
nameAge("Suraj", 27)
# You will get incorrect output because
# argument is not in order
print("\nCase-2:")
nameAge(27, "Suraj")
Case-1:
Hi, I am Suraj
My age is 27
Case-2:
Hi, I am 27
My age is Suraj
*args
argumenti
[22]:
def my_function(*kids):
print(type(kids)) # tuple
# kids = ("Emil", "Tobias", "Linus")
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
<class 'tuple'>
The youngest child is Linus
[24]:
# Python program to illustrate
# *args for variable number of arguments
def myFun(*argv):
for arg in argv:
print(arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks', 'sd', 43, 43)
Hello
Welcome
to
GeeksforGeeks
sd
43
43
[28]:
def my_sum(*values):
s = 0
for val in values:
s += val
return s
a = 1
b = 2
c = 3
d = 4
print(my_sum(a))
print(my_sum(a, b))
print(my_sum(a, b, c))
print(my_sum(a, b, c, d))
1
3
6
10
[31]:
# Python program to illustrate
# *kwargs for variable number of keyword arguments
def myFun(**kwargs):
for key, value in kwargs.items():
print(key, value)
# Driver code
myFun(first='Geeks', mid='for', last='Geeks', age=25)
first Geeks
mid for
last Geeks
age 25
Hujjat qatori
[34]:
# A simple Python function to check
# whether x is even or odd
# docstr
def evenOdd(x):
"""Function to check if the number is even or odd
x bu yerda butun son
"""
if (x % 2 == 0):
print("even")
else:
print("odd")
# Driver code to call the function
print(evenOdd.__doc__)
Function to check if the number is even or odd
x bu yerda butun son
[ ]:
evenOdd()
Kalit so’zli argumentlar
[6]:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
The youngest child is Linus
**kwargs
argumenti
[7]:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
His last name is Refsnes
[46]:
def my_function(a=2, b=3, /, c=5):
print(a, b, c)
my_function(a=3, c=5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[46], line 4
1 def my_function(a=2, b=3, /, c=5):
2 print(a, b, c)
----> 4 my_function(a=3, c=5)
TypeError: my_function() got some positional-only arguments passed as keyword arguments: 'a'
[49]:
def my_function(a, b, *, c=5):
print(a, b, c)
my_function(3, 6, c=5)
3 6 5