Python Basics - Notes & Examples
1. Introduction to Python
- Python is a high-level, interpreted programming language known for its readability
and simplicity.
- Supports multiple programming paradigms: procedural, object-oriented, and
functional.
2. Variables & Data Types
- Variables store data. No need to declare type explicitly.
- Common data types: int, float, str, bool, list, tuple, dict, set.
3. Conditional Statements
- if, elif, else are used to control program flow.
- Example:
if x > 0:
print('Positive')
else:
print('Non-positive')
4. Loops
- for loop iterates over sequences, while loop iterates while condition is True.
- Example:
for i in range(5):
print(i**2)
5. Functions
- Functions are defined using 'def'. Can return values using 'return'.
- Example:
def add(a, b):
return a + b
print(add(2,3))
6. Lists & Dictionaries
- Lists: ordered, mutable collection. Example: nums = [1,2,3]
- Dictionaries: key-value pairs, unordered. Example: data = {'name':'Keshav','age':18}
7. File Handling
- Open files using open(filename, mode). Modes: 'r','w','a','rb','wb'.
- Example:
with open('file.txt','r') as f:
content = f.read()
8. Modules & Libraries
- Python has a rich standard library. Example modules: math, random, datetime, os
- Import using: import module_name