Python Basic Notes
1. Introduction to Python:
- Python is a high-level, interpreted programming language.
- It is easy to learn and widely used in web development, data science, AI, and more.
2. Python Syntax Basics:
- Statements are written line by line.
- Indentation is important; it defines code blocks.
3. Variables:
- Used to store data values.
- Example: x = 5, name = 'John'
4. Data Types:
- int: Integer numbers (e.g., 5, -2)
- float: Decimal numbers (e.g., 3.14)
- str: Strings (e.g., 'Hello')
- bool: Boolean values (True or False)
5. Operators:
- Arithmetic: +, -, *, /, %, **, //
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
6. Conditional Statements:
if condition:
# code block
elif condition:
# code block
else:
# code block
7. Loops:
- for loop: for i in range(5):
print(i)
- while loop: while condition:
# code block
8. Functions:
def greet():
print('Hello!')
9. Lists:
- Example: fruits = ['apple', 'banana', 'cherry']
- Access: fruits[0] → 'apple'
10. Dictionaries:
- Example: person = {'name': 'John', 'age': 25}
- Access: person['name'] → 'John'
11. Comments:
- Single-line: # This is a comment
- Multi-line: ''' This is a multi-line comment '''
12. Input and Output:
- input(): Takes user input.
- print(): Displays output.
13. Importing Modules:
- Example: import math
- print([Link](16)) → 4.0
14. File Handling:
- f = open('[Link]', 'r') → Read mode
- f = open('[Link]', 'w') → Write mode
15. Exception Handling:
try:
# risky code
except Exception as e:
print(e)