Python Coding Resharpen Part 1-10
Python Coding Resharpen Part 1-10
Environment Setup
Tasks:
● Install Python
Verify installation:
python --version
○
● Install VS Code
Task:
Write and execute a simple program:
print("Hello World")
Run in Terminal:
python hello.py
●
3. Basic Operations & Debugging
Tasks:
Arithmetic Operations:
x = 10
y = 20
print(x + y) # Addition (30)
print(x - y) # Subtraction (-10)
print(x * y) # Multiplication (200)
print(x / y) # Division (0.5)
●
● Debugging in PyCharm/VS Code:
Tasks:
Single-line Comment:
# This is a comment
Multi-line Comment:
"""
This is a multi-line
comment (docstring).
"""
Rules:
● Use lower_case_with_underscores (PEP 8 standard).
● Avoid reserved keywords (if, for, class).
● Start with a letter or _, not a number.
Example:
total_cost = 100 # Descriptive name
student_age = 20 # Lowercase with underscore
_is_admin = True # Private variable (convention)
6. Data Types
Tuple (Immutable):
colors = ("red", "green", "blue")
# colors[0] = "yellow" # Error (immutable)
Range:
numbers = range(1, 10, 2) # 1, 3, 5, 7, 9
●
© Mapping Type
Dictionary (dict):
person = {
"name": "Rabbi",
"age": 34,
"is_bangladeshi": True
}
print(person["name"]) # Access value
FrozenSet (Immutable):
frozen = frozenset({1, 2, 3})
● is_student = True
● has_money = None # Absence of value
Tasks:
Check Type:
print(type(10)) # <class 'int'="">
print(type("Hello")) # <class 'str'="">
●
Explicit Conversion:
num_str = "123"
num_int = int(num_str) # String → Integer
Implicit Conversion:
x = 10 # int
y = 3.14 # float
z = x + y # float (13.14)
8. Mutability vs Immutability
Immutable Objects:
Cannot be modified after creation (int, str, tuple).
a = 10
print(id(a)) # Memory address (unchanged if reassigned)
Mutable Objects:
Can be modified (list, dict, set).
my_list = [1, 2, 3]
my_list[0] = 100 # Valid (same memory address)
●
9. Exception Handling