Python Fundamentals: A Beginners Guide
Variables and Data Types
In Python, you dont need to declare variable types Python figures it out for you.
name = "Alice" # String
age = 30 # Integer
height = 5.6 # Float
is_student = True # Boolean
Common Data Types:
- int: Whole numbers (10)
- float: Decimal numbers (3.14)
- str: Text ("Hello")
- bool: Boolean values (True / False)
- list, tuple, dict, set: Collections
Control Flow
Python uses indentation (usually 4 spaces) to define blocks of code.
if statements:
if age >= 18:
print("Adult")
else:
print("Minor")
for loops:
for i in range(5):
print(i) # 0 to 4
while loops:
count = 0
while count < 3:
print(count)
count += 1
Data Structures
Lists (Ordered, changeable, allow duplicates):
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
Tuples (Ordered, immutable):
dimensions = (10, 20)
Dictionaries (Key-value pairs):
person = {"name": "Alice", "age": 30}
print(person["name"])
Sets (Unordered, no duplicates):
unique_numbers = {1, 2, 3, 3} # {1, 2, 3}
Functions
Functions allow you to reuse code.
def greet(name):
return f"Hello, {name}!"
print(greet("Bob"))
Modules and Imports
Python has a huge standard library and supports custom modules.
import math
print(math.sqrt(16)) # 4.0
You can also create your own .py files and import them.
Error Handling
Use try...except to catch exceptions:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
Input and Output
Input from user:
name = input("Enter your name: ")
Output to console:
print("Hello", name)
File Handling
Reading from and writing to files:
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, file!")
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
Next Steps
Once youve mastered the fundamentals, you can explore:
- Object-Oriented Programming (OOP)
- Web frameworks (Flask, Django)
- Data analysis (Pandas, NumPy)
- Automation (Selenium, pyautogui)
Final Tip
Practice consistently. Python is readable and beginner-friendly, but mastering it requires writing
code regularly.
Try small projects like a calculator, a to-do app, or a simple game to build confidence.