Starting with Python basics is a smart approach. Here’s a roadmap to get you comfortable with the language:
1. Python Syntax and Basics
- Hello World: The simplest program in Python.
print("Hello, World!")
- Variables and Data Types: Learn to store values.
name = "Chandra"
age = 30
height = 5.8
Python has basic data types like:
int
(integers)float
(floating-point numbers)str
(strings)bool
(booleans:True
/False
)
2. Control Flow
- If Statements: Make decisions in your code.
age = 18
if age >= 18:
print("You're an adult.")
else:
print("You're a minor.")
- For Loops: Loop through a sequence.
for i in range(5):
print(i)
- While Loops: Keep running while a condition is
True
.
count = 0
while count < 5:
print(count)
count += 1
3. Functions
Functions allow you to reuse code blocks.
def greet(name):
return f"Hello, {name}!"
print(greet("Chandra"))
4. Lists, Tuples, and Dictionaries
These are core data structures in Python:
- Lists: Ordered, mutable collections.
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Outputs: banana
- Tuples: Ordered, immutable collections.
point = (3, 4)
- Dictionaries: Key-value pairs.
person = {"name": "Chandra", "age": 30}
print(person["name"])
5. Basic Input/Output
Get user input and display output.
name = input("Enter your name: ")
print(f"Hello, {name}!")
6. Basic File Handling
Reading and writing files is crucial in many projects.
# Writing to a file
with open("output.txt", "w") as file:
file.write("Hello, world!")
# Reading from a file
with open("output.txt", "r") as file:
content = file.read()
print(content)
7. Error Handling
Catch and handle errors to make your code more robust.
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
8. Libraries and Modules
Python has a vast library ecosystem. Importing a module allows you to use pre-written code:
import math
print(math.sqrt(16))
Next Steps
- Practice: Try small programs like a calculator, number guessing game, or to-do list.
- Projects: As you get comfortable, start small projects like creating a text-based game or basic file management system.