CS Logo

Python Coding Camp

Day 1 — Python Basics

Variables · Strings · Input/Output · Conditionals · Loops

⬇ Today's slides (PDF)

Schedule

TimeActivity
0:00 – 0:40Lecture: variables, strings, input, print
0:40 – 1:20Project: Mad Libs
1:20 – 1:30Break
1:30 – 2:00Lecture: if/elif/else, while loops
2:00 – 3:00Project: Number Guessing Game

Variable Types

Every value in Python has a type that tells Python what kind of data it is. This matters because Python treats them differently — you can do math with numbers but not with text.

The Four Types You'll Use Today

# int — whole numbers (no decimal point)
age = 16
score = 100
temperature = -5

# float — numbers with a decimal point
gpa = 3.75
price = 9.99

# str — text, always wrapped in quotes
name = "Alice"
greeting = 'Hello, world!'   # single or double quotes both work

# bool — only two possible values: True or False (capital T and F!)
is_raining = True
has_homework = False

Checking the Type of a Variable

print(type(42))        # <class 'int'>
print(type(3.14))      # <class 'float'>
print(type("hello"))   # <class 'str'>
print(type(True))      # <class 'bool'>

Converting Between Types

input() always gives you a str, even if the user types a number. You must convert it before doing math — this trips up almost every beginner.

# input() gives back a str, not a number
user_input = input("Enter a number: ")   # user types 5
print(type(user_input))                  # <class 'str'> — it's text, not a number!

# int() converts a str to a whole number so we can do math
number = int(user_input)
print(number + 1)                        # now works — prints 6

# float() converts to a decimal number
price = float(input("Enter a price: "))  # e.g. user types 3.99

# str() converts a number to text so we can join it with other strings
age = 16
message = "I am " + str(age) + " years old"
print(message)                           # I am 16 years old

# Common mistake — this crashes with a TypeError!
# print("I am " + age + " years old")   # can't add str and int directly

Quick Reference — Things You'll Use Today

Variables and Print

# A variable stores a value and gives it a name you can use later
name = "Alice"   # str — text in quotes
age = 16         # int — whole number

print(name)                                          # prints: Alice
print("I am", age, "years old")                      # prints: I am 16 years old
print(f"Hello, {name}! You are {age} years old.")    # f-string: put { } around variables

Getting Input from the User

name = input("What is your name? ")   # pauses and waits for the user to type something
print(f"Hi, {name}!")                 # uses whatever they typed

# input() ALWAYS gives back a str — convert it if you need a number
age = int(input("How old are you? "))   # int() converts "16" (str) to 16 (int)

If / Elif / Else

score = int(input("Enter your score: "))

if score >= 90:       # if this is True, run the indented block below
    print("A")
elif score >= 80:     # "else if" — only checked when the if above was False
    print("B")
elif score >= 70:
    print("C")
else:                 # runs when ALL the conditions above were False
    print("Keep trying!")

While Loop

count = 0               # start at 0

while count < 5:        # keep looping as long as count is less than 5
    print(count)        # prints 0, then 1, 2, 3, 4
    count = count + 1   # increase count each time — REQUIRED or the loop runs forever!

Random Numbers

import random                          # load the random module (built into Python)
number = random.randint(1, 100)        # pick a random whole number from 1 to 100 (inclusive)
print(number)

Project 1 — Mad Libs

Ask the user for a list of words, then plug them into a funny story. The program asks for the words first, then reveals the story at the end.

Starter Code

# Mad Libs Generator

print("Answer these questions to create your story!")
print()

noun1 = input("Give me a noun: ")
noun2 = input("Give me another noun: ")
verb1 = input("Give me a verb (action word): ")
adjective1 = input("Give me an adjective (describing word): ")
place = input("Give me a place: ")
number = input("Give me a number: ")

print()
print("--- Your Story ---")
print()
print(f"One day, a {adjective1} {noun1} decided to {verb1} all the way to {place}.")
print(f"On the way, they found {number} {noun2}s just lying on the ground.")
print("The end.")
Tip: Run the program, type your words, and see what happens! Then change the story template to make it funnier.

Stretch Goals

  1. Add more words (a verb2, a second adjective, a celebrity name, etc.)
  2. Write a completely different story — make it about school, gaming, or something you care about
  3. Ask the user how many nouns they want, then use a loop to collect them all
  4. Let the user choose from two different story templates

Project 2 — Number Guessing Game

The computer picks a secret number between 1 and 100. The player guesses until they get it right. After each guess, the program says "too high" or "too low."

