Understand Why We Need Variable Types
You looked at different types of variables in the previous chapter, but there is much more to know about the subject!
The types encountered so far are called primitive types. They exist in Python—a bit like atoms. These are the simplest types of variables; they are the foundation of all computer operations and programs. In the same way that atoms can be combined to make more complex molecules, you can combine primitive types to create much more complex variable types, as you will see in the next parts of this course. For now, you will explore numeric types and strings in a little more depth. Let's go!
Numeric Variables
Numeric variables can be broken down into two distinct types:
Integers, which correspond to the set of positive or negative integers (1, 2, 0, 123, -3, etc.)
Decimals, which, in addition to integers, include all decimal numbers (2.50, 5.99, -1.20, etc.)
Start with the one you are already familiar with: integers. Integers are declared like any other variable, by associating a value to a variable name.
account = 10
Here you have the value 10 associated with the variable account
. 10 being an integer, account
is automatically an integer variable (int
).
For decimal numbers
, python uses the float
type. You can define it in the same way as integers, by simply adding the decimal point explicitly:
length = 1876.79
width = 870.0
As long as the associated value is a decimal number, Python will automatically consider the variable as beingfloat
. This is true even if the digit after the decimal point is a 0, as is the case above with the width variable.
Mix Several Numeric Variables
It is important to keep in mind how the different numeric types can be mixed together and what the potential consequences are. If you mix different types, the most complex will be the one kept for the final result. For example, an integer value can be stored as a float
, as seen above with the width variable, but the opposite is not possible if there are numbers after the decimal point! The float
is therefore the most complex type: if you mix an int
with a float
, the result will always be a float
, whatever operation is performed or whatever the result is.
a = 7.5
b = 3
c = a/b
print(c)
# this will print 2.5, which is a float
If the result of an operation between two integers is supposed to be a decimal number, Python will automatically convert it to a float. Moreover, division (even if the result is supposed to be an integer) will necessarily return a float as well:
a = 10
b = 5
c = a/b
print(c)
# it's a float
However, you can force the conversion of a variable into a well-defined type. This is called typecasting, because by doing so you are changing (casting) the type of a variable. To do this, you will need the corresponding functions:
int()
: for integersfloat()
: for decimals
a = 14.0
# a is a float
a = int(a)
print(a)
# a is now an integer: it prints 14 and not 14.0
Try it for Yourself
Try to transform the type of our variable in the following exercise.
You can find the solution right here.
Character Strings
You will now explore a little more about character strings, which let you store text in your variables. First, a bit of semantics: we call them character strings, because Python does not consider these variables as text, as such, but as a set of characters put together. This is how you can define character strings in Python (you can use either single or double quotes):
city = 'New York'
film = 'taxi driver'
emptyString = ''
city = 'New York'
film = 'taxi driver'
emptyString = ''
Character strings are string
types in Python.
Assembling several strings
together is one of the most common operations you will have to perform when using strings
—his operation is called concatenation. See how to do this in Python:
favoriteCityOne = "San Francisco"
favoriteCityTwo = "New York"
favorites = favoriteCityOne + favoriteCityTwo
print(favorites) # => "San FranciscoNew York"
Note that there is no space between the two. Make your code more readable by concatenating your variables with other strings:
favoriteCityOne = "San Francisco"
favoriteCityTwo = "New York"
favorites = "My favorite cities are " + favoriteCityOne + " and "+ favoriteCityTwo
print(favorites) # -> "My favorite cities are San Francisco and New York"
It's much better this way, isn't it? However, you cannot concatenate other types of variables with strings, such as numeric variables---this would return an error. To remedy this, you will need to cast your numeric variable to a string, via the str()
function:
city = "Sydney"
numberTrips = 5
history = "I've been to " + city + " " + str(numberTrips) + " times "
print(history) # => "I've been to Sydney 5 times"
Let's Recap
In this chapter, you have encountered three primitive types of variables, essential to all programs/analyses:
Integers (int)
Decimals (float)
Character strings (string)
You have also seen how to use these different types:
You can perform numeric operations on numeric variables of different types.
You can cast variables to force the transformation of the type of a variable into another specific type, according to your needs.
Strings can be assembled together: this is called concatenation.
In the next chapter you will see how to write and use functions. Let's go!