Variables, printing, & types#
Creating variables#
To create a variable, simply write the variable name of your choosing, followed by its definition (using =
).
Here we create the variable num_companies
which contains an integer, and avg_assets
which contains a float (i.e., decimal).
num_companies = 3
avg_assets = 19.1
How to name your variables#
You can name your variables whatever you would like. However, it it not a good idea to name your variables with all uppercase letters, e.g., PRICE
. There are situations where this could be problematic, so try to avoid it.
You also cannot have spaces in your variable names. Most people create variables with lowercase letters, and use underscores instead of spaces, e.g., interest_rate
.
Basic print statements#
You can print out the value of any variable you create by using the print()
function.
print(num_companies)
print(avg_assets)
3
19.1
You can also print many things at once if you separate them with commas:
print(num_companies, avg_assets)
3 19.1
Simple data types#
Numeric#
We have already seen two types of numeric variables: integers and floats. Integers are whole numbers, while floats are numbers with decimals.
num_companies = 3
avg_assets = 19.1
String#
Variables can also contain strings. A string is just a group of characters. The string must be wrapped in either single or double quotes — '
or "
. It does not matter unless the string itself contains a quote character (e.g., McDonald's
).
company1 = 'Microsoft'
company2 = 'Apple'
# notice the use of quotes here
company3 = "McDonald's"
If you have a long string that spans multiple lines, you can store it in a variable by wrapping the text in '''
like this:
my_long_string = '''this is my long string
that spans multiple lines
it just keeps going'''
print(my_long_string)
this is my long string
that spans multiple lines
it just keeps going
Boolean#
Boolean variables can be either True
or False
(capitalization matters here).
industry_tech = True
industry_retail = False
Note: industry_tech = true
would not work since true
is not capitalized. See the error below.
industry_tech = true
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 1
----> 1 industry_tech = true
NameError: name 'true' is not defined
Now let’s print out a couple of statements using our string and boolean variables.
print('The first company is', company1)
print('Second company:', company2)
print('Is it a tech company?', industry_tech)
The first company is Microsoft
Second company: Apple
Is it a tech company? True
You can identify the variable type with the type()
function.
print( type(company1) )
print( type(industry_retail) )
print( type(avg_assets) )
<class 'str'>
<class 'bool'>
<class 'float'>
None#
It is possible for a variable to be empty, or hold nothing. Python has a special type for this: None
.
nothing_var = None
type(nothing_var)
NoneType
Advanced printing with f strings#
f strings allow you to plug variables into strings.
Simply place an f
in front of the first quote.
Variables must be wrapped in curly braces.
For example:
print( f'{company1} is one of the {num_companies} companies.' )
Microsoft is one of the 3 companies.
Formatting numbers in f strings#
If the variable is numeric, you can format it in the string.
The formatting instructions go after the colon. See here for more details.
For example, the following will print out the variable with a “fixed” two decimal places.
pi_approx = 3.14159
print(f'Pi is about {pi_approx:.2f}')
Pi is about 3.14
You can also include commas with large numbers.
big_num = 1234567.8999123
print(f'The big num is {big_num:,}')
# combine commas and a fixed number of decimals
print(f'The big num is {big_num:,.1f}')
The big num is 1,234,567.8999123
The big num is 1,234,567.9
And use it for percentages.
rate_of_return = 0.0489263
print(f'Return = {rate_of_return:.2%}')
Return = 4.89%
We can also easily add leading zeros to numbers if we want them formatted that way.
my_number = 23
# print number with 5 characters and leadings zeros where appropriate
print(f'The number with leading zeros: {my_number:05}')
The number with leading zeros: 00023
To “print” or not#
In many cases we can just type the name of our variable and it will display. This is the case when we are working in an interactive environment.
company1
'Microsoft'
This works fine if we are only interested in one variable at a time. But notice the difference here.
In this first example, only the last variable is displayed.
company1
company2
company3
"McDonald's"
When we use print()
, all of the variables are displayed.
print(company1)
print(company2)
print(company3)
Microsoft
Apple
McDonald's
Without the print()
function, only the last variable is displayed. If you want to display multiple things, or display them in a formatted way, then you should use print()
.
Another example that highlights the difference in output:
my_long_string = '''this is my long string
that spans multiple lines
it just keeps going'''
my_long_string
'this is my long string\nthat spans multiple lines\nit just keeps going'
Notice, \n
is how newline characters are stored in a string.
print(my_long_string)
this is my long string
that spans multiple lines
it just keeps going