Python Basics
1. Introduction to Python:
- Python is a high-level, interpreted programming language.
- Syntax is clean and easy to read.
2. Variables and Data Types:
- Variable assignment: x = 5
- Data types: int, float, str, bool, list, tuple, dict, set
3. Operators:
- Arithmetic: +, -, *, /, %, **
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
4. Control Flow:
- if, elif, else statements
- Example:
if x > 0:
print("Positive")
5. Loops:
- for loops: for i in range(5): print(i)
- while loops: while x < 10: x += 1
6. Functions:
- def greet(name):
return "Hello " + name
7. Data Structures:
- List: [1, 2, 3]
- Tuple: (1, 2, 3)
- Dictionary: {"key": "value"}
- Set: {1, 2, 3}
8. String Operations:
- Concatenation: "Hello" + "World"
- Methods: .upper(), .lower(), .strip(), .split()
9. Input and Output:
- input(): name = input("Enter your name: ")
- print(): print("Hello", name)
10. File Handling:
- Reading: open('file.txt', 'r')
- Writing: open('file.txt', 'w')
11. Exception Handling:
- try:
# code
except Exception as e:
print(e)
12. Modules and Packages:
- import math
- from datetime import datetime
13. Classes and Objects:
- class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
14. Useful Built-in Functions:
- len(), type(), range(), enumerate(), zip(), map(), filter(), lambda
15. Comments:
- Single-line: # This is a comment
- Multi-line: '''This is a multi-line comment'''