-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_functions.py
More file actions
181 lines (143 loc) · 3.88 KB
/
07_functions.py
File metadata and controls
181 lines (143 loc) · 3.88 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Functions - reusable blocks of code
# Basic function
def greet():
print("Hello!")
print("Welcome to Python")
greet() # calling the function
# Function with parameters
def greet_person(name):
print(f"Hello, {name}!")
greet_person("Peter")
greet_person("Sarah")
# Multiple parameters
def add_numbers(a, b):
result = a + b
print(f"{a} + {b} = {result}")
add_numbers(5, 3)
# Return values
def multiply(x, y):
return x * y
result = multiply(4, 5)
print(result)
# Multiple return values
def calculate(a, b):
sum_result = a + b
diff_result = a - b
prod_result = a * b
return sum_result, diff_result, prod_result
s, d, p = calculate(10, 5)
print(f"Sum: {s}, Diff: {d}, Product: {p}")
# Default parameters
def power(base, exponent=2):
return base ** exponent
print(power(5)) # uses default exponent=2
print(power(5, 3)) # uses exponent=3
# Keyword arguments
def create_profile(name, age, city):
print(f"Name: {name}")
print(f"Age: {age}")
print(f"City: {city}")
create_profile(name="John", city="NYC", age=30)
# Variable number of arguments
def sum_all(*numbers):
total = 0
for num in numbers:
total += num
return total
print(sum_all(1, 2, 3))
print(sum_all(5, 10, 15, 20))
# Keyword variable arguments
def print_info(**info):
for key, value in info.items():
print(f"{key}: {value}")
print_info(name="Peter", age=30, city="NYC")
# Function with docstring (documentation)
def calculate_area(length, width):
"""
Calculate the area of a rectangle.
Parameters:
length (float): Length of rectangle
width (float): Width of rectangle
Returns:
float: Area of rectangle
"""
return length * width
print(calculate_area(5, 3))
# Practical example - calculator
def calculator():
print("\n--- Calculator ---")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Choose operation: ")
num1 = float(input("First number: "))
num2 = float(input("Second number: "))
if choice == "1":
result = num1 + num2
print(f"Result: {result}")
elif choice == "2":
result = num1 - num2
print(f"Result: {result}")
elif choice == "3":
result = num1 * num2
print(f"Result: {result}")
elif choice == "4":
if num2 != 0:
result = num1 / num2
print(f"Result: {result}")
else:
print("Cannot divide by zero")
# Temperature converter
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
temp_c = 25
temp_f = celsius_to_fahrenheit(temp_c)
print(f"{temp_c}°C = {temp_f}°F")
# Check if number is even
def is_even(number):
return number % 2 == 0
print(is_even(4)) # True
print(is_even(7)) # False
# Find maximum in list
def find_max(numbers):
if not numbers:
return None
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
nums = [5, 2, 9, 1, 7]
print(f"Maximum: {find_max(nums)}")
# Count vowels in string
def count_vowels(text):
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
return count
text = "Hello World"
print(f"Vowels in '{text}': {count_vowels(text)}")
# Password validator
def is_valid_password(password):
if len(password) < 8:
return False
has_digit = False
has_upper = False
for char in password:
if char.isdigit():
has_digit = True
if char.isupper():
has_upper = True
return has_digit and has_upper
print(is_valid_password("weak")) # False
print(is_valid_password("Strong123")) # True
# Lambda functions (short anonymous functions)
square = lambda x: x ** 2
print(square(5))
add = lambda a, b: a + b
print(add(3, 7))