Python is one of the most beginner-friendly programming languages, and it’s no surprise that it’s become the go-to choice for web development, data science, automation, machine learning, and more. Whether you’re completely new to coding or switching from another language, this tutorial will guide you through the fundamentals of Python, with clear examples and explanations.
Let’s break it down into digestible parts, so you can learn Python from scratch — no previous experience needed.
Table of Contents
- What Is Python and Why Learn It?
- Installing Python and Setting Up
- Writing Your First Python Program
- Python Variables and Data Types
- Operators in Python
- Control Flow:
if
,else
,elif
- Loops:
for
andwhile
- Functions in Python
- Lists, Tuples, and Dictionaries
- Error Handling with
try
andexcept
- File Handling
- Intro to OOP (Object-Oriented Programming)
- Python Libraries to Explore Next
1. What Is Python and Why Learn It?
Python is a high-level, interpreted programming language known for its:
- Simple, readable syntax
- Large community and library support
- Versatility (web apps, AI, automation, games, etc.)
Who uses Python?
Companies like Google, Netflix, NASA, and Instagram rely on Python daily.
2. Installing Python and Setting Up
Step 1: Install Python
Go to python.org and download the latest stable version (e.g., Python 3.12+).
Step 2: Choose an IDE
- IDLE (comes with Python)
- VS Code (lightweight and powerful)
- PyCharm (feature-rich)
3. Writing Your First Python Program
Open your IDE and type:
print("Hello, world!")
Save the file as hello.py
and run it. You should see:
Hello, world!
4. Python Variables and Data Types
name = "Alice" # String
age = 25 # Integer
height = 5.5 # Float
is_student = True # Boolean
Python is dynamically typed. You don’t have to declare the type explicitly.
5. Operators in Python
- Arithmetic:
+
,-
,*
,/
,//
,%
,**
- Comparison:
==
,!=
,<
,>
,<=
,>=
- Logical:
and
,or
,not
x = 10
y = 3
print(x + y) # 13
print(x // y) # 3 (floor division)
6. Control Flow: if, elif, else
num = 7
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
Indentation is mandatory in Python.
7. Loops: for and while
for
loop:
for i in range(5):
print(i)
while
loop:
count = 0
while count < 3:
print(count)
count += 1
8. Functions in Python
def greet(name):
print("Hello, " + name)
greet("Alice")
You can also return values:
def add(a, b):
return a + b
9. Lists, Tuples, and Dictionaries
Lists (mutable):
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
Tuples (immutable):
colors = ("red", "green", "blue")
Dictionaries (key-value pairs):
person = {"name": "Alice", "age": 25}
print(person["name"])
10. Error Handling
try:
x = 5 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
finally:
print("Execution complete.")
11. File Handling
# Writing to a file
with open("example.txt", "w") as f:
f.write("Hello from Python!")
# Reading from a file
with open("example.txt", "r") as f:
content = f.read()
print(content)
12. Introduction to Object-Oriented Programming (OOP)
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello, my name is", self.name)
p = Person("Alice")
p.greet()
Key concepts: class, object, constructor (__init__
), methods, and self.
13. Python Libraries You Should Explore Next
Once you’re confident with the basics, try:
math
andrandom
for simple calculationsdatetime
for date/time handlingos
andsys
for system-level tasksrequests
for web scrapingpandas
,matplotlib
,numpy
for data sciencetkinter
orpygame
for GUI/game development
Final Tips for Learning Python
Practice regularly — build small projects
Read others’ code (GitHub is a great place to start)
Use Python Tutor to visualize code
Explore free tutorials, coding challenges, and YouTube channels
Ready to Start Building?
This tutorial gives you a solid foundation. From here, you can move into web development with Flask or Django, data analysis, machine learning, or even automation scripts that save you time daily.
Stay curious, experiment often, and most importantly — keep coding!