Sunum 2222
Sunum 2222
WEEK 2
FUNCTIONS
o Defining and calling functions
def greet():
# local variable # declare local variable
message = 'Hello' print('Local', message)
greet()
print('Local', message) print('Global', message)
Local Hello
greet()
Global Hello
Local Hello
NameError: name 'message' is not defined
RETURN VALUES
In Python, a function can return a value after performing its function. This value is specified with the return keyword.
IF WE DON’T USE RETURN RETURN VALUES EXAMPLE
WRITE:
If the file exists, it deletes the content and
replaces it with a new one.
try: try:
numerator = 10 numerator = 10
denominator = 0 denominator = 0
result = numerator/denominator
result = numerator/denominator
print(result)
print(result) except:
except: print("Error: Denominator cannot be 0.")
print("Error: Denominator cannot be 0.")
finally:
print("This is finally block.")
# Output: Error: Denominator cannot be 0.
ADVANCED DATA STRUCTURES
• A Python dictionary is a collection of items, similar to lists and tuples. However, unlike lists and tuples, each item in a dictionary is a key-
value pair (consisting of a key and a value).
TUPLES are:
• Ordered - They maintain the order of elements.
• Immutable - They cannot be changed after creation.
• Allow duplicates - They can contain duplicate values.
TUPLES
ACTIVITIES