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.


You can also print many things at once if you separate them with commas:

print(num_companies, avg_assets)
3 19.1

Data types#

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'''

this is my long string
that spans multiple lines
it just keeps going

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 [7], 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'>

Advanced printing — f strings#

f strings allow you to plug variables into strings.
Simply put 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.

If the variable is numeric, you can format it in the string.
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 in the 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%

To “print” or not#

In many cases we can just type the name of our variable and it will display:


This works fine if we are only interested in one variable at a time. But notice the difference here:


Without the print() function, only the last variable was displayed. If you want to display multiple things, or display them in a formatted way, then you should use print(). Otherwise just typing the name of the variable is probably fine.

Another example that highlights the difference in output:

my_long_string = '''this is my long string
that spans multiple lines
it just keeps going'''

'this is my long string\nthat spans multiple lines\nit just keeps going'

Here, \n is how newline characters are stored in a string.

this is my long string
that spans multiple lines
it just keeps going