Variables in Python
In Python, a variable is a case-sensitive name that can hold a value. A variable can hold all kinds of values such as numbers, strings, etc. Below is an example of a variable storing a number.
>>> price = 200 >>> print(price) 200
The above code creates a variable called
price and assigns the integer value
200 to it. We can ask Python to tell us what is stored in the variable
price using the
We can change the value stored in the variable by just reassigning a new value to it.
>>> price = 200 >>> print(price) 200 >>> price = 300 >>> print(price) 300
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.
p = 100 r = 6 t = 2 I = p *(r/100) * t print(I)
- 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
It will produce the following result:
C:\pythondatascience>python simpleinterest.py 12.0
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:
>>> course = "Financial Data Science" # String example >>> print (course) Financial Data Science >>> age = 7.5 # Float example >>> print(age) 7.5 >>> score = 103 # Integer example >>> print(score) 103 >>> valuable = False # Boolen example (True or False) >>> print(valuable) False
You can check that these variables have the correct data type using the
>>> type(course) <class 'str'> >>> type(age) <class 'float'> >>> type(score) <class 'int'> >>> type(valuable) <class 'bool'> >>>
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:
>>> the_text = "The " + "Quick " + "Brown " + "Fox" >>> print(the_text) The Quick Brown Fox
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.
>>>True + True # Think 1 + 1 2 >>>True + False # Think 1 + 0 1 >>>False + False # Think 0 + 0 0
For Boolen variables, only the words
False are recognized. Other variations, such as
T are incorrect.
Adding Variables of Different Types
What if we try to add variables of different types, for example, string and integer?
>>> text = "My total portfolio value is: " >>> value = 5000 print(text+value)
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:
>>> text+str(value) 'My total portfolio value is: 5000' >>>
You can use the functions str(), int(), float() and bool() to convert Python values into any type.
- 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
boolenvariable with value
False. Then convert it into a string, integer, and float and print the results. Notice the difference in the formats.