Python Programming Fundamentals to Intermediate
Python Core Review
Core Concepts Revisited
Think of this as a quick warm-up. You already know the basics, so let's focus on how Python's fundamental pieces fit together in practice. We'll start with how data is stored and manipulated.
# Variable assignment and basic types
user_name = "Alex"
user_age = 32
login_attempts = 2.5 # A float, perhaps an average?
is_active = True
# Using an f-string to combine different types
welcome_message = f"Welcome, {user_name}! Age: {user_age}, Active: {is_active}"
print(welcome_message)
# Expected output: Welcome, Alex! Age: 32, Active: True
The code above is a compact reminder of variable assignment and Python’s core data types: strings (str), integers (int), floating-point numbers (float), and booleans (bool). Notice how an elegantly combines these different types into a single, readable string. This is the modern, preferred way to handle string formatting.
Operators and Control Flow
Operators are the verbs of programming—they perform actions. We can combine arithmetic, comparison, and logical operators to make decisions. Let's see how they work together to control the flow of a program.
# Combining operators for decision making
account_balance = 150.75
purchase_price = 160.00
credit_score = 720
# Can the user make the purchase?
# They need enough balance OR a good credit score.
if account_balance >= purchase_price or credit_score > 700:
print("Purchase approved.")
else:
print("Purchase denied.")
# Expected output: Purchase approved.
Here, the if statement evaluates a compound condition. Because the credit_score > 700 part is True, the logical or makes the entire condition True, and the purchase is approved, even though the balance was insufficient. This is a common pattern for creating flexible rules in an application.
For repetitive tasks, we use loops. A for loop is perfect when you want to perform an action on every item in a sequence, known as an iterable (like a list or a string). A while loop is better when you need to repeat an action until a specific condition is no longer true.
# --- For Loop Example ---
permissions = ["admin", "editor", "viewer"]
print("User Roles:")
for role in permissions:
print(f"- {role}")
# --- While Loop Example ---
import time
countdown = 3
print("\nLaunching in...")
while countdown > 0:
print(countdown)
countdown -= 1 # Decrement the counter
# time.sleep(1) # Uncomment to see a real countdown!
print("Launch!")
Bundling Logic with Functions
As programs grow, you don't want to repeat the same logic. Functions let you package up a piece of functionality, give it a name, and call it whenever you need it. This makes your code modular, reusable, and much easier to read.
def check_purchase_approval(balance, price, credit):
"""Checks if a purchase can be approved based on balance or credit."""
if balance >= price or credit > 700:
return "Approved"
else:
return "Denied"
# Let's use the function with different data
status1 = check_purchase_approval(150.75, 160.00, 720) # Approved (good credit)
status2 = check_purchase_approval(50.00, 100.00, 650) # Denied (both fail)
status3 = check_purchase_approval(200.00, 120.00, 600) # Approved (good balance)
print(f"Transaction 1: {status1}")
print(f"Transaction 2: {status2}")
print(f"Transaction 3: {status3}")
By wrapping our logic in the check_purchase_approval function, we've created a reusable tool. We can now test different scenarios just by calling the function with new arguments, without rewriting the if/else logic each time. This is the essence of building robust programs: breaking down problems into manageable, functional blocks.
Let's check your understanding of these core concepts.
What will be the output of the following Python code?
age = 30
is_member = False
print(f"User is over 25: {age > 25}, Is not a member: {not is_member}")
Given the code below, what is the final value of the approved_transactions counter?
approved_transactions = 0
attempts = [50, 200, 75, 300]
account_balance = 100
for purchase in attempts:
if purchase <= account_balance:
approved_transactions += 1
This quick review covers the foundational syntax and structures you'll build upon. With these patterns reinforced, you're ready to tackle more complex topics.