Skip to content

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.

Expected output
About Me
Name: Kalyan
Learning: Python
Need a hint?

Call print() once for each line, with the text in quotes.

Show the solution
s02_practice01_sol_first_program.py
print("About Me")
print("Name: Kalyan")
print("Learning: Python")

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 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.

Expected output
Hello
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
s02_practice02_sol_comments.py
# Hello program
print("Hello")  # this is the hello program
# print("Goodbye")
"""
Line1
Line2
Line3
"""

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 red, green, blue on one line, separated by ,.

Expected output
red, green, blue
Need a hint?

Pass sep=", " to print().

Show the solution
s02_practice03_sol_print_options.py
# Problem-1 Task-1: print three colors separated by comma
print("red", "green", "blue", sep=", ")

View the full solution file on GitHub

Problem-2

Write a program that prints Loading and done on the same line, joined by ... (no line break between them).

Expected output
Loading...done
Need a hint?

print("Loading", end="...") keeps the next print on the same line.

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

View the full solution file on GitHub

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.

Expected output
Name:   Kalyan
City:   Hyderabad
Need a hint?

Use \t for a tab and \n for a new line, all inside one string.

Show the solution
s02_practice03_sol_print_options.py
# Problem-3 Task-1: print a tab-aligned two-line table
print("Name:\tKalyan\nCity:\tHyderabad")

View the full solution file on GitHub

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.

Expected output
C:\Temp\data
She said "Hi"
Need a hint?

Escape a backslash as \\ and a double quote as \".

Show the solution
s02_practice03_sol_print_options.py
# Problem-4 Task-1: print escaped path and quoted text
print("C:\\Temp\\data")
print("She said \"Hi\"")

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, 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 -.

Expected output
Intro Card
Name: Kalyan
City: Hyderabad

Kalyan - Hyderabad
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
s02_practice04_sol_variables.py
# Problem-1 Task-1: store name and city, print intro card
name = "Kalyan"
city = "Hyderabad"

print("Intro Card")
print("Name:", name)
print("City:", city)

print()
print(name, city, sep=" - ")

View the full solution file on GitHub

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.

Expected output
Python for Beginners
50
5
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
s02_practice04_sol_variables.py
# Problem-2 Task-1: snake_case vars and UPPER_CASE constant, print all three
course_title = "Python for Beginners"
lesson_count = 50
MAX_RATING = 5

print(course_title)
print(lesson_count)
print(MAX_RATING)

View the full solution file on GitHub

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.

Expected output
30
31
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
s02_practice04_sol_variables.py
# Problem-3 Task-1: store an age, change it after a birthday, print both
age = 30
print(age)
age = 31
print(age)

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()

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).

Expected output
Python Crash Notes
96
14.99
True
str
int
float
bool
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
s02_practice05_sol_data_types.py
# 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__)

View the full solution file on GitHub

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).
Expected output
12.35
12.3
12
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
s02_practice05_sol_data_types.py
amount = 12.3456
# Problem-2 Task-1: print rounded to 2 decimals
print(round(amount, 2))
# Problem-2 Task-2: print rounded to 1 decimal
print(round(amount, 1))
# Problem-2 Task-3: print rounded to a whole number
print(round(amount))

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

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 15 into a whole number, add 10, and print the result.
  • Task-2: turn the text 3.5 into a decimal number and print it.
  • Task-3: build the text age 30 by joining age with the number 30 and print it.
Expected output
25
3.5
age 30
Need a hint?

int("15"), float("3.5"), and str(30) convert between text and numbers.

Show the solution
s02_practice06_sol_type_conversion.py
# Problem-1 Task-1: convert text to int, add 10, print
quantity = int("15")
print(quantity + 10)
# Problem-1 Task-2: convert text to float and print
print(float("3.5"))
# Problem-1 Task-3: join text with a number and print
print("age " + str(30))

View the full solution file on GitHub

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.)

Example run
Enter the first number: 10
Enter the second number: 20
30
Need a hint?

input() returns text, so wrap each value in int() before adding.

Show the solution
s02_practice06_sol_type_conversion.py
# Problem-2 Task-1: read two numbers and print their sum
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print(a + b)

View the full solution file on GitHub

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.

Example run
Enter your name: Kalyan
Enter your age: 30
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
s02_practice06_sol_type_conversion.py
# Problem-3 Task-1: read name and age, greet with next-year age
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hi " + name + ", next year you will be " + str(age + 1))

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 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.

Expected output
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
s02_practice07_sol_keywords.py
import keyword

print("Total hard keywords:", len(keyword.kwlist))
print("Is 'if' a keyword?", keyword.iskeyword("if"))
print("Is 'class' a keyword?", keyword.iskeyword("class"))
print("Is 'course' a keyword?", keyword.iskeyword("course"))

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 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.

Get the Video Course

Next: Practice: Strings and fstrings