Cheat Sheet: Python Programming Fundamentals
Python Programming Fundamentals Cheat Sheet
| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| AND | Returns `True` if both statement1 and statement2 are `True`. Otherwise, returns `False`. | Syntax:
Example:
|
| Class Definition | Defines a blueprint for creating objects and defining their attributes and behaviors. | Syntax:
Example:
|
| Define Function | A `function` is a reusable block of code that performs a specific task or set of tasks when called. | Syntax:
Example:
|
| Equal(==) | Checks if two values are equal. | Syntax:
Example 1:
returns True Example 2:
returns False |
| For Loop | A `for` loop repeatedly executes a block of code for a specified number of iterations or over a sequence of elements (list, range, string, etc.). | Syntax:
Example 1:
Example 2:
|
| Function Call | A function call is the act of executing the code within the function using the provided arguments. | Syntax:
Example:
|
| Greater Than or Equal To(>=) | Checks if the value of variable1 is greater than or equal to variable2. | Syntax:
Example 1:
returns True Example 2:
returns True |
| Greater Than(>) | Checks if the value of variable1 is greater than variable2. | Syntax:
Example 1: 9 > 6 returns True Example 2:
returns False |
| If Statement | Executes code block `if` the condition is `True`. | Syntax:
Example:
|
| If-Elif-Else | Executes the first code block if condition1 is `True`, otherwise checks condition2, and so on. If no condition is `True`, the else block is executed. | Syntax:
Example:
|
| If-Else Statement | Executes the first code block if the condition is `True`, otherwise the second block. | Syntax:
Example:
|
| Less Than or Equal To(<=) | Checks if the value of variable1 is less than or equal to variable2. | Syntax:
Example 1:
returns True Example 2:
returns True |
| Less Than(<) | Checks if the value of variable1 is less than variable2. | Syntax:
Example 1:
returns True Example 2:
returns True |
| Loop Controls | `break` exits the loop prematurely. `continue` skips the rest of the current iteration and moves to the next iteration. | Syntax:
Example 1:
Example 2:
|
| NOT | Returns `True` if variable is `False`, and vice versa. | Syntax:
Example:
returns True if the variable is False (i.e., unlocked). |
| Not Equal(!=) | Checks if two values are not equal. | Syntax:
Example:
returns True Example 2:
returns False |
| Object Creation | Creates an instance of a class (object) using the class constructor. | Syntax:
Example:
|
| OR | Returns `True` if either statement1 or statement2 (or both) are `True`. Otherwise, returns `False`. | Syntax:
Example:
returns True |
| range() | Generates a sequence of numbers within a specified range. | Syntax:
Example:
|
| Return Statement | `Return` is a keyword used to send a value back from a function to its caller. | Syntax:
Example:
|
| Try-Except Block | Tries to execute the code in the try block. If an exception of the specified type occurs, the code in the except block is executed. | Syntax:
Example:
|
| Try-Except with Else Block | Code in the `else` block is executed if no exception occurs in the try block. | Syntax:
Example:
|
| Try-Except with Finally Block | Code in the `finally` block always executes, regardless of whether an exception occurred. | Syntax:
Example:
|
| While Loop | A `while` loop repeatedly executes a block of code as long as a specified condition remains `True`. | Syntax:
Example:
|
Comments
Post a Comment