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.
To start programming in Python, you need to install it on your computer:
python --versionLet'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.
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"
}
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)
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")
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!
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}")
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!
Build a simple calculator that can add, subtract, multiply, and divide two numbers.
Requirements:
Hint: Use the input() function to get user input,
and remember to convert strings to numbers using int() or
float().
Great job completing Python Basics! You now have a solid foundation in Python programming.
Recommended next steps: