Every Python journey starts with two things: print(), which shows text on the screen, and variables, which remember values for later. Learn these two and you can already write small programs.
PRINT(): SHOW SOMETHING
print("Hello, world!")
print(7 + 3)Hello, world!
10
Text goes inside quotes. Numbers and math don't need quotes, and Python calculates them first.
VARIABLES: GIVE A VALUE A NAME
A variable is like a labelled box. You put a value in with = and use the name later.
name = "Mia"
coins = 3
print(name, "has", coins, "coins")Mia has 3 coins
= as "gets". coins = 3 means "coins gets the value 3". It is not a question.CHANGING A VARIABLE
coins = 3
coins = coins + 5
print(coins)8
Python works out the right side first (3 + 5), then stores the answer back in coins. A shorter way to write the same thing is coins += 5.
THE MAIN TYPES OF VALUES
- Strings (text):
"snek",'hi' - Integers (whole numbers):
42,-7 - Floats (decimals):
3.5 - Booleans:
TrueorFalse
print(type("snek"))
print(type(42))
print(type(3.5))<class 'str'>
<class 'int'>
<class 'float'>
MATH
print(10 / 4)
print(10 // 4)
print(10 % 4)
print(2 ** 3)2.5
2
2
8
/ divides normally, // drops the decimals, % gives the remainder and ** is "to the power of".
F-STRINGS: TEXT WITH VALUES INSIDE
Put an f before the quotes and write variables in curly brackets. This is the easiest way to build sentences.
player = "Ivo"
score = 120
print(f"{player} scored {score} points!")Ivo scored 120 points!
a = 5
b = a
a = 10
print(b)b = a copies the value at that moment (5). Changing a later does not change b.COMMON BEGINNER MISTAKES
"Score: " + 5 is an error. Use an f-string, or str(5).print(hello) looks for a variable called hello. You probably meant print("hello").my score and my-score are not allowed. Use my_score.x = "3"
y = 4
print(x * y)x is the text "3", not the number 3. Multiplying text by 4 repeats it 4 times: 3333.PRACTICE IT, DON'T JUST READ IT
Print Plains is the first world in Snekcade: tiny lessons, real Python on your phone, and an explanation for every answer. Free, no account needed.
PLAY PRINT PLAINS ▸FAQ
What is a variable in Python?
A name that stores a value so you can use it later, for example score = 10. You can change it at any time.
What is an f-string?
Text with an f before the quotes, like f"Hi {name}". Anything in curly brackets is replaced by its value.
Why does "Score: " + 5 give an error?
You cannot add text and a number. Turn the number into text with str(5), or use an f-string.