-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFunctions.py
More file actions
115 lines (74 loc) · 2.76 KB
/
Functions.py
File metadata and controls
115 lines (74 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Function with default parameter
def default_parameter(city='Lahore'):
print(f"the name of City is : {city}")
default_parameter('Okara')
# Function with unknown number of positional arguments (*args)
def unknown_parameters(*name):
print(f"The name of the winner is' {name}")
unknown_parameters('Ali', 'Haider', 'Hafiz')
# Function with keyword arguments
def my_function(child3, child2, child1):
print("The youngest child is", child1)
my_function(child1="Emil", child2="Tobias", child3="Linus")
# Passing a dictionary to a function
students = {
'name': 'Ali',
'age': 21,
}
def passingDictionaryTOfunction(anyDictionary):
for key in anyDictionary:
print(key , anyDictionary[key])
print(passingDictionaryTOfunction(students))
# def passing_dict(info):
# for key in info:
# print(key, ':', info[key])
# passing_dict(students)
# Basic function returning square
def square(number):
return number ** 2
print(square(9))
# Function for multiplication
def multiply(q, w):
return q * w
print(multiply(8, 2))
print(multiply('a', 2))
print(multiply(2, 'a'))
# Function to calculate area and circumference of a circle
import math
def circle(radius):
area = math.pi * (radius ** 2)
circumference = 2 * math.pi * radius
return area, circumference
a, c = circle(3)
print('Area:', round(a, 2), 'Circumference:', round(c, 2))
# Function with default greeting
def default_greeting(greet='Good morning'):
print('Hey Sir!', greet)
default_greeting()
# Lambda function (anonymous function)
cube = lambda x: x ** 3 #these function have not name
print(cube(2))
# Sum using *args , *args allows you to pass any number of values to a function. Inside the function, args is a tuple of all the passed values.
# When you don't know how many arguments will be passed.
def sum_all(*args): #*with anyPerameter mean single value and it will be in tuple mean if the single value valiues then it will be in tuple
return sum(args)
print(sum_all(2, 4, 5, 2))
# Function with keyword arguments (**kwargs) , **kwargs lets you pass any number of keyword arguments (key-value pairs).
# When you want to accept named arguments without knowing what they are in advance.
def print_kwargs(**kwargs): # **asAnyparameter use to show dictionaly it means it will have key values . it only take argument as a key valued
for key, value in kwargs.items():
print(f"{key}: {value}")
print_kwargs(name='Ali', age=21, power='Lazer')
# Generator function using yield
def even_generator(limit):
for i in range(2, limit + 1, 2):
yield i
for num in even_generator(10):
print(num)
# Recursive function to calculate factorial
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(4))