html Copy code Introduction to Python Programming - Exercise One

Introduction to Python Programming

Andrew J. Pounds, Ph.D.

👋 Welcome to Programming!

If you've never written a program before, don't worry—this exercise will walk you through everything step by step. Programming is simply giving the computer a set of instructions to follow. Let's get started!

What You'll Learn:
  • How to get input from users
  • How to store data in variables
  • How to display output to the screen
  • How to work with different types of data

🚀 Getting Started with Anaconda

Since you have Anaconda installed, you have several ways to write and run Python code. We'll use the simplest method to start.

Launching Spyder (Recommended for Beginners)

On Windows:

  1. Click the Start menu
  2. Find and open Anaconda Navigator
  3. Click Launch under Spyder

On Mac:

  1. Open Finder → Applications → Anaconda Navigator
  2. Click Launch under Spyder
💡 What is Spyder?

Spyder is an application designed for writing Python code. When it opens, you'll see a window split into sections. The left side is where you'll type your code. The bottom-right is where your program's output will appear.

✍️ Your First Program

Step 1: Type the Code

In the left panel of Spyder (the Editor), type the following exactly as shown:

# Simulate checking in for an appointment
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
time = input("Enter time in hh:mm format: ")

print()
print("Thank you " + first_name + " " + last_name)
print()
print("We have you checked in at " + time)

Step 2: Save Your File

  1. Click File → Save As
  2. Choose a location you'll remember (like your Desktop or a folder for this class)
  3. Name the file checkin.py
    • The .py extension tells the computer this is a Python program

Step 3: Run Your Program

  1. Click the green Run button (▶) at the top of Spyder, or press F5
  2. Look at the bottom-right panel (the Console)—your program is now running there!
  3. Type your responses when prompted and press Enter after each one

What you'll see:

Enter your first name: Maria
Enter your last name: Garcia
Enter time in hh:mm format: 09:15

Thank you Maria Garcia

We have you checked in at 09:15
🎉 Congratulations! You just ran your first Python program!

📊 Understanding Data Types

Python works with different types of data. For now, you need to know three:

Type What It Is Examples
String Text—a collection of characters. In the example "ABC Bail Bonding," there are 16 characters (including the spaces, since spaces count too!). "Hello", "ABC Bail Bonding", "Room 204"
Integer Whole numbers (no decimals). These are positive and negative counting numbers. -10, 0, 42, 2024
Float "Floating point" numbers—numbers with decimals. Important: they are finite values, truncated or rounded after a certain number of decimal places. 3.14159, 98.6, -0.5
⚠️ Important: In Python 3, when you use input(), you do NOT need to enter your name in quotes. The function automatically treats everything as a string of text. Just type naturally!

🔍 Understanding the Code

Let's break down what each line of our first program does:

Code What It Does
# Simulate checking in... A comment—notes for humans. Python ignores lines starting with #
first_name = input("Enter your first name: ") Displays the message in quotes, waits for you to type something, then stores what you typed in a variable called first_name
print() Prints a blank line (adds spacing for readability)
print("Thank you " + first_name + " " + last_name) Displays text by joining (concatenating) strings together with +

What is a Variable?

Think of a variable as a labeled box where you can store information. When you write:

first_name = input("Enter your first name: ")

You're saying: "Whatever the user types, put it in a box labeled first_name so I can use it later."

Variable Naming Tips:
  • Use lowercase letters
  • Use underscores to separate words: first_name (not firstName or firstname)
  • Make names descriptive so you remember what they store
  • Spelling must be exact—first_name and firstname are different!

🔢 Example 2: Working with Numbers

What if we want to do calculations? We need to convert the input from a string to a number using float() or int().

Rectangle Area Calculator

# Calculate the area of a rectangle
print("=== Rectangle Area Calculator ===")
print()

length = float(input("Enter the length: "))
width = float(input("Enter the width: "))

area = length * width

print()
print("The area is " + str(area) + " square units")

Save and Run This Program

  1. Click File → New File
  2. Type the code above
  3. Save it as rectangle.py
  4. Press F5 to run

Sample interaction:

=== Rectangle Area Calculator ===

Enter the length: 5.5
Enter the width: 3

The area is 16.5 square units

What's New Here?

Code Purpose
float(input(...)) Takes the user's input and converts it to a decimal number
length * width Multiplies the two numbers together
str(area) Converts the number back to a string so we can combine it with other text using +

🧮 Example 3: Simple Calculator

# Simple addition calculator
print("=== Addition Calculator ===")
print()

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

total = num1 + num2

print()
print("The sum is: " + str(total))
Note: We used int() here instead of float() because we want whole numbers. Use int() for whole numbers and float() for decimals.

🔄 Type Conversion Functions

Function Purpose Example
int() Convert to integer (whole number) int("42") → 42
float() Convert to float (decimal number) float("3.14") → 3.14
str() Convert to string (text) str(42) → "42"

⚠️ Common Mistakes (And How to Fix Them)

Mistake 1: Forgetting to Convert Numbers

❌ Wrong:
age = input("Enter your age: ") next_year = age + 1 # ERROR! Can't add a string and a number
✅ Correct:
age = int(input("Enter your age: ")) next_year = age + 1 # Works! age is now an integer

Mistake 2: Typos in Variable Names

❌ Wrong:
first_name = input("Enter your first name: ") print("Hello " + firstname) # ERROR! firstname ≠ first_name
✅ Correct:
first_name = input("Enter your first name: ") print("Hello " + first_name) # Variable names must match exactly

Mistake 3: Missing Quotation Marks

❌ Wrong:
print(Hello there) # ERROR! Text needs quotes
✅ Correct:
print("Hello there") # Strings must be in quotes

💪 Practice Exercises

Try writing these programs on your own! This is the best way to learn.

Exercise 1: Personal Greeting

Write a program that asks for the user's name and favorite color, then prints a message like:

Hi, Maria! I hear your favorite color is blue.

Exercise 2: Temperature Converter

Write a program that:

  1. Asks the user for a temperature in Fahrenheit
  2. Converts it to Celsius using the formula: C = (F - 32) * 5 / 9
  3. Displays the result

Exercise 3: Tip Calculator

Write a program that:

  1. Asks for the bill amount
  2. Asks for the tip percentage (like 15, 18, or 20)
  3. Calculates and displays the tip amount and total bill

Exercise 4: Age Calculator

Write a program that:

  1. Asks for the user's birth year
  2. Calculates their age (assuming the current year is 2024)
  3. Displays their age

📋 Quick Reference

Get text input:
name = input("Enter name: ")
Get a whole number:
age = int(input("Enter age: "))
Get a decimal number:
price = float(input("Enter price: "))
Print text:
print("Hello!")
Print a blank line:
print()
Join strings together:
print("Hello " + name)

🆘 Need Help?

If your program doesn't work, follow these steps:

  1. Read the error message—Python usually tells you what line has the problem
  2. Check your spelling—variable names must match exactly
  3. Check your quotes—strings need opening AND closing quotes
  4. Check your parentheses—every ( needs a matching )
Remember: Programming takes practice. If something doesn't work the first time, that's completely normal—even experienced programmers spend time fixing errors! Each error you fix teaches you something new.