0% found this document useful (0 votes)
14 views13 pages

Sunum 2222

Uploaded by

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

Sunum 2222

Uploaded by

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

PYTHON BASICS

WEEK 2
FUNCTIONS
o Defining and calling functions

# function with two arguments


def add_numbers(num1, num2):
sum = num1 + num2
print("Sum: ", sum)

# function call with two values


add_numbers(5, 4)
o Default and keyword arguments
def add_numbers( a = 7, b = 8):
sum = a + b
def display_info(first_name, last_name):
print('Sum:', sum)
print('First Name:', first_name)
# function call with two arguments
print('Last Name:', last_name)
add_numbers(2, 3)

# function call with one argument


add_numbers(a = 2)
display_info(last_name = 'Cartman', first_name = 'Eric')
# function call with no arguments
add_numbers() First Name: Eric
Sum: 5
Sum: 10
Last Name: Cartman
Sum: 15
SCOPE
*Defines the area within which a variable or function is accessible

LOCAL VARIABLE GLOBAL VARIABLE


# declare global variable
def greet(): message = 'Hello'

def greet():
# local variable # declare local variable
message = 'Hello' print('Local', message)

greet()
print('Local', message) print('Global', message)

Local Hello
greet()
Global Hello

# try to access message variable


# outside greet() function
print(message)

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

Printing to the Screen: print writes the output to the screen.


Not Returning a Value: Since there is no return, None is
returned as a result of the function call
FILE HANDLING
o Reading from and writing to files
APPEND:
READ:

WRITE:
If the file exists, it deletes the content and
replaces it with a new one.

If the file does not exist, it creates a new file.


ERROR HANDLING
Introduction to exceptions (try, except, finally)

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).

o DICTIONARIES(KEY- VALUE PAIRS)


ADVANCED DATA STRUCTURES
o DICTIONARIES(KEY- VALUE PAIRS)
ADVANCED DATA STRUCTURES
TUPLES AND SETS
*Both are used to store multiple items (data) together.
ADVANCED DATA STRUCTURES
SETS
SETS are:
• Unordered - They don't maintain the order of elements.
• Immutable - Items cannot be changed after creation.
• Don't Allow duplicates - They cannot contain duplicate values.

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

You might also like