|
| 1 | +# if |
| 2 | +# elif |
| 3 | +# else |
| 4 | + |
| 5 | +#Example 1: Only if statement |
| 6 | +x = 15 |
| 7 | + |
| 8 | +if x > 10: # If condition: Check if x is greater than 10 |
| 9 | + print("x is greater than 10") # Output: "x is greater than 10" |
| 10 | +else: |
| 11 | + print("x is less than or equal to 10") # Output: This line won't be executed in this case |
| 12 | + |
| 13 | +#Example 2: if and elif statements |
| 14 | +y = 7 |
| 15 | + |
| 16 | +if y > 10: # If condition: Check if y is greater than 10 |
| 17 | + print("y is greater than 10") # Output: This line won't be executed in this case |
| 18 | +elif y == 10: # Elif condition: Check if y is equal to 10 |
| 19 | + print("y is equal to 10") # Output: This line won't be executed in this case |
| 20 | +else: |
| 21 | + print("y is less than 10") # Output: "y is less than 10" |
| 22 | + |
| 23 | + |
| 24 | +#Example 3: Nested if and else statements |
| 25 | +z = 5 |
| 26 | + |
| 27 | +if z < 10: # If condition: Check if z is less than 10 |
| 28 | + print("z is less than 10") # Output: "z is less than 10" |
| 29 | + |
| 30 | + if z % 2 == 0: # Nested If condition: Check if z is an even number |
| 31 | + print("z is an even number") # Output: This line won't be executed in this case |
| 32 | + else: |
| 33 | + print("z is an odd number") # Output: "z is an odd number" |
| 34 | +else: |
| 35 | + print("z is 10 or greater") # Output: This line won't be executed in this case |
| 36 | + |
| 37 | + |
| 38 | +# Relational Operators |
| 39 | +x = 10 |
| 40 | +y = 5 |
| 41 | + |
| 42 | +if x > y: |
| 43 | + print("x is greater than y") # Output: "x is greater than y" |
| 44 | +elif x == y: |
| 45 | + print("x is equal to y") # Output: This line won't be executed in this case |
| 46 | +elif x != y: |
| 47 | + print("x is not equal to y") # Output: "x is not equal to y" |
| 48 | +elif x >= y: |
| 49 | + print("x is greater than or equal to y") # Output: This line won't be executed in this case |
| 50 | +elif x < y: |
| 51 | + print("x is less than y") # Output: This line won't be executed in this case |
| 52 | +else: |
| 53 | + print("x is not a integer") |
| 54 | + |
| 55 | +# Logical Operators |
| 56 | +p = True |
| 57 | +q = False |
| 58 | + |
| 59 | +if p and q: |
| 60 | + print("Both p and q are True") # Output: This line won't be executed in this case |
| 61 | +elif p or q: |
| 62 | + print("At least one of p or q is True") # Output: "At least one of p or q is True" |
| 63 | +elif not p: |
| 64 | + print("p is False") # Output: This line won't be executed in this case |
| 65 | +elif not q: |
| 66 | + print("q is False") # Output: "q is False" |
| 67 | +else: |
| 68 | + print("something is not right") |
| 69 | + |
| 70 | + |
| 71 | +# Using 'in' and 'is' operators |
| 72 | +a = [1, 2, 3] |
| 73 | +b = [1, 2, 3] |
| 74 | +c = a |
| 75 | + |
| 76 | +if 2 in a: |
| 77 | + print("2 is in list a") # Output: "2 is in list a" |
| 78 | +elif b is a: |
| 79 | + print("b is the same object as a") # Output: This line won't be executed in this case |
| 80 | +elif c is a: |
| 81 | + print("c is the same object as a") # Output: "c is the same object as a" |
| 82 | +else: |
| 83 | + print("None of the conditions are True") # Output: This line won't be executed in this case |
0 commit comments