Python Programming Fundamentals and Applications
Python Basics Review
Python's Building Blocks
Since you already know what variables and data types are, let's look at how Python handles them. Python is dynamically typed, which means you don't need to declare a variable's type beforehand. The interpreter figures it out when you assign a value.
# Python infers the data types
# Integer
user_age = 30
# Float (floating-point number)
price = 19.99
# Boolean
is_active = True
# String
user_name = "Alex"
Operators work as you'd expect. Arithmetic operators perform math, comparison operators evaluate relationships, and logical operators combine boolean expressions.
| Operator Type | Python Symbols | Example |
|---|---|---|
| Arithmetic | +, -, *, /, % | 10 / 2 is 5.0 |
| Comparison | ==, !=, >, < | age > 18 |
| Logical | and, or, not | x > 0 and y < 10 |
| Assignment | =, +=, -= | count += 1 |
Talking to Your Program
To display information, you use the print() function. To get information from the user, you use the input() function.
A key detail: input() always returns the user's entry as a string. If you need a number, you have to convert it explicitly using int() or float().
# Get the user's name and age
name = input("What is your name? ")
age_str = input("How old are you? ")
# Convert the age string to an integer
age = int(age_str)
# Print a formatted message
print(f"Hello, {name}! You will be {age + 1} next year.")
Making Decisions
Python uses if, elif (short for "else if"), and else for conditional logic. The structure relies on colons and indentation to define code blocks, not curly braces. This whitespace is not optional; it's a core part of the syntax and a guiding principle of PEP 8.
score = int(input("Enter your score (0-100): "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Keep studying!")
The program checks each condition from top to bottom and executes the first block where the condition is true. If none are true, the else block runs.
Repeating Actions
Python has two main loop types: for and while.
A for loop is ideal when you want to iterate over a sequence of items, like a range of numbers. The range() function is perfect for this, generating numbers on demand. For example, range(5) gives you numbers from 0 up to, but not including, 5.
# Prints numbers 0, 1, 2, 3, 4
for i in range(5):
print(i)
A while loop runs as long as a certain condition remains true. It's best when you don't know in advance how many times you need to repeat the action.
count = 0
while count < 3:
print("Looping...")
count += 1 # Don't forget to change the condition!
You can also control loop behavior with break and continue. The break statement exits a loop immediately, while continue skips the rest of the current iteration and jumps to the next one.
# Example of break and continue
for num in range(10):
if num == 3:
continue # Skip printing 3
if num == 7:
break # Stop the loop when num is 7
print(num)
# Output: 0, 1, 2, 4, 5, 6
Now, let's test your understanding of these core concepts.
What does it mean for Python to be a "dynamically typed" language?
What is the data type of the value returned by the input() function?
With these fundamentals refreshed, you're ready to build more complex programs in Python.