In Python, variables are containers used to store data while the program is running. Variables can be named just about anything, and often are named things that make you think about what you store there.
Think of them as little boxes you can put data into, to keep it there and not lose it.
There are some rules about variables.
-
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
currentScore
playerOne
playerTwo
sumOfCredits
_lastPlay
isPlayerAliveAnd 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(cats) # -> 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 into the variable age.
Now, one of the weird (to me anyway) things Python can do is change the type of a variable 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 contain any data) which means a variable can be a string 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.
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
-
Integers are like 0, -4, 5, 6, 1234
-
Floats 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.
-
-
str - a string of characters -
-
like 'text' or "Hello, World!"
-
-
Boolean - is either True or False
-
often used to decide things like isPlayer(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.
|
Tip
|
Create variables for each primitive data type:
Store a value in each. |
# Here are some samples.
# integer
x = 0
# boolean
playerOneAlive = True
# float
currentSpeed = 55.0
# string
playerOneName = 'Rocco'Now, you try it. Write down a variable name and assign a normal value to it.