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 =:
= 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
type() reports what a value is:
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:
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:
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:
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: ")putsCity: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 reading12gives you"12", not12.
Putting a variable inside a sentence
Two ways, both producing the same line:
+ 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:
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(). Withinput("City: ")your program's first output isCity:, which lands in front of your real first line and breaks the match. - Commas where one string was needed.
print("City:", city, ".")printsCity: 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.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…