We can change the value stored in the variable by just reassigning a new value to it.
1>>> price = 200
2>>> print(price)
3200
4>>> price = 300
5>>> print(price)
6300
7
Variables in Calculations
We can use variables in calculations instead of directly using values. Let's take an example. Let's say you have $100 in savings and you put it in a bank account that pays a simple interest of 6% per year. We can create a formula in Python that can help us calculate the simple interest you will earn on your savings.
1p = 100
2r = 6
3t = 2
4I = p *(r/100) * t
5print(I)
6
- p is the principal amount, $100.00.
- r is the interest rate, 6% per year, or in decimal form, 6/100=0.06.
- t is the time involved, 2 years time periods.
Save this script in your folder with the name simpleinterest.py
. Run the script on the command prompt using python simpleinterest.py
It will produce the following result:
1C:\pythondatascience>python simpleinterest.py
212.0
3
Common Data Types
The most common data types in Python are:
int
, or integer. These are positive or negative whole numbers with no decimal point
float
, or floating point. They are real numbers and are written with a decimal point between the integer and fractional parts.
str
, or string. A string holds any combination of letters and numbers you would like it to hold. Use double or single quotes to build strings.
bool
, or boolen. Python provides the boolean type that can be either set to False or True.
Below are a few examples:
1>>> course = "Financial Data Science" # String example
2>>> print (course)
3Financial Data Science
4>>> age = 7.5 # Float example
5>>> print(age)
67.5
7>>> score = 103 # Integer example
8>>> print(score)
9103
10>>> valuable = False # Boolen example (True or False)
11>>> print(valuable)
12False
13
You can check that these variables have the correct data type using the type()
function.
1>>> type(course)
2<class 'str'>
3>>> type(age)
4<class 'float'>
5>>> type(score)
6<class 'int'>
7>>> type(valuable)
8<class 'bool'>
9>>>
10
Operations on Data Types
We have already seen that we can perform math operations on integer variable. Python behaves differently when you perform these operations on other data types.
Adding Two Strings
If you try to add two strings, Python will concatenate the two strings as shown below:
1>>> the_text = "The " + "Quick " + "Brown " + "Fox"
2>>> print(the_text)
3The Quick Brown Fox
4
Adding Two Boolens
Internally, Python treats True
as 1 and False
as 0. So, if you perform any mathematical calculation on boolens, it will do the operation on their numeric counterpart.
1>>>True + True # Think 1 + 1
22
3>>>True + False # Think 1 + 0
41
5>>>False + False # Think 0 + 0
60
7
For Boolen variables, only the words True
and False
are recognized. Other variations, such as TRUE
, true
, or T
are incorrect.
Adding Variables of Different Types
What if we try to add variables of different types, for example, string and integer?
1>>> text = "My total portfolio value is: "
2>>> value = 5000
3print(text+value)
4
As you can guess, this is not possible and will throw an error. If we want to add these two things, we can convert value
to string type using the str()
function as shown below:
1>>> text+str(value)
2'My total portfolio value is: 5000'
3>>>
4
You can use the functions str(), int(), float() and bool() to convert Python values into any type.
Exercise
- Create the simple interest formula as we did in the above example, and present the results in a nicely formatted way, e.g., "I will earn an interest of 12".
- Create a
boolen
variable with value False
. Then convert it into a string, integer, and float and print the results. Notice the difference in the formats.