Starter Code

import random

secret = random.randint(1, 100)   # computer picks a secret number
guess = 0                          # set to 0 so the while loop starts (0 != secret)

print("I'm thinking of a number between 1 and 100.")
print("Can you guess it?")
print()

while guess != secret:             # keep looping until the guess matches the secret
    guess = int(input("Your guess: "))   # input() gives a str; int() converts it to a number

    if guess < secret:
        print("Too low! Try again.")
    elif guess > secret:
        print("Too high! Try again.")
    else:
        print("You got it!")       # only reached when guess == secret, which ends the loop
Tip: What happens if the user types a word instead of a number? Don't worry about that yet — just get it working first.

Stretch Goals

  1. Count how many guesses the player takes and print it at the end
  2. Give a rating: "Amazing!" for under 5 guesses, "Good!" for under 10, "Keep practicing!" for more
  3. Limit the player to 7 guesses. If they run out, reveal the number and print "Game over!"
  4. After the game ends, ask "Play again? (yes/no)" and restart if they say yes
  5. Boss challenge: Build Rock Paper Scissors — player vs. computer, best of 3

More Project Options — Pick Any

Done with the two projects above? Pick anything here that sounds fun. They all use today's tools: variables, input, f-strings, if/elif/else, loops, and lists. The Boss ones are for when you're flying.

Starter — everyone can finish one of these

Starter

Fortune Teller / Magic 8-Ball

Ask a yes/no question, then print a random answer from a list.

import random

answers = ["Yes", "No", "Definitely!", "Ask again later", "No way"]
question = input("Ask the Magic 8-Ball a question: ")
print(random.choice(answers))   # random.choice picks one item from the list
Starter

Tip / Pizza-Split Calculator

Ask for a bill total and number of people, then print each person's share.

total  = float(input("Bill total: $"))
people = int(input("How many people? "))
tip    = total * 0.18                       # 18% tip
each   = (total + tip) / people
print(f"Each person pays ${round(each, 2)}")   # round to 2 decimal places
Starter

Times-Table Printer

Ask for a number and print its multiplication table using a loop.

Standard — pick one and make it yours

Standard

Choose-Your-Own-Adventure

Tell a story where the player types choices. Use if/elif/else to branch to different endings. Add more branches for a longer adventure.

Standard

Password Strength Checker

Ask for a password and rate it Weak / OK / Strong based on its length and whether it has numbers and symbols.

pw = input("Choose a password: ")
long_enough = len(pw) >= 8
has_number  = any(c.isdigit() for c in pw)   # True if any character is a digit
print("Long enough:", long_enough)
print("Has a number:", has_number)
Standard

Rock–Paper–Scissors

Play against the computer (random.choice). Compare the two choices with if/elif/else to decide the winner. Make it best-of-3.

Boss Challenge — for when you're way ahead

Boss

Caesar Cipher

Shift every letter in a message forward by N to encode it, then subtract N to decode it. Look up ord() and chr() to turn letters into numbers and back.

Boss

Hangman

Pick a secret word, let the player guess letters one at a time, and only allow a limited number of wrong guesses. Track guessed letters in a list and rebuild the word with blanks for the letters not yet found.

Save Your Work with Git & GitHub

A version control system saves snapshots of your code as you work, so a bad edit can never wipe out your progress. Git is the tool that takes the snapshots; GitHub is the website that stores them online. Almost every programmer uses both.

The Workflow, Start to Finish

Step 1 — create the repo. Make a free account at github.com, click New repository, and give it a name (e.g. my-code).

Heads up — 2FA is required: GitHub makes everyone who pushes code turn on two-factor authentication. Set it up (an authenticator app or your phone number) right after you make the account, or you'll be blocked at your first push.

Step 2 — clone it. Copy the repo's URL, then clone to pull a copy down onto your computer. Cloning makes a folder that's already linked to GitHub — no other setup needed:

git clone https://github.com/you/my-code.git

Step 3 — go in and make changes. Step into the folder (open it in your editor) and write your code:

cd my-code                     # step into your project folder
# ...write madlibs.py, guessing_game.py...

Step 4 — save a snapshot and upload it. A commit is one saved snapshot with a short message; push sends it up to GitHub:

git add .                      # gather up all your changes
git commit -m "My first Python programs"   # save a snapshot
git push                       # upload it to GitHub

Repeat steps 3–4 every time you get something working — by Friday your whole week of projects is saved in one place.