👋 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!
- 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:
- Click the Start menu
- Find and open Anaconda Navigator
- Click Launch under Spyder
On Mac:
- Open Finder → Applications → Anaconda Navigator
- Click Launch under 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
- Click File → Save As
- Choose a location you'll remember (like your Desktop or a folder for this class)
- Name the file
checkin.py- The
.pyextension tells the computer this is a Python program
- The
Step 3: Run Your Program
- Click the green Run button (▶) at the top of Spyder, or press F5
- Look at the bottom-right panel (the Console)—your program is now running there!
- Type your responses when prompted and press Enter after each one
What you'll see:
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
📊 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 |
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."
- Use lowercase letters
- Use underscores to separate words:
first_name(notfirstNameorfirstname) - Make names descriptive so you remember what they store
- Spelling must be exact—
first_nameandfirstnameare 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
- Click File → New File
- Type the code above
- Save it as
rectangle.py - Press F5 to run
Sample interaction:
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))
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
age = input("Enter your age: ")
next_year = age + 1 # ERROR! Can't add a string and a numberage = int(input("Enter your age: "))
next_year = age + 1 # Works! age is now an integerMistake 2: Typos in Variable Names
first_name = input("Enter your first name: ")
print("Hello " + firstname) # ERROR! firstname ≠ first_namefirst_name = input("Enter your first name: ")
print("Hello " + first_name) # Variable names must match exactlyMistake 3: Missing Quotation Marks
print(Hello there) # ERROR! Text needs quotesprint("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:
Exercise 2: Temperature Converter
Write a program that:
- Asks the user for a temperature in Fahrenheit
- Converts it to Celsius using the formula:
C = (F - 32) * 5 / 9 - Displays the result
Exercise 3: Tip Calculator
Write a program that:
- Asks for the bill amount
- Asks for the tip percentage (like 15, 18, or 20)
- Calculates and displays the tip amount and total bill
Exercise 4: Age Calculator
Write a program that:
- Asks for the user's birth year
- Calculates their age (assuming the current year is 2024)
- Displays their age
📋 Quick Reference
name = input("Enter name: ")age = int(input("Enter age: "))price = float(input("Enter price: "))print("Hello!")print()print("Hello " + name)🆘 Need Help?
If your program doesn't work, follow these steps:
- Read the error message—Python usually tells you what line has the problem
- Check your spelling—variable names must match exactly
- Check your quotes—strings need opening AND closing quotes
- Check your parentheses—every
(needs a matching)