Home › Learn › Python › First steps

Variables

Lesson 2 of 40. Store values under names and use them later.

A variable is a name for a value. name = "Ada" stores the text; from then on name means that text. There is no declaration keyword: the first assignment creates the variable, and assigning again replaces the value.

Every value has a type. Text is a str, whole numbers are int, numbers with a decimal point are float, and True or False are bool. Python works out the type from what you write; type(x) tells you what it decided.

Names can contain letters, digits, and underscores, cannot start with a digit, and are case-sensitive. The convention is lower-case words joined by underscores, like total_price.

city = "Lisbon"
population = 545000
city = "Porto"   # the name now points at a different value
print(city, population)

Your task

Create three variables: name holding any text, age holding a whole number, and height_m holding a number with a decimal point (your height in meters, or anything).

Then print the sentence <name> is <age>, for example Ada is 36, using the variables rather than typing the values again.

One more, for later: PUNCTUATION holding the text .,!?;:. This course ends in a text statistics project, and a few lessons along the way build its parts; this constant is the first, the characters the project strips from the ends of words.

Part of the final project. PUNCTUATION is the set of characters stripped from the ends of words before they are counted. It is saved in this browser and loaded into Project: text statistics when you get there.

Hint 1

Text needs quotes; numbers do not. A float is a number written with a decimal point, like 1.75.

Hint 2

print() takes several values separated by commas and puts a space between them: print(name, "is", age).

Show the solution
name = "Ada"
age = 36
height_m = 1.7
PUNCTUATION = ".,!?;:"
print(name, "is", age)

    Runs CPython compiled to WebAssembly in this tab (12 MB download the first time, then cached); nothing is sent anywhere.