Variables

Store a value now, use it later.

The four kinds of value

Python figures out the kind for you. You just pick a name and use =.

Text (str)

Anything inside quotes.

name = "Steve"
print(name)
Output
Steve

Whole number (int)

No decimal point.

age = 24
print(age)
Output
24

Decimal number (float)

Has a decimal point.

angle = 31.8
print(angle)
Output
31.8

True or False (bool)

Only two options. Both start with a capital letter.

is_raining = True
print(is_raining)
Output
True

Check the kind with type()

type() tells you what kind of value a variable holds. Handy when your math will not work.

age = 24
height = 1.78
name = "Steve"
is_valid = True
print(type(age))
print(type(height))
print(type(name))
print(type(is_valid))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

A dictionary: values with labels

A list keeps values in order. A dictionary gives each value a label, called a key, and you look the value up by its key.

Make a dictionary

Curly braces. Each key, then a colon, then its value. Put the key in square brackets to get the value.

person = {"name": "Steve", "age": 34}
print(person["name"])
print(person["age"])
Output
Steve
34

One key per line

Same dictionary, easier to read when there are a lot of keys.

person = {
    "name": "Steve",
    "age": 34
}
print(f"{person['name']} is {person['age']}.")
Output
Steve is 34.

Naming variables

Some names on the right break Python: a space, a number at the start, or a symbol like a dash. The rest work, but make your code harder to read.

Good nameWhyInstead of
grocery_listSays what is in it. Use _ between words, since a name cannot have spaces.my list
friendsA plural name tells you it is a list.list1
can_driveStart a True or False name with is, can, has, or did. It reads like a question.drive
paddle_priceOne to three words. A long name is hard to read every time you use it.how_much_the_paddle_costs
MAX_LIVESAll capitals means this value should never change.max_lives
player2Numbers are fine, just not at the start.2player
top_scoreOnly letters, numbers, and _. No other symbols.top-score

Changing capitalization

These hand back a new piece of text. The variable itself does not change unless you store the result.

MethodWhat it doesExample
.upper()Every letter capital"becca".upper() gives "BECCA"
.lower()Every letter lowercase"BECCA".lower() gives "becca"
.capitalize()First letter capital, the rest lowercase"bECCA".capitalize() gives "Becca"

Make it all capitals

.upper() shouts the whole thing.

name = "becca"
print(name.upper())
Output
BECCA

Make it all lowercase

.lower() is the one you reach for most, because it makes two pieces of text easy to compare.

name = "BECCA"
print(name.lower())
Output
becca

Capitalize the first letter

.capitalize() also lowercases everything after the first letter.

name = "bECCA"
print(name.capitalize())
Output
Becca

Keep the new version

Store the result in a variable, or use it right where you need it.

name = "becca"
loud = name.upper()
print(loud)
Output
BECCA

Forgetting to store it

name.upper() on its own throws the new text away. name is still the way it was.

name = "becca"
name.upper()
print(name)
Output
becca

Comments

Leave a note with #

Start the line with # and Python ignores it.

# This line is a note for humans. Python skips it.
score = 0
print(score)
Output
0