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

Day 3 - Python Lect 2

Machine Learning Course Ai(102)

Uploaded by

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

Day 3 - Python Lect 2

Machine Learning Course Ai(102)

Uploaded by

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

Python

Programming Language
AI 102 COURSE
D AY 3
PYTHON LECT # 2

1
Agenda
 Conditional Statement
 Relational Operators
 Indentation
 Logical Operators
 Loops
 Nested Loops

2
Agenda…
 break
 continue
 pass

3
Relational Operators
Operator Symbol Meaning Example

Equal to == Checks if two values are equal x == y

Not equal to != Checks if two values are not equal x != y

Greater than > Checks if the left value is greater than the right x>y
value

Less than < Checks if the left value is less than the right value x<y

Checks if the left value is greater than or equal to


Greater than or equal to >= the right value x >= y

Checks if the left value is less than or equal to the


Less than or equal to <= right value x <= y

4
Indentation
Indentation in Python refers to the use of whitespace (spaces or tabs) at the
beginning of a line to define the structure of the code.
it indicates the grouping of statements. Unlike many other programming
languages that use braces {} or keywords to delimit blocks of code, Python
uses indentation levels.
Indentation Levels: Each indentation level typically consists of four spaces.
Consistency: Indentation must be consistent within a block of code. Mixing
tabs and spaces in the same block is not allowed and will result in an
‘IndentationError’.

5
Conditional Statement
 Simple if
 if else
 if elif else
 Nested if else

6
If Statement
 if statement is used to test a specific condition. If the condition
evaluates to True, the code block under the if statement is executed.

a = 10
if a > 5:
print("a is greater than 5")

7
If else Statement
 In if else statement if block will be executed if condition is True
and else block will be executed if condition is False.
a = 1
if a == 1:
print("a is 1")
else:
print("a is not 1")

8
If elif else Statement
 In elif (short for else if) statement is used to check multiple expressions
for True and execute a block of code as soon as one of the conditions is
True. It is used when you have more than two conditions to check.
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
9
Nested if else
Nested if-else statements in Python are a way to place one if-else
statement inside another if-else statement. This allows you to check
multiple conditions and execute different blocks of code based on
those conditions.
if condition1:
if condition2:
# code block 1
else:
# code block 2
else:
# code block 3
10
Nested if else Example
x = 15
if x > 10:
print("x is greater than 10")
if x > 20:
print("x is also greater than 20")
else:
print("x is 10 to 20")
else:
print("x is 10 or less")

11
Logical Operators
 and , return true if both operands are true
 or , return True if any one of operands are true
 not , it inverts operands value
x = 5
y = 10
if x > 0 and y > 5:
print("Both conditions are True")

12
Loops
 Loop allow to execute block of code repeatedly
while loop
i = 1
while i < 6: # condition
print(i)
i += 1

13
Loops
 for loop
for i in range(1, 6):
print(i) # output 1 2 3 4 5

for i in range(1, 6, 2):


print(i) # output 1 3 5

14
Nested loop
 loop with in another loop is called nested loop
i = 1
while i <= 2: # Outer loop
print(i, "Outer loop is executed")
j = 1
while j <= 2: # Inner loop
print(j, "Inner loop is executed")
j += 1
i += 1
print(“End of code”)
15
break statement
 break statement is used to exit the loop prematurely, i.e., before the loop
finishes its natural iteration. When a break statement is encountered, the loop
is terminated, and the program continues executing the code after the loop.
i = 0
while i < 5:
if i == 3:
break
print(i)
i += 1
print("Loop ended") # Output 0 1 2 Loop ended

16
continue statement
 The continue statement is used to skip the current iteration of the loop
and move on to the next one
i = 0
while i <= 10:
if i%3 == 0:
i += 1
continue
print(i)
i += 1
print("Loop ended") # output 1 2 4 5 7 8 10 Loop ended
17
pass statement
 The pass statement in Python is a null operation. In other words,
pass does nothing when executed. It is often used in situations
where you need to define a block of code (such as a function or a
loop) but you don't want to execute any code yet.
for i in range(5):
pass # no output but loop will run 5 times

18
Q&A

19

You might also like