Skip to content

Workshop: Variables and Data Types (Optional, Self-Study)

View workshop code on GitHub Get the Video Course

From the lesson

Workshop problems for Section 02 Concepts. Build each yourself first, then expand the solution to check.

Seven problems, one per step, using the same ideas as the lesson in a car scenario. Write the code yourself, then check python-workshop/solutions.

Step-01: Your First Program

Concepts: print().

Write a program that prints a three-line car intro: a title Car Intro, then Brand: Toyota, then Model: Camry.

Expected output
Car Intro
Brand: Toyota
Model: Camry
Need a hint?

Call print() once per line.

Show the solution
s02_workshop01_sol_first_program.py
print("Car Intro")
print("Brand: Toyota")
print("Model: Camry")

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-02: Comments

Concepts: comments (file comment, inline comment, commented-out line, triple-quote block).

Write a program that prints Car ready and uses each kind of comment: a file comment at the top, an inline comment on the print line, a commented-out line, and a triple-quote note. Only Car ready should appear in the output.

Expected output
Car ready
Need a hint?

A # starts a comment: on its own line, at the end of the print line, or in front of a line of code to switch it off. """...""" holds a note that spans several lines.

Show the solution
s02_workshop02_sol_comments.py
# Car Notes: prints a one-line status for the car
print("Car ready")  # inline comment at the end of a line
# print("Car not ready")
"""
Tires checked.
Oil changed.
"""

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-03: print() Options and Escape Sequences

Concepts: print() with sep and end, and the escapes \n and \t.

Problem-1

Write a program that prints Toyota, Camry, 2021 on one line, separated by |.

Expected output
Toyota | Camry | 2021
Need a hint?

Use sep=" | ".

Show the solution
s02_workshop03_sol_print_options.py
# Problem-1 Task-1: print three values separated by pipe
print("Toyota", "Camry", 2021, sep=" | ")

View the full solution file on GitHub

Problem-2

Write a program that prints Checking and ok on the same line, joined by ....

Expected output
Checking...ok
Need a hint?

print("Checking", end="...").

Show the solution
s02_workshop03_sol_print_options.py
# Problem-2 Task-1: join two words with ... on one line
print("Checking", end="...")
print("ok")

View the full solution file on GitHub

Problem-3

Write a program that prints a two-line table from a single string: Brand: then Toyota, and Year: then 2021, lined up with tabs.

Expected output
Brand:  Toyota
Year:   2021
Need a hint?

Use \t and \n inside one string.

Show the solution
s02_workshop03_sol_print_options.py
# Problem-3 Task-1: print a tab-aligned two-line table
print("Brand:\tToyota\nYear:\t2021")

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-04: Variables

Concepts: variables, print(), the sep option, naming rules and conventions.

Problem-1

Write a program that stores a car's brand (Toyota), model (Camry), and year (2021) in variables, then prints a spec card: the title Car Spec Card, each labeled detail on its own line, and finally the brand and model on one line joined by -.

Expected output
Car Spec Card
Brand: Toyota
Model: Camry
Year: 2021
Toyota - Camry
Need a hint?

Store brand, model, and year in variables, print each label with print("Brand:", brand), then join brand and model on the last line with sep=" - ".

Show the solution
s02_workshop04_sol_variables.py
# Problem-1 Task-1: store car details, print spec card
brand = "Toyota"
model = "Camry"
year = 2021

print("Car Spec Card")
print("Brand:", brand)
print("Model:", model)
print("Year:", year)

print(brand, model, sep=" - ")

View the full solution file on GitHub

Problem-2

Write a program that stores a car_brand (Toyota) and model_year (2021) in snake_case variables and a MAX_SPEED (180) as an UPPER_CASE constant, then prints all three.

Expected output
Toyota
2021
180
Need a hint?

Name the two variables in snake_case like car_brand and model_year, and the constant in UPPER_CASE like MAX_SPEED, then print() all three.

Show the solution
s02_workshop04_sol_variables.py
# Problem-2 Task-1: snake_case vars and UPPER_CASE constant, print all three
car_brand = "Toyota"
model_year = 2021
MAX_SPEED = 180

print(car_brand)
print(model_year)
print(MAX_SPEED)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-05: Data Types and type()

Concepts: data types (str, int, float, bool), type(x).__name__.

Write a program that stores a car's model (Tesla Model 3), seats (5), price (42999.50), and in_stock (True), one value of each core type. Print all four values, then print each value's type name.

Expected output
Tesla Model 3
5
42999.5
True
str
int
float
bool
Need a hint?

type(x).__name__ gives each value's type name. Python drops the trailing zero when it prints a decimal, so 42999.50 prints as 42999.5.

Show the solution
s02_workshop05_sol_data_types.py
model = "Tesla Model 3"
seats = 5
price = 42999.50
in_stock = True

print(model)
print(seats)
print(price)
print(in_stock)

print(type(model).__name__)
print(type(seats).__name__)
print(type(price).__name__)
print(type(in_stock).__name__)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-06: Type Conversion

Concepts: int(), float(), str().

Write a program that makes three conversions:

  • Task-1: the number of cars in a lot arrives as the text 8. Turn it into a whole number, add the 2 cars that just arrived, and print the new total.
  • Task-2: turn the text 1499.50 into a decimal and print it.
  • Task-3: build the text cars 8 by joining cars with the number 8, and print it.
Expected output
10
1499.5
cars 8
Need a hint?

int("8") gives a number you can add to; float() handles the decimal (Python drops the trailing zero when it prints it); str() turns a number into text so it can join other text.

Show the solution
s02_workshop06_sol_type_conversion.py
# Task-1: convert the text count to a number, add the new cars, print
cars = int("8")
print(cars + 2)

# Task-2: convert text to float and print
print(float("1499.50"))
# Task-3: join text with a number and print
print("cars " + str(cars))

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-07: Keywords

Concepts: the keyword module.

Write a program to check whether class and brand are Python keywords, and print each result in the form shown below.

Expected output
Is 'class' a keyword? True
Is 'brand' a keyword? False
Need a hint?

Start with import keyword. keyword.iskeyword() gives True or False for each word.

Show the solution
s02_workshop07_sol_keywords.py
import keyword

print("Is 'class' a keyword?", keyword.iskeyword("class"))
print("Is 'brand' a keyword?", keyword.iskeyword("brand"))

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Finished this section?

Check your work against the solutions above, then head back to the Section 02 lesson, or try the Section 02 practice for more reps.

Prefer to learn by watching?

The full video course teaches every concept on screen and builds the projects with you, step by step.

Get the Video Course

Next: Workshop: Strings and fstrings