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.
Need a hint?
Call print() once per line.
Show the solution
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.
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
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 |.
Need a hint?
Use sep=" | ".
Show the solution
Problem-2
Write a program that prints Checking and ok on the same line, joined by ....
Need a hint?
print("Checking", end="...").
Show the solution
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.
Need a hint?
Use \t and \n inside one string.
Show the solution
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 -.
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
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.
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
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.
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
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 the2cars that just arrived, and print the new total. - Task-2: turn the text
1499.50into a decimal and print it. - Task-3: build the text
cars 8by joiningcarswith the number8, and print it.
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
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.
Need a hint?
Start with import keyword. keyword.iskeyword() gives True or False for each word.
Show the solution
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.