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)Steve
Whole number (int)
No decimal point.
age = 24
print(age)24
Decimal number (float)
Has a decimal point.
angle = 31.8
print(angle)31.8
True or False (bool)
Only two options. Both start with a capital letter.
is_raining = True
print(is_raining)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))<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"])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']}.")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 name | Why | Instead of |
|---|---|---|
| grocery_list | Says what is in it. Use _ between words, since a name cannot have spaces. | my list |
| friends | A plural name tells you it is a list. | list1 |
| can_drive | Start a True or False name with is, can, has, or did. It reads like a question. | drive |
| paddle_price | One to three words. A long name is hard to read every time you use it. | how_much_the_paddle_costs |
| MAX_LIVES | All capitals means this value should never change. | max_lives |
| player2 | Numbers are fine, just not at the start. | 2player |
| top_score | Only 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.
| Method | What it does | Example |
|---|---|---|
| .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())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())becca
Capitalize the first letter
.capitalize() also lowercases everything after the first letter.
name = "bECCA"
print(name.capitalize())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)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)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)