0% found this document useful (0 votes)
6 views

02-Introduction To Python

Uploaded by

waqarkhan03109
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

02-Introduction To Python

Uploaded by

waqarkhan03109
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction to Python

Instructor:
Musawir Ghani
Research Assistant,
AI in Healthcare, National Center of AI UET Peshawar
Email: [email protected]
Introduction to Python
Basics

➔ Importance of python for machine learning

➔ IDE (jupyter)

➔ Basic functionality + Hello word


Introduction to Python
Data types

➔ 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

➔ Python supports integers, floating point numbers

➔ Numbers are used for arithmetic operations, storing information etc

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

➔ String is a sequence of characters

➔ It is used to represent text instead of numbers

➔ String in python can be easily concatenated, sliced, etc

str = 'Hello World' # string declaration


print(str) # outputs Hello World
print(str + ' ' + str) # outputs Hello World Hello World
print(str[0:5]) # outputs Hello
Introduction to Python
Lists

➔ 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

➔ Set is an unordered collection of data types that is iterable and mutable.


➔ Sets in python are similar to sets in mathematics.
➔ The value in sets cannot be repeated and a set cannot be indexed.
➔ A set is normally used to verify if a specific element exists in a set or not.
➔ Declaration symbol { }

my_set = {1,2,3,4,5,5,6,6} # outputs {1,2,3,4,5,6}


my_set.add(7) # adds 7 to the set
my_set.remove(2) # removes 2 from set
my_set.union(other_set) # takes union with other_set
my_set.intersection(other_set) # takes intersection with other_set
Introduction to Python
Dictionaries

➔ A dictionary is a collection which is unordered, changeable and indexed.


➔ In Python, dictionaries are written with curly brackets, and they have keys and values.
➔ Dictionary holds key:value pair.
➔ Key value is provided in the dictionary to make it more optimized
➔ Declaration symbol {key:value}

My_dict = { 'id': 1, 'name': 'John', 'age': 32 } # the id, name and john are keys

# while 1, john and 32 are values


My_dict['id'] # outputs 1
My_dict['name'] # outputs john
My_dict['age'] # outputs 32
Introduction to Python
Logical Operators

➔ and

➔ or

➔ Not (!)
Introduction to Python
and

➔ (true) and (true) returns true

➔ (true) and (false) returns false

➔ (false) and (true) returns false

➔ (false) and (false) returns false


Introduction to Python
or

➔ (true) or (true) returns true

➔ (true) or (false) returns true

➔ (false) or (true) returns true

➔ (false) or (false) returns false


Introduction to Python
Not (!)

➔ !(true) returns false

➔ !(false) returns true

0, empty string or null is considered false


Any other number or string is considered true
Introduction to Python
Decision Making

➔ If, elif, else

➔ Nested decision making


Introduction to Python
if

If weather = “sunny”
then
“I will play outside”
Introduction to Python
elif

If weather = “sunny”
then
“I will play outside”

else if weather = “rainy”


then
“I will play video games”
Introduction to Python
else

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 range(len(my_list)): #range function return the range of elements in a sequence


print(my_list[i])

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

➔ A variable is normally used in the condition which is updated in every iteration

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

➔ A simple function is a block of code that performs a specific operation

def my_first_function:
print(“This is my first function”)

def my_first_function(data):
print(data)

data = “this is my first parameterized function”


my_first_function(data)
Introduction to Python
Boolean functions

➔ A function is a block of code which only returns true or false

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)

You might also like