Python - First Program

Overview

Estimated time: 25–35 minutes

Write your first Python program, take user input, and format strings using f-strings. You’ll also practice running and verifying the output.

Learning Objectives

  • Print output and read input from the console.
  • Use variables and f-strings for formatting.
  • Run a script and verify expected output.

Prerequisites

Hello, World

print("Hello, World!")

Expected Output:

Hello, World!

Input and f-strings

name = input("What is your name? ")
print(f"Nice to meet you, {name}!")

Sample Run:

What is your name? Alice
Nice to meet you, Alice!

Two numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(f"{a} + {b} = {a + b}")

Sample Run:

Enter first number: 2
Enter second number: 3
2 + 3 = 5

Common Pitfalls

  • Forgetting to convert input (which is a string) to int for arithmetic.
  • Using quotes incorrectly inside f-strings (prefer single quotes outside if you need double quotes inside, or escape).

Checks for Understanding

  1. How do you read a line of text from the user?
  2. How do you insert a variable’s value into a string?
Show answers
  1. input()
  2. Use an f-string: f"Value is {x}"

Exercises

  1. Ask the user for their first and last name, then print a greeting using both.
  2. Prompt for width and height and print the area of a rectangle.