Lab 1: Hello Python

Overview

Welcome to your first programming lab! Today you'll write your first Python programs, learn how to store information in variables, and practice basic input/output operations.

Learning Objectives

By completing this lab, you will be able to:

Prerequisites

  • Python installed on your computer
  • A code editor (VS Code recommended)
  • Curiosity and patience!

Setup

  1. Open your code editor
  2. Create a new file called lab1.py
  3. Save it somewhere you can find it later

Instructions

Part 1: Your First Program

Every programmer starts here. Let's write the classic "Hello, World!" program.

  1. Type this code in your file:
print("Hello, World!")
  1. Run your program (in VS Code, click the play button or press F5)

Expected output:

Hello, World!

Congratulations—you just wrote your first program! The print() function displays text on the screen. The text inside the quotes is called a string.

  1. Try changing the message. Make Python say something else:
print("Python is fun!")

Part 2: Variables

Variables are like labeled boxes that store information. Let's create some.

  1. Create variables to store your information:
# Storing text (strings)
name = "Alex"
favorite_color = "blue"

# Storing numbers (integers)
age = 20
year = 2025

# Storing decimal numbers (floats)
height = 5.8
gpa = 3.7
  1. Print your variables:
name = "Alex"
print(name)
print("My name is", name)

Expected output:

Alex
My name is Alex
  1. Now try combining variables in a print statement:
name = "Alex"
age = 20
print(name, "is", age, "years old")

Expected output:

Alex is 20 years old

Part 3: Getting User Input

Programs become more interesting when they interact with users.

  1. Ask the user for their name:
name = input("What is your name? ")
print("Hello,", name)

Run this program. Notice it waits for you to type something and press Enter.

Example interaction:

What is your name? Jordan
Hello, Jordan
  1. Create a program that asks for two pieces of information:
name = input("What is your name? ")
color = input("What is your favorite color? ")
print(name, "likes the color", color)

Part 4: Basic Math

Python can work as a calculator. Let's do some math!

  1. Try these operations:
# Addition
total = 5 + 3
print("5 + 3 =", total)

# Subtraction
difference = 10 - 4
print("10 - 4 =", difference)

# Multiplication
product = 6 * 7
print("6 * 7 =", product)

# Division
quotient = 15 / 3
print("15 / 3 =", quotient)
  1. Create a program that calculates the area of a rectangle:
length = 8
width = 5
area = length * width
print("A rectangle with length", length, "and width", width)
print("has an area of", area)

Expected output:

A rectangle with length 8 and width 5
has an area of 40

Part 5: Putting It Together

Now let's combine everything you've learned!

Create a program called greeting.py that:

  1. Asks the user for their name
  2. Asks the user for their birth year
  3. Calculates their approximate age
  4. Prints a personalized greeting
# Get user information
name = input("What is your name? ")
birth_year = input("What year were you born? ")

# Calculate age (we need to convert the input to a number)
birth_year = int(birth_year)
current_year = 2025
age = current_year - birth_year

# Print the greeting
print("Hello,", name + "!")
print("You are approximately", age, "years old.")

Example interaction:

What is your name? Sam
What year were you born? 2003
Hello, Sam!
You are approximately 22 years old.

Submission

  1. Create a final program called about_me.py that:

    • Stores your name in a variable
    • Stores your major/intended major in a variable
    • Stores the number of credits you're taking in a variable
    • Asks the user how many hours they study per week
    • Calculates study hours per credit
    • Prints all this information in a nice format
  2. Push your lab1.py and about_me.py files to your GitHub repository

Self-Check

Before submitting, verify:

Extension Challenges (Optional)

  1. Temperature Converter: Write a program that asks for a temperature in Fahrenheit and converts it to Celsius. Formula: celsius = (fahrenheit - 32) * 5/9

  2. Mad Libs: Create a simple mad libs game that asks for a noun, verb, and adjective, then prints a silly sentence using them.

Getting Help