Skip to content
Lesson 2 of 13

Step 1 of 8 · Reading · ~4 min

Learn

Getting Started

Variables and Data Types

A variable is a name your program attaches to a value so it can reach that value later. By the end of this lesson you can create variables, tell Python's four everyday types apart, read a line of input into one, and drop variables into a sentence you print.

Creating a variable

You create a variable by assigning a value to a name with a single =:

python

= is the assignment operator. Read it left to right: the name on the left now refers to the value on the right. From that line onward, wherever you write city, Python uses "Oslo".

You never declare a type: Python takes it from the value you assigned, which is called dynamic typing.

The four everyday types

python

type() reports what a value is:

python

Look at the third line: 12 and "12" look identical on screen but are different types, and that difference decides what you may do with them.

Reassignment

A name is not locked to its first value:

python

A variable always shows its most recent assignment. The old value is gone.

Naming

Names are letters, digits and underscores, never starting with a digit: depth, _depth and depth2 are fine, 2depth is a SyntaxError. They are case sensitive, so Depth and depth are two variables. Keywords such as if, for and None are off limits, and so are spaces and hyphens: sea-depth reads as subtraction.

The convention Python programmers follow is snake_case, lowercase words joined by underscores:

python

Pick names that say what the value means: p = 4.75 says nothing, sea_depth = 4.75 says everything.

Getting a value from outside the program

input() reads one line from the program's input and hands it back:

python

Three rules catch almost everyone:

  • One call reads one line. Two values means two separate calls, in the order the lines arrive.
  • Call it with empty parentheses. Anything you put inside is printed as a prompt, so input("City: ") puts City: at the front of your output and an exactly-compared exercise fails.
  • What comes back is always a str, even when the line is all digits. A line reading 12 gives you "12", not 12.

Putting a variable inside a sentence

Two ways, both producing the same line:

python

+ joins strings to strings only. An f-string is a string with an f in front of the opening quote, where each {...} is replaced by that variable's value.

A third form looks equivalent and is not:

python

print glues its arguments together with a space of its own, so a stray space lands before the final .. Handy while debugging, wrong whenever the output is compared character by character.

Common mistakes

  • A prompt inside input(). With input("City: ") your program's first output is City: , which lands in front of your real first line and breaks the match.
  • Commas where one string was needed. print("City:", city, ".") prints City: Oslo ., with an extra space before the period.
  • One variable for two input lines. If both input() calls assign to the same name, the first line is lost and the second value shows up where the first belonged.
  • Copying the sample output into the program. Printing the literal example text matches the example and nothing else; the hidden tests use other names and ids.

Your exercise

Build the two-line library-card record shown below from the two input lines; the starter reads both lines and prints a placeholder you replace. The likeliest slip is letting print insert its own spaces instead of building each line as one string.

Up nextWorking with NumbersGetting Started

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…