Mastering Python: Day 3 — Control Flow, User Input, and Building a Simple Calculator
Welcome to Day 3 of your Python learning journey! Today, we’ll expand on our understanding of control flow, specifically focusing on conditional statements, loops, and how to interact with users through user input. Additionally, we’ll practice these concepts by building a simple calculator program.
Part 1: Conditional Statements (if, elif, else)
Conditional statements are used to make decisions in Python programs. They allow you to execute different blocks of code based on specific conditions. In this part, we’ll explore conditional statements and how they work.
Conditional statements in Python include:
if
Statements: These statements are used to execute a block of code if a condition is true.elif
Statements: “elif” stands for “else if” and is used to check additional conditions if the initial “if” condition is false.else
Statements: The “else” statement provides a fallback option if none of the preceding conditions are true.
Example: Checking if a Number is Even or Odd
# Conditional Statement Example
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even.")
else:
print(f"{num} is odd.")
Part 2: Loops (for, while)
Loops in Python are used to execute a block of code repeatedly. There are two primary types of loops we’ll explore:
for
Loops: These loops are used to iterate over a sequence or range of values.while
Loops: “while” loops repeatedly execute a block of code as long as a specified condition remains true.
Example: Printing Numbers from 1 to 5 Using a for Loop
# for Loop Example
for i in range(1, 6):
print(i)
Example: Counting Down from 5 to 1 Using a while Loop
# while Loop Example
countdown = 5
while countdown > 0:
print(countdown)
countdown -= 1
Example: Calculates the sum of all positive integers ≤ 100
# Initialize a variable to store the sum
total = 0
# Loop to calculate the sum of positive integers up to 100
for number in range(1, 101):
total += number
# Display the result
print("The sum of all positive integers less than or equal to 100 is:", total)
Part 3: Building a Simple Calculator Program
Now, let’s put our knowledge into practice by building a basic calculator program. This program will take user input for two numbers and perform addition, subtraction, multiplication, or division based on the user’s choice.
Example: Simple Calculator Program
# Simple Calculator Program
# Display a menu for the user to select an operation
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
# Get the user's choice of operation
choice = input("Enter choice (1/2/3/4): ")
# Get the user's input for two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Perform the selected operation based on the user's choice
if choice == '1':
result = num1 + num2
elif choice == '2':
result = num1 - num2
elif choice == '3':
result = num1 * num2
elif choice == '4':
if num2 == 0:
print("Division by zero is not allowed.")
else:
result = num1 / num2
else:
print("Invalid input") # Handle invalid choices
# Display the result of the operation
print(f"Result: {result}")
By the end of Day 3, you’ll have a strong grasp of control flow, user input, and basic programming concepts in Python. Keep practicing and exploring more complex problems to further develop your skills. Next, we will delve into Python data structures.