- **File Extension:** Python files are saved with the `.py` extension.
- **Versatility:** Python is versatile and used in many different use cases (e.g., web development, data science, automation, etc.).
- **Reuse Code:** You can use already written code from others.
- **Comments:**
- Single-line comment: `# Comment`
- Multi-line comment: `""" Comment """` or `''' Comment '''`
#### **Output and Variables:**
- **Print Statement:** `print("")` is used to display output on the console.
- **Variables:**
- **Case Sensitivity:** Variables are case-sensitive.
- **Assignment:** Use `=` to assign values to variables.
- **Type Checking:** `type(variable)` to check the type of the variable.
#### **Data Types and Operations:**
- **String Construction:** Strings can be built with `'___'` or `"__"`.
- **Booleans:** Use `True` or `False` for boolean values (case-sensitive).
- **Variable Naming Conventions:**
- Snake Case: `total_spend`
- Camel Case: `TotalSpend`
- **Escape Characters:** - Use `\"` to add double quotes within a string.
- Example: `print("Hello \"World\"")`
- **Separator:** `sep=` to separate values in `print()` statements.
- **End Character:** `end=` to specify what to print at the end of the `print()` function.
#### **Complex Numbers:**
- **Complex Numbers:** Use `a = complex(8, 2)` to create a complex number.
- Output: `8+2i`
#### **Data Structures:**
- **List:** A collection of different data types.
- **Tuple:** Similar to lists but **immutable** (cannot be modified).
- **Dictionary (Dict):** A collection of key-value pairs.
#### **Mathematical Operations:**
- **Division (//):** Divides and discards the remainder (integer division).
- **Exponentiation (**): Used for powers.
- **Modulus (%):** Returns the remainder of division.
#### **Typecasting:**
- **Typecasting:** Converting one data type into another.
- **Explicit Typecasting:** The user specifies the conversion.
- **Implicit Typecasting:** Python automatically converts data types when needed.
- **Input Function:** The `input()` function takes input as a string. To perform mathematical operations, you need to convert the input to `int` or `float` using typecasting.
#### **Strings:**
- **Multiline Strings:** Use `'''` or `"""` for multiline strings.
- **Indexing Strings:** You can access individual characters by their index.
- Example: `n = "harry"; print(n[0])` outputs `h`.
- **For Loops in Strings:**
```python
for character in n:
print(character)
```
Output:
```
h
a
r
r
y
```
- **String Length:** `len(string)` returns the length of the string.
- **String Slicing:** `n[0:n-1]` slices the string (excluding the last index).
#### **String Methods:**
- **Case Conversion:**
- `print(nm.upper())` converts to uppercase.
- `print(nm.lower())` converts to lowercase.
- `print(nm.title())` capitalizes the first letter of each word.
- **Trimming:** - `print(nm.rstrip("!"))` removes the trailing "!" from the string.
- **Replacing:** - `print(a.replace("Harry", "John"))` replaces occurrences of "Harry" with "John".
- **Splitting:** - `print(a.split(" "))` splits the string by spaces into a list.
- **Capitalization:**
- `print(blogheading.capitalize())` capitalizes the first letter of the string.
- **Centering:**
- `print(str1.center(50))` centers the string with 50 spaces around it.
- **Counting Occurrences:**
- `print(a.count("harry"))` counts how many times "harry" appears in the string.
- **Finding Substring:**
- `print(a.find("harry"))` returns the index of the first occurrence of "harry".
- If not found, it returns `-1`.
- **Index Method:**
- `print(a.index("harry"))` is similar to `find()`, but raises an error if "harry" isn't found.
- **Alphanumeric Check:** - `a.isalnum()` checks if the string is alphanumeric (A-Z, a-z, 0-9).
- **Alphabetic Check:**
- `a.isalpha()` returns `True` if the string contains only alphabetic characters.
- **Lowercase Check:**
- `a.islower()` checks if all characters are lowercase.
- **Printable Check:**
- `a.isprintable()` checks if all characters in the string are printable.
- `\n`, `\a`, `\t` are non-printable characters.
- **Whitespace Check:**
- `a.isspace()` returns `True` if the string only contains spaces.
- **Title Case Check:**
- `a.istitle()` returns `True` if the first letter of each word is capitalized.
- **Uppercase Check:**
- `a.isupper()` returns `True` if the string is all uppercase.
#### **Conditionals:**
- **If-Else Statement:**
```python
if condition:
# statement
else:
# statement
```
- **Indentation:** Proper indentation is required to define the blocks of code for `if` and `else`.
- **Elif:** Used to check multiple conditions.
```python
if condition1:
# statement
elif condition2:
# statement
else:
# statement
```
- **Nested If-Else:**
```python
if condition1:
if condition2:
# statement
else:
# statement
else:
# statement