d Domain 1 - Python Workbook

Lesson 1: Getting Started

In Python, we use the print() function to display information.

🎯 Learning Target

I can use the print() function to display a message.

print("Hello, world!")

✏️ Your Turn

Write a Python statement to print your name and your favorite emoji.

Lesson 1.1: Strings and Integers

Strings and integers are data types that can be added to variables within Python code. A variable is a container that stores a value, like a string of text, a number, or other data types.

Strings of text go inside quotation marks and can be used in various ways. An integer is a numeric data type that represents a whole number.

Variable names can contain letters, numbers, and underscores, but not symbols or spaces. Python’s standard naming convention is all lowercase characters with underscores replacing spaces.

🎯 Purpose

Understand how to use strings and integers in Visual Studio using Python.

πŸ› οΈ Steps for Completion

  1. Open the 111-str.py file from your Domain 1 Student folder within Visual Studio.
  2. Note the two variables with strings containing a first and last name.
  3. Add a print statement on line 3 to output a full name.
    • You may choose "First Last" or "Last, First" format.
    • If using "Last, First", include a comma between the names.
  4. Run the code to confirm correct output.
  5. Python supports single or double quotes around strings.
  6. Why does this cause an error?
    print('Jason's turn')
  7. Save the file as 111-str-completed.py.
  8. Open the 112-numbers.py file.
  9. Numbers stored as strings (inside quotes) can't be used in calculations.
  10. On line 4, add this to check the data type of number_of_tries:
    print(type(number_of_tries))
  11. Run the file to confirm it's an integer.
  12. Save it as 112-numbers-completed.py.

Lesson: Floats and Booleans

Floats and booleans are additional data types in Python. While integers represent whole numbers, floats (short for "floating point numbers") are numbers with decimals, like 3.14 or 0.75. The boolean (or bool) data type only has two values: True or False. These are often used to control game logic, such as whether a player is alive or has won.

🎯 Learning Target

I can use floats and booleans in basic operations and identify their data types in Python.

βš™οΈ Steps for Completion

  1. Open the 113-numbers.py file from your Domain 1 Student folder.
  2. Observe that the variable multiplier on line 2 is a float.
  3. Add a print statement on line 4 that calculates number_of_tries + multiplier. Run the code.
  4. What data type is returned from this addition?
  5. Change the operator to / (division) and run the code again.
  6. What data type is returned now?
  7. Save the updated file as 113-numbers-completed.py.
  8. Floats are useful for tracking scores, currency, and measurements in games.
  9. Open the 114-boolean.py file.
  10. Check out the char_life variable. It’s set to True. Run the code to see what it does.
  11. Save the file as 114-boolean-completed.py.
  12. ⚠️ Reminder: Python is case-sensitive. Booleans must use capital True and False.

πŸ“ Project Details

πŸ“Œ Objectives Covered

Project: Analyze Data Types

🎯 Purpose

By completing this project, you will strengthen your understanding of Python data types and improve your ability to identify them correctly in code.

πŸ“ Steps for Completion

  1. Open the 114-analyze.py file from your Domain 1 Student folder.
  2. Review the five print statements in the file.
  3. Identify the data type used in each statement and record your answers below:

πŸ” Reflect

Think about what you just explored. Understanding data types is a key part of writing error-free, logical code.

Lesson 2: Variables

Variables store information in Python. You create a variable by giving it a name and assigning it a value using the = sign.

🎯 Learning Target

I can store and reuse data in variables.

name = "Ava"
print("Hi " + name)
  

✏️ Your Turn

Create a variable for your favorite movie or show. Then print a message using it.

Lesson: Data Type Conversion

  1. Open the 121-conversion.py file from your Domain 1 Student folder.
  2. Notice that the variable on line 2 stores a user rating (expected to be a whole number). Run the code.
  3. Try entering a float value between 1 and 5 in the terminal. Run the code again.
  4. What data type is stored in the input variable by default?

Lesson: Indexing

  1. Open the 122-indexing.py file from your Domain 1 Student folder.
  2. Line 1 lists categories for users to choose from. Line 2 prints the entire list.
  3. Copy the code from line 2 and paste it into line 4. Edit it to print only the first category: Early Bronze Age.
  4. Change the index value to 2 and run the code again.
  5. Which item in the list was returned?

Lesson 3: Numbers and Math

Python can do math just like a calculator. Use + to add, - to subtract, * to multiply, and / to divide.

🎯 Learning Target

I can use variables to perform math operations.

a = 5
b = 3
print(a + b)
  

✏️ Your Turn

Create two variables with numbers and print their product.

πŸ” Slicing

  1. Open the 123-slicing.py file from your Domain 1 Student folder.
  2. Notice the variable serial_number on line 1. It's a series of 16 numbers.
  3. Add a print() function on line 2 to show only the first 4 characters.
  4. On line 4, print only the last 4 characters.
  5. On line 5, print the 8 middle characters.

πŸ“¦ Data Structures

  1. Strings are a type of data structure because they are collections of characters.
  2. Match each code example to the correct data structure:

Lesson: Lists and Their Operations

A list is a collection of items stored in a specific order. Instead of creating a separate variable for each value, you can store them all in one list. Lists can grow and change using built-in methods like .append() and .insert().

🎯 Learning Target

I can store and manipulate multiple values using lists in Python.

✏️ Your Turn: Thinking About Lists

In the code below, each value is stored in a separate variable. Why might someone do this instead of using a list?

city1 = "Dallas"
city2 = "Austin"
city3 = "Houston"
    

πŸ›  Try It: Modify the Code

  1. Open 126-list_operations.py from your Domain 1 Student folder.
  2. Use append() to add "Sacramento" to the capitals list. Run the code.
  3. Use insert() to place "Little Rock" before "Sacramento". Rerun the code.
  4. What is the object and method in capitals.append("Sacramento")?
    • Object: capitals
    • Method: append
  5. Save the file as 126-list_operations-completed.py.

🧠 Deeper Thinking

  1. Why does "Little Rock" appear twice when you run the program?
  2. What are two ways you could fix this?
    • a. ______
    • b. ______

πŸ§ͺ Practice and Analyze

  1. Open 126-analyze.py.
  2. What will the print() statement on line 2 output?
  3. What kind of data structure is used on line 4?
  4. How would you change the coins list to use "bronze" instead of "platinum"?

πŸ“‚ Project Summary

  • Files: 126-list_operations.py, 126-analyze.py
  • Estimated Time: 10–15 minutes
  • Objectives:
    • 1.2.5 Use lists to store data
    • 1.2.6 Perform operations on lists

πŸ“Œ Notes

  • Append: capitals.append("Sacramento")
  • Insert before: capitals.insert(3, "Little Rock")
  • Reinforce that . accesses a method of the object before it.
  • Have You should verbalize how list operations affect code flow and output.

Lesson 5: String + Number

You can use str() to convert numbers into strings so they can be combined with text.

🎯 Learning Target

I can display numbers in sentences using str().

age = 12
print("I am " + str(age) + " years old.")
  

✏️ Your Turn

Create a variable for a number, convert it to a string, and print it in a sentence.

Lesson: Assignment, Comparison & Logical Operators

🟒 Assignment Operators

Operators perform calculations and evaluate conditions. The six main types in Python are arithmetic, containment, comparison, identity, logical, and assignment. Assignment operators are used to set values to variables.

🎯 Purpose

Understand how assignment operators work and in what sequence they are executed.

πŸ“ Steps for Completion

  1. Name the six types of operators in the order of execution.
  2. Review the code and answer the following:
    • What will happen when this code is run?
    • Print will disply as x is now: 8. 5+3=8

      x = 5
      x += 3
      print("x is now:", x)
      

πŸ“Œ Notes

  • Explain that variables take the most recent value assigned.
  • The variable on the left side of = is set to the value on the right side.

🟣 Comparison Operators

Comparison operators compare two values: equal to, not equal to, less than, greater than, less than or equal to, and greater than or equal to. They help determine whether a condition is met.

🎯 Purpose

Understand how Python uses comparison operators to evaluate expressions.

πŸ“ Steps for Completion

  1. What type of value do comparisons return?
  2. In what sequence are comparisons evaluated?
  3. What happens if part of a comparison is false?
a = 10
b = 20
print(a == b)   # False
print(a < b)    # True

πŸ”΅ Logical Operators

Logical operators in Python include not, and, and or. These operators follow a specific order of precedence and are essential for controlling flow based on multiple conditions.

🎯 Purpose

Learn how logical operators influence the flow of logic in code.

πŸ“ Steps for Completion

  1. List the logical keywords in order of precedence:
    • a. and
    • b. or
    • c. not
  2. Review the code and answer the following:
    • What will happen when this code is run?

πŸ“Œ Notes

  • If time permits, share additional examples of logical operators in Python code.
x = 7
print(x > 5 and x < 10)  # True
print(not x == 7)        # False

Lesson 6: Input from the User

You can ask the user questions using the input() function. The user's response is stored as a string.

🎯 Learning Target

I can get and use input from a user in a program.

name = input("What is your name? ")
print("Hello, " + name + "!")

✏️ Your Turn

Ask the user their favorite color and print a message with their answer.

Arithmetic

Arithmetic has an order of operations when performing calculations. This order ensures that calculations are performed the same way every time to provide consistent results.

Purpose

Upon completing this project, you will better understand the order of operations.

Steps for Completion

  1. List the order of operations from first to last.
    • a. ()
    • b. **
    • c. *
    • d. / & //
    • e. +
    • f. -
  2. Open the 134-arithmetic.py file from your Domain 1 Student folder.
  3. Run the code. What is the result?

Project Details

Project file: 134-arithmetic.py

Estimated completion time: 10 minutes

Video reference: Domain 1 Topic: Sequence of Execution Subtopic: Arithmetic Order

Objectives covered:

Notes

You should completed projects and show an opening parenthesis added before the base variable and a closing parenthesis added after the bonus variable.

Steps for Completion (Continued)

  1. Edit the code so that the base and bonus variables are added together before being multiplied by the rank variable.
  2. Run the code. What is the result?
  3. Save the file as 134-arithmetic-completed.

Identity Operator

Two variables can appear equal, but one can use the identity operator to determine if they are truly the same. The identity operator determines if two variables share the same memory location.

Purpose

Upon completing this project, you will better understand the identity operator.

Steps for Completion

  1. The is keyword is used to identify whether two variables share the same memory space.
  2. In the order of operations, the identity operation happens before logical and assignment operations but after arithmetic and comparison operations.
  3. If two variables share the same memory space, they will have the same values.

Containment Operator

Containment is often used for lists, sets, tuples, and dictionaries. The containment operator checks whether a value is contained within a list of values.

Purpose

Upon completing this project, you will better understand the containment operator.

Steps for Completion

  1. Containment operations happen after identity operations.
  2. Review the code, then answer the question about the containment operator.

Project Details

Project file: N/A

Estimated completion time: 5 minutes

Video reference: Domain 1 Topic: Sequence of Execution Subtopic: Containment Order

Objectives covered:

Notes

Ensure You understand why one may want to use a containment operator.

Questions

Review 1.3

Purpose

Upon completing this project, you will better understand the order of operations across categories of operators and within a category of operators.

Steps for Completion

  1. What is the order for the six types of operators?
    • a. ()
    • b. x[index] & [index:index]
    • c. **
    • d. -x & +x //
    • e. str
    • f. %
  2. What supersedes all operators in the order of operations?
    • a. ()
  3. What is the order of preference for logical operators?
    • a. and
    • b. or
    • c. not
  4. What is the order of operations for arithmetic operators?
    • a. ()
    • b. **
    • c.
    • d.
    • e.
    • f.

Project Details

Project file: N/A

Estimated completion time: 15 minutes

Video reference: Domain 1 Topic: Sequence of Execution Subtopic: Containment Order; Review on 1.3

Objectives covered:

Notes

Make sure You understand not only the order of operations but how to analyze code using order of operations to either generate a result or see how a result was found.

Lesson 7: Integers from Input

You can turn input into a number using int(). This allows you to do math with it.

🎯 Learning Target

I can collect numbers from a user and use them in calculations.

age = int(input("How old are you? "))
print("Next year you’ll be " + str(age + 1))

✏️ Your Turn

Ask the user how many siblings they have. Then print double that number in a sentence.

Lesson 8: More Math

You can perform different operations like subtraction, multiplication, division, and remainders using -, *, /, and %.

🎯 Learning Target

I can perform and understand different types of math operations in Python.

apples = 9
friends = 2
print("Each friend gets", apples // friends, "apples")

✏️ Your Turn

Ask the user how many cookies they have and how many friends they’ll share with. Show how many each friend gets using division.

Lesson 9: Using Comments

Use # to add comments. These lines are ignored by Python but help humans understand the code.

🎯 Learning Target

I can use comments in my code to explain what it does.

# This line prints a greeting
print("Hello!")

✏️ Your Turn

Write a small program with at least two comments that explain what each part does.

Lesson 10: Syntax and Errors

Python will give an error if it doesn’t understand your code. These are called syntax errors.

🎯 Learning Target

I can recognize and fix syntax errors in Python code.

# Incorrect: missing parenthesis
print("Hello"
# Fixed version
print("Hello")

✏️ Your Turn

Fix this line of code with a syntax error:

print("What's up"

Lesson 11: Debugging

Sometimes your code runs but doesn’t do what you expect. Use print() to see what’s happening and find bugs.

🎯 Learning Target

I can use print statements to debug and fix code.

num = 10
# We expect this to print 20, but it prints 30!
print(num + 20)

✏️ Your Turn

Use print() to figure out what's wrong in a small buggy program. Then fix it.

Lesson 12: Order of Operations

Python follows PEMDAS rules for math: parentheses, exponents, multiplication/division, addition/subtraction.

🎯 Learning Target

I can use parentheses to control the order of operations in math expressions.

print(3 + 2 * 4)      # 11
print((3 + 2) * 4)    # 20

✏️ Your Turn

Write two print statements: one with parentheses and one without, using different math operations.

Lesson 13: Mini-Project

Let's combine everything we’ve learned into a short interactive project.

🎯 Learning Target

I can write a program that uses input, variables, math, and output.

name = input("What's your name? ")
birth_year = int(input("What year were you born? "))
age = 2025 - birth_year
print("Hi " + name + "! You are " + str(age) + " years old.")

✏️ Your Turn

Make your own short program! Use input, math, and strings to tell the user something fun or helpful.

Lesson 14: More Data Types and String Slicing

Learning Target: I can use booleans, floats, the modulus operator, and slice strings in Python.

Booleans

Booleans represent truth values: True or False. They are often used in conditions.

is_raining = True
print("Bring an umbrella:", is_raining)

Floats

Floats are numbers with decimal points.

price = 4.99
tax = 0.40
total = price + tax
print("Total:", total)

Modulus Operator

The modulus operator % returns the remainder of a division.

print(10 % 3)  # Output: 1
print(15 % 4)  # Output: 3

String Slicing

You can extract parts of a string using slicing.

word = "Python"
print(word[0:3])  # Output: Pyt
print(word[2:])   # Output: thon
print(word[-1])   # Output: n

Your Turn!

Try writing a program that:

  • Defines a float variable for a meal price
  • Calculates tax using a float
  • Prints whether the total is over \$10 using a boolean
  • Prints the first 3 letters of a string using slicing

Try it here: