Day 3 - Python Lect 2.pdf
Document Details
Uploaded by LuxurySocialRealism
Tags
Full Transcript
Python Programming Language A I 1 0 2 CO U RS E DAY 3 PY T H O N L EC T # 2 1 Agenda Ø Conditional Statement Ø Relational Operators Ø Indentation Ø Logical Operators Ø Loops Ø Nested Loops 2 Agenda… Ø break Ø continue Ø pass 3 Rel...
Python Programming Language A I 1 0 2 CO U RS E DAY 3 PY T H O N L EC T # 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 Checks if the left value is greater than the right Greater than > x>y value Less than < Checks if the left value is less than the right value x= x >= y the right value Checks if the left value is less than or equal to the Less than or equal to 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