Basic Python - One Night Exam Revision Notes
1. Introduction to Python
- High-level, interpreted, object-oriented programming language.
- Dynamically typed and easy to learn.
2. Basic Syntax
- Case-sensitive, indentation is crucial.
- Comments: # for single line, ''' ''' or for multi-line.
3. Variables and Data Types
- No need to declare type explicitly.
- Types: int, float, str, bool, list, tuple, dict, set.
4. Operators
- Arithmetic: +, -, *, /, %, //, **
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
5. Control Structures
- if, elif, else
- while loop
- for loop (with range or iterable)
6. Functions
- Defined with def keyword.
```python
def greet(name):
return "Hello " + name
```
7. Data Structures
- List: Mutable ordered collection.
- Tuple: Immutable ordered collection.
- Set: Unordered unique elements.
- Dictionary: Key-value pairs.
8. String Operations
- Concatenation, slicing, methods (upper(), lower(), split(), join(), etc.)
9. Exception Handling
- try, except, finally
```python
try:
x = 10 / 0
except ZeroDivisionError:
print("Can't divide by zero.")
```
10. File Handling
```python
with open("file.txt", "r") as f:
content = f.read()
```
11. Modules and Libraries
- Import standard or third-party modules using import.
- Popular libraries: math, random, os, sys
12. Object-Oriented Programming
- Class, object, __init__, self, methods
```python
class Person:
def __init__(self, name):
self.name = name
```
13. List Comprehension
- Short syntax for creating lists.
```python
squares = [x**2 for x in range(10)]
```
14. Useful Functions
- len(), type(), range(), input(), print(), int(), str()
15. Virtual Environments (Advanced)
- Use venv to manage dependencies separately for projects.