02-Introduction To Python
02-Introduction To Python
Instructor:
Musawir Ghani
Research Assistant,
AI in Healthcare, National Center of AI UET Peshawar
Email: [email protected]
Introduction to Python
Basics
➔ IDE (jupyter)
➔ Numbers
➔ Strings
➔ Lists
➔ Dictionaries
➔ Tuples
➔ Sets
Introduction to Python
Data Types
Introduction to Python
Immutable Mutable
Immutable cannot be changed after it is created Mutable can be changed after it is created
Introduction to Python
Numbers
x=5 # int
x = 5.0 # float
The best thing about python is that you don't have to define the datatype of a variable
Introduction to Python
Strings
➔ Unlike Strings, lists are mutable which means that lists can be changed after creation, otherwise
their behavior is same as a tuple
➔ Declaration symbol [ ]
my_list = [30, 45.6 , 'hello'] # This list consists of an integer(30), float(45.6), and string(“hello”)
My_list[0] = ‘hello’
Please notice that I have changed the type and value of an element in a list if we tried to do something like this in string,
this will be considered as error
Introduction to Python
Tuples
➔ A tuple is an immutable python object which means it cannot be changed once it's created.
➔ Tuples are like an array but each element in a tuple can be of different types
➔ Declaration symbol ( )
my_tuple = (30, 45.6 , 'hello') #This tuple consists of an integer, float, and string
Introduction to Python
Sets
My_dict = { 'id': 1, 'name': 'John', 'age': 32 } # the id, name and john are keys
➔ and
➔ or
➔ Not (!)
Introduction to Python
and
If weather = “sunny”
then
“I will play outside”
Introduction to Python
elif
If weather = “sunny”
then
“I will play outside”
If day = “sunday”
then
“I will go to the amusement park”
else
“I will stay at home”
Introduction to Python
Nested If
if weather = “sunny”
then
“Call friends”
if friends = “available”
then
“I will play soccer”
Introduction to Python
Loops
➔ For loops
➔ While loop
Introduction to Python
For loops
➔ For loop is used for iterating over a sequence that is either a list, a tuple,
a dictionary, a set, or a string.
➔ Syntax:
for index in range(len(sequence)): or for iterator in sequence:
for i in my_list: # i becomes the element of list itself and will iterate through all
print(i) # elements of sequence(my_list)
Introduction to Python
While loop
➔ While loop is a conditional loop which means it will run until the given condition is fulfilled
value = 0
while(value<5):
print(value)
value+=1
Introduction to Python
Functions
➔ A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a
function.
➔ SRP(Single Responsibility Principle)
Type:
➔ Simple functions
➔ Boolean functions
➔ Value returning functions
Introduction to Python
Simple functions
def my_first_function:
print(“This is my first function”)
def my_first_function(data):
print(data)
def my_boolean_function:
return true
def is_less(x,y):
return x < y ? true : false
x=2
y=6
if my_boolean_function(x , y):
print(“x is less than y”)
Introduction to Python
Value returning functions
➔ A value returning function is a block of code which returns one or more values
def my_function:
return ‘I am a value returning function’
def get_minimum(x,y):
return x < y ? x : y
x=2
y=6
answer = get_minimum(x , y)
Introduction to Python
Functions
➔ Project (Calculator)