Back to Guides

Python Basics

⏱️ 4 hours 📝 18 lessons 🎯 Beginner
Lesson 1

Why Python?

Python is one of the most popular programming languages in the world. It's known for its simple, readable syntax that makes it perfect for beginners while being powerful enough for professionals.

Python is Used For

  • Web Development (Django, Flask)
  • Data Science & Machine Learning
  • Automation & Scripting
  • Game Development
  • Desktop Applications
Lesson 2

Installing Python

To start programming in Python, you need to install it on your computer:

  1. Visit python.org
  2. Download the latest version (Python 3.x)
  3. Run the installer (make sure to check "Add Python to PATH")
  4. Verify installation by opening terminal/command prompt and typing: python --version
Lesson 3

Your First Python Program

Let's write the classic "Hello, World!" program:

print("Hello, World!")

That's it! Python's simplicity shines here. Compare this to other languages that require more boilerplate code. The print() function displays text to the console.

Lesson 4

Variables and Data Types

In Python, you don't need to declare variable types - Python figures it out automatically (dynamic typing):

# Strings
name = "Neo"
greeting = 'Hello, World!'

# Numbers
age = 30
price = 19.99

# Boolean
is_active = True
is_logged_in = False

# Lists (arrays)
numbers = [1, 2, 3, 4, 5]
names = ["Neo", "Trinity", "Morpheus"]

# Dictionaries (key-value pairs)
person = {
  "name": "Neo",
  "age": 30,
  "city": "The Matrix"
}
Lesson 5

String Operations

Python provides many ways to work with strings:

name = "Neo"

# String concatenation
greeting = "Hello, " + name

# F-strings (modern way)
greeting = f"Hello, {name}!"

# String methods
text = "the matrix has you"
print(text.upper()) # THE MATRIX HAS YOU
print(text.title()) # The Matrix Has You
print(text.replace("matrix", "code")) # the code has you

# String slicing
word = "Python"
print(word[0]) # P (first character)
print(word[-1]) # n (last character)
print(word[0:3]) # Pyt (characters 0 to 2)
Lesson 6

Conditional Statements

Python uses indentation to define code blocks (instead of curly braces like other languages):

age = 18

if age >= 18:
  print("You are an adult")
elif age >= 13:
  print("You are a teenager")
else:
  print("You are a child")

# Multiple conditions
username = "neo"
password = "theone"

if username == "neo" and password == "theone":
  print("Welcome to the Matrix")
else:
  print("Access denied")

Important!

Python uses indentation (4 spaces or 1 tab) to define code blocks. Proper indentation is not just for readability - it's required for your code to work!

Lesson 7

Loops

Python has two main types of loops: for loops and while loops.

# For loop - iterate over a sequence
names = ["Neo", "Trinity", "Morpheus"]
for name in names:
  print(f"Hello, {name}!")

# For loop with range
for i in range(5): # 0, 1, 2, 3, 4
  print(f"Count: {i}")

# While loop - repeat while condition is true
count = 0
while count < 5:
  print(f"Count: {count}")
  count += 1

# Loop through dictionary
person = {"name": "Neo", "age": 30}
for key, value in person.items():
  print(f"{key}: {value}")
Lesson 8

Functions

Functions help organize your code into reusable blocks:

# Basic function
def greet(name):
  return f"Hello, {name}!"

message = greet("Neo")
print(message) # Hello, Neo!

# Function with multiple parameters
def add_numbers(a, b):
  return a + b

result = add_numbers(5, 3)
print(result) # 8

# Function with default parameter
def greet_user(name="User"):
  return f"Welcome, {name}!"

print(greet_user()) # Welcome, User!
print(greet_user("Neo")) # Welcome, Neo!
Practice Project

Build a Simple Calculator

Challenge: Create a Calculator

Build a simple calculator that can add, subtract, multiply, and divide two numbers.

Requirements:

  • Create functions for each operation (add, subtract, multiply, divide)
  • Ask the user to input two numbers
  • Ask the user to choose an operation
  • Display the result

Hint: Use the input() function to get user input, and remember to convert strings to numbers using int() or float().

Next Steps

Continue Learning Python

Great job completing Python Basics! You now have a solid foundation in Python programming.

Recommended next steps:

  • Practice by building small projects (to-do list, number guessing game, etc.)
  • Learn about Python libraries and modules
  • Explore Python for Data Science or Web Development
  • Work on file handling and error handling
🤖 AI Assistant
Hello! I'm here to help with Python. Ask me about syntax, concepts, or debugging!