Modulo 3 Python
Modulo 3 Python
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
Returns `True` if both statement1 and statement2 are `True`. Otherwise, 5. 5
AND 6. 6
returns `False`.
7. 7
8. 8
9. 9
1. marks = 90
2. attendance_percentage = 87
3.
4. if marks >= 80 and attendance_percentage >= 85:
5. print("qualify for honors")
6. else:
7. print("Not qualified for honors")
8.
9. # Output = qualify for honors
Copied!
Syntax:
1. 1
Copied!
Example:
Defines a blueprint for creating objects and defining their attributes and
Class Definition 1. 1
behaviors.
2. 2
3. 3
4. 4
1. class Person:
2. def __init__(self, name, age):
3. [Link] = name
4. [Link] = age
Copied!
Syntax:
1. 1
Copied!
A `function` is a reusable block of code that performs a specific task or
Define Function
set of tasks when called. Example:
1. 1
Copied!
Equal(==) Checks if two values are equal. Syntax:
1. 1
1. variable1 == variable2
Copied!
Example 1:
1. 1
1. 5 == 5
Copied!
returns True
Example 2:
1. 1
about:blank 1/7
5/11/24, 17:32 about:blank
1. age = 25 age == 30
Copied!
returns False
Syntax:
1. 1
Copied!
Example 1:
1. 1
2. 2
Example 2:
1. 1
2. 2
3. 3
Copied!
Syntax:
1. 1
1. function_name(arguments)
Copied!
A function call is the act of executing the code within the function
Function Call
using the provided arguments. Example:
1. 1
1. greet("Alice")
Copied!
Syntax:
1. 1
Copied!
Example 1:
1. 1
Copied!
Greater Than or Equal
Checks if the value of variable1 is greater than or equal to variable2.
To(>=) returns True
Example 2:
1. 1
2. 2
3. 3
1. quantity = 105
2. minimum = 100
3. quantity >= minimum
Copied!
returns True
Greater Than(>) Checks if the value of variable1 is greater than variable2. Syntax:
1. 1
Copied!
Example 1: 9 > 6
returns True
about:blank 2/7
5/11/24, 17:32 about:blank
Example 2:
1. 1
2. 2
3. 3
1. age = 20
2. max_age = 25
3. age > max_age
Copied!
returns False
Syntax:
1. 1
Copied!
Copied!
Syntax:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
1. if condition1:
2. # Code if condition1 is True
3.
4. elif condition2:
5. # Code if condition2 is True
6.
7. else:
8. # Code if no condition is True
Copied!
Executes the first code block if condition1 is `True`, otherwise checks
If-Elif-Else condition2, and so on. If no condition is `True`, the else block is Example:
executed.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
Copied!
If-Else Statement Executes the first code block if the condition is `True`, otherwise the Syntax:
second block.
1. 1
2. 2
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
about:blank 3/7
5/11/24, 17:32 about:blank
3. else:
4. print("You're not an adult yet.")
Copied!
Syntax:
1. 1
Copied!
Example 1:
1. 1
Copied!
Less Than or Equal
Checks if the value of variable1 is less than or equal to variable2.
To(<=) returns True
Example 2:
1. 1
2. 2
3. 3
1. size = 38
2. max_size = 40
3. size <= max_size
Copied!
returns True
Syntax:
1. 1
Copied!
Example 1:
1. 1
1. 4 < 6
Copied!
Less Than(<) Checks if the value of variable1 is less than variable2.
returns True
Example 2:
1. 1
2. 2
3. 3
1. score = 60
2. passing_score = 65
3. score < passing_score
Copied!
returns True
Loop Controls `break` exits the loop prematurely. `continue` skips the rest of the Syntax:
current iteration and moves to the next iteration.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
Copied!
Example 1:
1. 1
2. 2
3. 3
4. 4
about:blank 4/7
5/11/24, 17:32 about:blank
1. for num in range(1, 6):
2. if num == 3:
3. break
4. print(num)
Copied!
Example 2:
1. 1
2. 2
3. 3
4. 4
Copied!
Syntax:
1. 1
1. !variable
Copied!
1. 1
1. !isLocked
Copied!
1. variable1 != variable2
Copied!
Example:
1. 1
2. 2
3. 3
1. a = 10
2. b = 20
3. a != b
Not Equal(!=) Checks if two values are not equal.
Copied!
returns True
Example 2:
1. 1
2. 2
1. count=0
2. count != 0
Copied!
returns False
Syntax:
1. 1
1. object_name = ClassName(arguments)
Copied!
Object Creation Creates an instance of a class (object) using the class constructor.
Example:
1. 1
Copied!
OR Returns `True` if either statement1 or statement2 (or both) are `True`. Syntax:
Otherwise, returns `False`.
1. 1
1. statement1 || statement2
Copied!
about:blank 5/7
5/11/24, 17:32 about:blank
Example:
1. 1
2. 2
Copied!
returns True
Syntax:
1. 1
2. 2
3. 3
1. range(stop)
2. range(start, stop)
3. range(start, stop, step)
Copied!
range() Generates a sequence of numbers within a specified range.
Example:
1. 1
2. 2
3. 3
Copied!
Syntax:
1. 1
1. return value
Copied!
Copied!
Syntax:
1. 1
2. 2
Copied!
Tries to execute the code in the try block. If an exception of the Example:
Try-Except Block
specified type occurs, the code in the except block is executed.
1. 1
2. 2
3. 3
4. 4
1. try:
2. num = int(input("Enter a number: "))
3. except ValueError:
4. print("Invalid input. Please enter a valid number.")
Copied!
Try-Except with Else Code in the `else` block is executed if no exception occurs in the try Syntax:
Block block.
1. 1
2. 2
3. 3
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
about:blank 6/7
5/11/24, 17:32 about:blank
6. 6
1. try:
2. num = int(input("Enter a number: "))
3. except ValueError:
4. print("Invalid input. Please enter a valid number")
5. else:
6. print("You entered:", num)
Copied!
Syntax:
1. 1
2. 2
3. 3
Copied!
Example:
Try-Except with Code in the `finally` block always executes, regardless of whether an 1. 1
2. 2
Finally Block exception occurred.
3. 3
4. 4
5. 5
6. 6
7. 7
1. try:
2. file = open("[Link]", "r")
3. data = [Link]()
4. except FileNotFoundError:
5. print("File not found.")
6. finally:
7. [Link]()
Copied!
Syntax:
1. 1
Copied!
Copied!
about:blank 7/7