6. Data in Python (labo)
6. Data in Python (labo)
There are different types of data in Python. Here, you will see how text and numbers are used
in Python.
Strings
Strings are used to store and manipulate text. As you see here, strings are written inside of
quotes and can contain letters, numbers, punctuation marks, and other special characters. Run
the following cells to print these four different strings.
Entrée [1]:
print("Hello, World")
Hello, World
Entrée [2]:
print("adrew is handsome")
adrew is handsome
Entrée [3]:
print("¯\_(ツ)_/¯")
¯\_(ツ)_/¯
Entrée [4]:
print("2.99")
2.99
Multiline strings
If you use triple quotation marks, you can store a multiline string. These strings can span
more than one line. When you run the following cell, you will see how the spaces in the
second line are actually read as characters for the string.
Entrée [11]:
print(""""Hello, World!,
It's great to be here!""")
"Hello, World!,
It's great to be here!
Trying to define a multiline string using single quotes will lead to errors.
Entrée [ ]:
print("Hello, World!
It's great to be here!")
The type() function
In Python, you can check the type of any data that you are using. To check the data type, you
can use the type() function. When you run the next cell, you will retrieve the type for the
string "Andrew".
Entrée [12]:
type("Andrew")
Out[12]:
str
Python returned str, which is short for string. Let's check the type for a multiline string:
Entrée [13]:
type("""
Numbers, text, and truth,
Strings, ints, and floats in our code,
Data shapes our path
""")
Out[13]:
str
This also returns str, since it is also a string. Let's try a number within quotation marks:
Entrée [14]:
type("2.99")
Out[14]:
str
This is also a string, even though it looks like a number. By using quotation marks you're
telling Python to treat it as text, rather than a number. What about a number without quotes?
Entrée [15]:
type(100)
Out[15]:
int
This time you get int, which is short for integer. Integers are the positive and negative whole
numbers, like 42, 100, -9, and 0. Since there are no quotes around the number Python assumes
this is numerical data, and since there is no decimal place on this number, it interprets it as an
integer. Now, let's try a number that does have decimal places:
Entrée [16]:
type(2.99)
Out[16]:
float
The function type gives float, which is the data type used to store floating point numbers.
Floating point numbers are positive and negative numbers that include a decimal place, like
3.14, 2.99, and -0.003.
Python as a calculator!
Python works great for quick arithmetic operations. For instance, if you had a lemonade stand,
and wanted to compute the total number of sales you made through the last 12 months, you
can use Python like this:
Entrée [18]:
print(28+35+43+50+65+70+68.5+66+75+80+95)
675.5
As another example, you can perform more advanced math, like computing the compound
interest after 10 years at a rate of 5%. To do that, you can compute 1.05 to the power of 10.
Not sure how to do it in Python? You can use the Chatbot!
Entrée [20]:
print(1.1**10)
2.5937424601000023
Order of operations
The order of operations in Python is the same as in arithmetic. First, you compute
parentheses, then exponents, then you multiply and divide (from left to right), and finally, you
add and subtract (from left to right).
So, if you are trying to convert from Fahrenheit to Celsius, the following cell will give you an
incorrect answer:
Entrée [21]:
print(75 - 32 * 5 / 9)
57.22222222222222
Whereas the computation in this cell is correct.
Entrée [22]:
print((75 - 32) * 5 / 9)
23.88888888888889
Try for yourself!
Try printing text with mixed numbers and letters, or just symbols, then check the type. Try
multiline strings using the triple quotes. If you make any mistakes, as the chatbot for help.
Entrée [25]:
print("geswgewgew")
geswgewgew
Entrée [26]:
Entrée [27]:
Entrée [30]: