Practice: Variables and Data Types¶
View practice code on GitHub Get the Video Course
From the lesson
Practice problems for Section 02 Concepts. Build each yourself first, then expand the solution to check.
Seven practice problems, one per concept step. The numbers line up (Practice-01 goes with Step-01). Read the task, write the program yourself, then check python-practice/solutions. Some problems are short input() "Write a Program" exercises.
Step-01: Your First Program¶
Concepts: print().
Write a program that prints a small "About Me" card for Kalyan, who is learning Python. It has three lines: a title, the name, and what is being learned.
Need a hint?
Call print() once for each line, with the text in quotes.
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 Hello and, along the way, uses each kind of comment: a file comment at the top, an inline comment on the Hello line, a commented-out line, and a triple-quote block that hides three lines (Line1, Line2, Line3) without using #. Only Hello should appear in the output.
Need a hint?
A # starts a comment: on its own line, at the end of a code line, or in front of a line of code to switch it off. Wrap several lines in """...""" to switch them all off at once.
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 red, green, blue on one line, separated by ,.
Need a hint?
Pass sep=", " to print().
Show the solution
Problem-2
Write a program that prints Loading and done on the same line, joined by ... (no line break between them).
Need a hint?
print("Loading", end="...") keeps the next print on the same line.
Show the solution
Problem-3
Write a program that prints a two-line table from a single string: Name: then Kalyan, and City: then Hyderabad, with the values lined up by a tab.
Need a hint?
Use \t for a tab and \n for a new line, all inside one string.
Show the solution
Problem-4
Write a program that prints the Windows path C:\Temp\data on one line and She said "Hi" on the next. You will need to escape the backslashes and the quotes.
Need a hint?
Escape a backslash as \\ and a double quote as \".
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, changing a variable's value, print() with several values and sep, naming rules and conventions.
Problem-1
Write a program that stores a name (Kalyan) and a city (Hyderabad) in variables, then prints an intro card: the title Intro Card, the labeled name and city, a blank line, and finally the name and city on one line joined by -.
Need a hint?
Store the name and city in variables, print the labeled lines, use an empty print() for the blank line, then pass sep=" - " to join the name and city on the last line.
Show the solution
Problem-2
Write a program that stores a course title and lesson count in snake_case variables (course_title = Python for Beginners, lesson_count = 50) and a maximum rating as an UPPER_CASE constant (MAX_RATING = 5), then prints all three.
Need a hint?
Use snake_case names like course_title and lesson_count for the variables and an UPPER_CASE name like MAX_RATING for the constant, then print() each one.
Show the solution
Problem-3
Write a program that stores an age of 30 and prints it, then changes age to 31 after a birthday and prints it again.
Need a hint?
Give the same name a new value with = (age = 31); the old value is replaced, so the next print() shows 31.
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()¶
Capitalization matters
Python's booleans are True and False with a capital first letter. Lowercase true / false are not valid.
Concepts: the four data types (str, int, float, bool), type(x).__name__.
Problem-1
Write a program that stores a book's title (Python Crash Notes), pages (96), price (14.99), and in_stock (True), one value of each core type. Print all four values, then print each value's type name (in the same order).
Need a hint?
type(x).__name__ gives each value's type name; print the type names in the same order as the values.
Show the solution
# Problem-1 Task-1: store four core-type values, print values then type names
title = "Python Crash Notes"
pages = 96
price = 14.99
in_stock = True
print(title)
print(pages)
print(price)
print(in_stock)
print(type(title).__name__)
print(type(pages).__name__)
print(type(price).__name__)
print(type(in_stock).__name__)
Problem-2
Write a program that stores amount = 12.3456 and:
- Task-1: print it rounded to 2 decimal places.
- Task-2: print it rounded to 1 decimal place.
- Task-3: print it rounded to a whole number (no decimals).
Need a hint?
round(amount, 2) rounds to 2 places, round(amount, 1) to 1; round(amount) with no second argument rounds to a whole number.
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¶
int() is strict
int() only accepts text that is a whole number. int("3.5") or int("ten") raises a ValueError.
Concepts: int(), float(), str(), input().
Problem-1
Write a program that makes three conversions:
- Task-1: turn the text
15into a whole number, add10, and print the result. - Task-2: turn the text
3.5into a decimal number and print it. - Task-3: build the text
age 30by joiningagewith the number30and print it.
Need a hint?
int("15"), float("3.5"), and str(30) convert between text and numbers.
Show the solution
Problem-2 (Write a Program)
Write a program that asks the user for two numbers and prints their sum. (Remember: what the user types comes in as text, so convert it before adding.)
Need a hint?
input() returns text, so wrap each value in int() before adding.
Show the solution
Problem-3 (Write a Program)
Write a program that asks for a name and an age, then greets the user and tells them how old they will be next year, for example Hi Kalyan, next year you will be 31.
Need a hint?
Read the age as text, int() it, add 1, then str() it to join into the message.
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 that prints how many hard keywords Python has, then checks and prints whether if, class, and course are Python keywords. Print each line in the form shown below.
Total hard keywords: 35
Is 'if' a keyword? True
Is 'class' a keyword? True
Is 'course' a keyword? False
Need a hint?
Start with import keyword. Use len(keyword.kwlist) for the total count, then call keyword.iskeyword("if") on each word to get the True/False checks.
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 workshop 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.