Note: Iβm not an expert. Iβm writing this blog just to document my learning journey. π
1. What is Python?
Python is a tool we use to give instructions to a computer.
Itβs a programming language β a way to write tasks in simple, readable English-like words so the computer can follow them.
2. What is a Program?
A program is a list of instructions that a computer runs step by step.
To write a program in Python, we write instructions using Pythonβs rules (called syntax). The instructions tell the computer what to do with data (numbers, text, etc.).
3. What is Data?
Data is information β like:
- Numbers (
10
) - Text (
"hello"
) - True/False values (
True
,False
)
Every piece of data has a type. Common types in Python:
-
int
: whole numbers like5
-
float
: decimal numbers like3.14
-
str
: text like"Python"
-
bool
: truth values:True
orFalse
4. What are Variables?
A variable is a name you give to a piece of data so you can use it later.
age = 25
name = "Alice"
Here, age
stores the number 25
, and name
stores the word "Alice"
.
5. What Can You Do With Data?
You use operators to do things with data.
Math:
5 + 2 # 7
5 - 2 # 3
5 * 2 # 10
5 / 2 # 2.5
Comparisons:
5 > 2 # True
3 == 3 # True
4 != 5 # True
6. How to Make Decisions in Code
We use if
statements to tell Python: βOnly do this if a condition is true.β
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
Python checks the condition. If it's true, it runs the code under if
. Otherwise, it runs the code under else
.
7. How to Repeat Things
You use loops to repeat actions.
for
loop β repeats a set number of times:
for i in range(3):
print("Hello")
This prints "Hello"
three times.
while
loop β repeats while a condition is true:
count = 0
while count < 3:
print(count)
count += 1
This prints 0
, 1
, and 2
.
8. Functions: Reusable Code
A function is a block of code that does one job. You can run it (call it) whenever you want.
def greet(name):
print("Hello", name)
greet("Alice")
You define it with def
, give it a name, and call it by writing its name and giving it any needed inputs.
9. Lists: A Collection of Values
A list holds multiple values in one variable.
fruits = ["apple", "banana", "cherry"]
You can get items by position:
print(fruits[0]) # apple
You can also add to it:
fruits.append("orange")
10. Dictionaries: Key-Value Pairs
A dictionary stores values with names (keys), like a mini-database.
person = {"name": "Alice", "age": 30}
print(person["name"]) # Alice
11. Handling Errors
Sometimes code breaks. You can handle errors using try
and except
.
try:
print(10 / 0)
except ZeroDivisionError:
print("Can't divide by zero")
This stops the program from crashing.
12. Using Tools: Importing Modules
Python has extra features stored in modules. You can bring them into your code using import
.
import math
print(math.sqrt(16)) # 4.0
13. Classes and Objects: Building Custom Types
A class is like a blueprint. It defines what something is and what it can do.
An object is a thing made from that blueprint.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(self.name + " says woof!")
my_dog = Dog("Buddy")
my_dog.bark() # Buddy says woof!
You use classes to organize your code and model real things.
Summary
Python lets you:
- Store data in variables
- Use operators to work with data
- Make decisions with
if
- Repeat tasks with loops
- Reuse code with functions
- Group data with lists and dictionaries
- Handle errors
- Use built-in tools (modules)
- Build custom structures with classes
Top comments (0)