A variable in Python is best understood as a name that points at a value — not a box that contains one. This distinction sounds pedantic the first time you hear it, and then becomes the thing that saves you from real confusion a few chapters from now, once two different names can point at the same value at the same time. For now, the practical consequence is simpler: a name is just a label, and Python does not care at all what you call something. It cares what the something is.Variables can be named just about anything, and often are named things that make you think about what you store there.
ratings = [4.5, 3.0, 5.0, 4.0]
cats = 3
cat_name = 'Fluffy'There are some rules about variable names.
-
All Variables must be named.
-
Names can contain letters, digits, and underscores
-
Names must begin with a letter
-
Names are case sensitive (y and Y are different variables)
-
Reserved words (like Python keywords) cannot be used as names
-
All variable names must be unique (no two alike)
So this means the following are fine for variable names in Python:
x
AREA
height
width
current_score
player_one
player_two
sum_of_credits
_last_play
is_player_aliveAnd uppercase and lowercase letters are different. So each of these are DIFFERENT variables even though they are based on the same word(s).
Current_Speed
current_speed
CURRENT_SPEEDSo be careful with UPPERCASE and lowercase letters in variable names.
We can declare a variable named 'cats' and assign it the value 3:
cats = 3
print(cats) # -> 3
dogs = 1
print(dogs) # -> 1
player1alive = true
carspeed = 55.6Variables are meant to be used and re-used throughout a program. You can set a variable by assigning it a new value. See how we use the age and name variables twice, for both James and Gina.
age = 19
name = "James"
print(name, "is", age, "years old") # -> James is 19 years old
age = 21
name = "Gina"
print(name, "is", age, "years old") # -> Gina is 21 years oldx = "five"
print(x) # -> five
x = "nineteen"
print(x) # -> nineteenNotice how we assign "nineteen" to x. We can assign (re-assign) to a variable as many times as we might need to.
age = 3
# have a birthday
age = age + 1
# have another birthday
age = age + 1
print(age); # -> 5Notice here how age’s current value is used, added one to it, and re-assigned back to the variable age.
Now, one of the weird (to me anyway) things Python can do is a variable to point to any kind of item, while the program is running. A lot of languages won’t you do this. But it can be handy in Python. In Python, variables are dynamic (can reference any data object) which means a variable can be a string now and later be a number.
height = 62.0 # inches maybe?
print(height) # -> 62
height = "very tall"
print(height) # -> very tall
# yep, first height is a number
# and then it's a string.You can’t see it, but I am slowly shaking my head in disbelief. Some day, maybe I’ll explain why. Or maybe you’ll explain it to me.
Constants are like variables but they contain values that SHOULD NOT change such as a person’s date of birth. Convention is to capitalize constant variable names.
DATE_OF_BIRTH = "04-02-2005"
PI = 3.14
GRAVITY = 9.8They are usually declared in a file named 'constants.py'.
Note: In reality, we don’t use constants in Python. (The language doesn’t have them.) Naming them in all capital letters is a convention to separate them from variables, however, it does not actually prevent reassignment. So be careful.
Python can keep track of a number of different kinds of data, and these are known as "data types". Here are a few of them.
-
Numeric - there are two kinds of numbers: integers, floats and complex
-
Whole Numbers - int are like 0, -4, 5, 6, 1234
-
Decimal Numbers - float are numbers where the decimals matter like 0.005, 1.7, 3.14159, -1600300.4329
-
Complex are, well, if you don’t already know, let’s ignore them. But, I should make a joke about how there are two kinds of number and then then show three kinds, and mumble something about an 'off-by-one' bug.
-
-
Strings - str - a string of characters -
-
like 'text' or "Hello, World!" or 'Wow, I really like Python!'
-
-
Boolean - bool - is either True or False
-
often used to decide things like is_player(1).alive() [True or False?]
-
-
None - no value at all (nil or null in other languages)
It is common for a computer language to want to know if data is a bunch numbers or text. Tracking what type a piece of data is is very important. And it is the programmer’s job to make sure all the data get handled in the right ways.
So Python has a few fundamental data types that it can handle. And we will cover each one in turn. Any variable can be handed to the type() builtin functions and it will tell you what type the variable references.
x = 42
print(type(x)) # -> <class 'int'>
r = 3.14
print(type(r)) # -> <class 'float'>
s = 'Hello, World!'
print(type(s)) # -> <class 'str'>|
Tip
|
Create variables for each builtin data type:
Store a value in each. |
# Here are some samples.
# integer
x = 0
# boolean
player_one_alive = True
# float
current_speed = 55.0
# string
player_one_name = 'Rocco'Now, you try it. Write down a variable name and assign a normal value to it.