Welcome to this Python tutorial post! If you’re new to programming or looking to get started with Python, this guide provides simple, hands-on code examples based on the core topics in any beginner-friendly Python course. These snippets are great for self-study, assignments, or sharing with friends.
1. ✅ Installation and First Program
Before anything, make sure you have Python installed. You can download it from python.org. After installation, test it by running this in your editor or terminal:
# First Python program
print("Hello, Python world!")
2. 🧮 Types of Data and Variables
Python supports multiple data types. You don’t need to declare variable types — just assign and use.
name = "Ly"
age = 24
height = 1.65
is_student = True
print(name, age, height, is_student)
3. 🔁 Control Structures (If, For, While)
Control structures help make decisions and repeat tasks.
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C")
for i in range(5):
print(i)
x = 0
while x < 3:
print("Counting", x)
x += 1
4. ⬅️ Indentation and Grouping
Python uses indentation instead of curly braces.
if True:
print("This is indented properly")
if 2 > 1:
print("Nested block")
5. ⚙️ Functions and Procedures
Functions make your code reusable and clean.
def square(x):
return x * x
def greet(name="friend"):
print("Hello,", name)
print(square(4))
greet("Ly")
6. 👤 Classes
Python is an object-oriented language. You can define your own classes:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
p1 = Person("Ly", 24)
p1.say_hello()
7. 🆚 Class vs Instance Variables
Understand the difference between shared (class-level) and unique (instance-level) data.
class Dog:
species = "Canine" # Class variable
def __init__(self, name):
self.name = name # Instance variable
dog1 = Dog("Rex")
dog2 = Dog("Buddy")
dog2.species = "Wolf" # Overrides class variable for this instance
print(dog1.species) # Output: Canine
print(dog2.species) # Output: Wolf
8. 📦 Modules
Modules are Python files you can import to reuse functions and classes.
math_tools.py
def add(a, b):
return a + b
main.py
import math_tools
print(math_tools.add(3, 4))
9. 📁 Packages
A package is a folder containing multiple modules and an __init__.py file.
shapes/circle.py
def area(radius):
return 3.14 * radius ** 2
main.py
from shapes import circle
print(circle.area(3))
10. ⚠️ Exceptions (Try and Except)
Handle errors gracefully using try, except, and finally.
def divide(a, b):
try:
return a / b
except ZeroDivisionError as e:
print("Cannot divide by zero:", e)
print(divide(10, 2))
print(divide(5, 0))
11. 📂 File Objects
Read and write files with ease in Python.
# Write to a file
with open("hello.txt", "w") as f:
f.write("Hello, file world!")
# Read from the file
with open("hello.txt", "r") as f:
content = f.read()
print(content)
💬 Final Thoughts
These examples are meant to help you practice core concepts quickly. You can run these snippets in any Python environment (like VS Code, IDLE, or Jupyter Notebook). If you’re an instructor or teaching assistant, feel free to share this post as a starter kit for Python learners.