|
| 1 | +# 1. All operators categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise |
| 2 | + |
| 3 | +#Arithmetic Operators: +, -, *, /, %, **, // |
| 4 | +Addition = 10 + 5 |
| 5 | +Subtraction = 10 - 5 |
| 6 | +Multiplication = 10 * 5 |
| 7 | +Division = 10 / 5 #(always float ) |
| 8 | +Floor_Division = 10 // 3 #(rounds down to nearest whole number) |
| 9 | +Modulus = 10 % 3 #(remainder of division) |
| 10 | +Exponentiation = 10 ** 2 |
| 11 | + |
| 12 | +#Comparison Operators: ==, !=, >, <, >=, <= |
| 13 | +Equal = (10 == 5) #False(checks if values are equal) |
| 14 | +Not_Equal = (10 != 5) #True(checks if values are not equal) |
| 15 | +Greater_than = (10 > 5) #True |
| 16 | +Less_than = (10 < 5) #False |
| 17 | +Greater_than_or_equal_to = (10 >= 5) #True |
| 18 | +Less_than_or_equal_to = (10 <= 5) #False |
| 19 | + |
| 20 | +#Logical Operators: and, or, not |
| 21 | +Logical_AND = (10 > 5 and 5 < 3) #False(both conditions must be true) |
| 22 | +Logical_OR = (10 > 5 or 5 < 3) #True(at least one condition must be true) |
| 23 | +Logical_NOT = not(10 > 5) #False(inverts the boolean value) |
| 24 | + |
| 25 | +#Assignment Operators: =, +=, -=, *=, /=, %=, //=, **= |
| 26 | +a = 10 #Assigns value 10 to a |
| 27 | +a += 5 #Equivalent to a = a + 5 (now a is 15) |
| 28 | +a -= 3 #Equivalent to a = a - 3 (now a is 12) |
| 29 | +a *= 2 #Equivalent to a = a * 2 (now a is 24) |
| 30 | +a /= 4 #Equivalent to a = a / 4 (now a is 6.0) |
| 31 | +a %= 4 #Equivalent to a = a % 4 (now a is 2.0) |
| 32 | +a //= 2 #Equivalent to a = a // 2 (now a is 1.0) |
| 33 | +a **= 3 #Equivalent to a = a ** 3 (now a is 1.0) |
| 34 | + |
| 35 | +#Identity Operators: is, is not |
| 36 | +x = ["apple", "banana"] #True (same object in memory) |
| 37 | +y = ["apple", "banana"] #False (different objects in memory) |
| 38 | + |
| 39 | +#Bitwise Operators: &, |, ^, ~, <<, >> |
| 40 | +Bitwise_AND = 5 & 3 #1 (binary 0101 & 0011 = 0001) |
| 41 | +Bitwise_OR = 5 | 3 #7 (binary 0101 | 0011 = 0111) |
| 42 | +Bitwise_XOR = 5 ^ 3 #6 (binary 0101 ^ 0011 = 0110) |
| 43 | +Bitwise_NOT = ~5 #-6 (inverts bits, ~0101 = 1010 which is -6 in two's complement) |
| 44 | +Left_Shift = 5 << 1 #10 (binary 0101 << 1 = 1010) |
| 45 | +Right_Shift = 5 >> 1 #2 (binary 0101 >> 1 = 0010) |
| 46 | + |
| 47 | +#Membership Operators: in, not in |
| 48 | +fruits = ["apple", "banana", "cherry"] #True (checks if "banana" is in the list) |
| 49 | +has_mango = "mango" not in fruits #True (checks if "mango" is not in the list) |
| 50 | + |
| 51 | +# 2. Control structures: if, elif, else, for, while, break, continue, pass |
| 52 | +#Sequential structure (default mode, executes statements one after another) |
| 53 | +print("Start") |
| 54 | +print("Middle") |
| 55 | +print("End") |
| 56 | + |
| 57 | +#A. Conditional Statements (decision-making) |
| 58 | +age = 20 |
| 59 | +if age < 18: |
| 60 | + print("Minor") |
| 61 | +elif age == 18: |
| 62 | + print("Just became an adult") |
| 63 | +else: |
| 64 | + print("Adult") |
| 65 | + |
| 66 | +#nested if |
| 67 | +if age >= 18: |
| 68 | + if age < 65: |
| 69 | + print("Adult") |
| 70 | + else: |
| 71 | + print("Senior") |
| 72 | + |
| 73 | + |
| 74 | +#B. Iteration (loops) |
| 75 | +#For loop (iterates over a sequence) |
| 76 | +fruits = ["apple", "banana", "cherry"] |
| 77 | +for fruit in fruits: |
| 78 | + print(fruit) |
| 79 | + |
| 80 | +#While loop (repeats as long as a condition is true) |
| 81 | +count = 0 |
| 82 | +while count < 5: |
| 83 | + print(count) |
| 84 | + count += 1 #Increment count to avoid infinite loop |
| 85 | + |
| 86 | +#Nested loops (loops inside loops) (Used for multi-dimensional logic) |
| 87 | +for i in range(3): #Outer loop |
| 88 | + for j in range(2): #Inner loop |
| 89 | + print(f"i: {i}, j: {j}") |
| 90 | + |
| 91 | +#C. Jump statements (alter the flow of loops) |
| 92 | +#Continue statement (skips the current iteration) |
| 93 | +for i in range(10): |
| 94 | + if i % 2 == 0: |
| 95 | + continue #Skip even numbers |
| 96 | + print(i) #Prints only odd numbers |
| 97 | + |
| 98 | +#Break statement (exits the loop) |
| 99 | +for i in range(10): |
| 100 | + if i == 5: |
| 101 | + break #Exit loop when i is 5 |
| 102 | + print(i) |
| 103 | + |
| 104 | +#Pass statement (does nothing, used as a placeholder) |
| 105 | +for i in range(5): |
| 106 | + pass #No operation, can be used to create empty loops or functions |
| 107 | +print("Loop completed") |
| 108 | + |
| 109 | +#D. Exception Handling (try, except, finally) |
| 110 | +try: |
| 111 | + result = 10 / 0 #This will raise a ZeroDivisionError |
| 112 | +except ZeroDivisionError: |
| 113 | + print("Error: Division by zero is not allowed.") |
| 114 | +finally: |
| 115 | + print("Execution completed.") |
| 116 | +#The finally block always executes, regardless of whether an exception occurred or not. |
| 117 | + |
| 118 | +#3. Print Examples |
| 119 | + |
| 120 | +#4. Optional exercise |
| 121 | + |
| 122 | +for x in range(10, 56, 1): |
| 123 | + if x % 2 != 0 or x == 16 or x % 3 == 0: |
| 124 | + continue |
| 125 | + print(x) |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments