= 10
x type(x)
int
2025-03-31
Justin Post
Next up, we look at two basic built-in data types: numeric types and booleans
As with lists and strings, we’ll go through and look at how to create them, common methods and functions, and look at some examples along the way.
Once we’re through this part, we’re ready to start thinking about compound data types (other than lists) and how we might store and summarize data. For dealing with data, we’ll look at two common python modules and their data types:
Note: These types of webpages are built from Jupyter notebooks (.ipynb
files). You can access your own versions of them by clicking here. It is highly recommended that you go through and run the notebooks yourself, modifying and rerunning things where you’d like!
int()
and float()
(33, 1)
math
modulemath
module has a number of useful functionsmath
module to gain access to its functions. We then preface functions/objects from the module with math.
Floats are not stored precisely!
Comes from using a binary representation of floats
Not worth getting into, but if you see something weird like this, that is why!
We saw how to format strings earlier. Let’s revit that year!
We can use the .format()
method on the string to place values in the placeholders. The years:d
above specifies the type of formatting to use on the number, d
stands for integer
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-41-7105395599c7> in <cell line: 1>() ----> 1 my_string.format(job = Job, years = Years) #throws an error as it expects an integer for years ValueError: Unknown format code 'd' for object of type 'float'
"I am a Professor and I've been teaching for 10 years"
We can specify the type of number input for the .format()
method to use via this name:number_type
syntax:
print("I am a {job} and I've been teaching for {years:f} years".format(job = Job, years = Years))
print("I am a {job} and I've been teaching for {years:.1f} years".format(job = Job, years = Years))
I am a Professor and I've been teaching for 10.230000 years
I am a Professor and I've been teaching for 10.2 years
Actually four different ways to substitute into a string (if you are interested!)
bool
is a True
or False
value (note the case of these two special terms)Many functions and methods return a boolean. For instance, the .is_integer()
method:
For strings (and other objects we’ll see) in
and not in
are useful operators that return a boolean
isInstance()
is a useful function to determine if an object is a certain type!
if/then/else
and looping
(covered soon!)bool()
function on anything to get a True
or False
True
except empty onesTrue
treated as 1False
treated as 0One thing of note is that when you do math on True
or False
it converts the result. Note the last computation result below.
This quick video shows some useful functions from the .math
module for dealing with integers, floats, and booleans. Remember to pop the video out into the full player.
The notebook written in the video is available here.
from IPython.display import IFrame
IFrame(src="https://ncsu.hosted.panopto.com/Panopto/Pages/Embed.aspx?id=d59f4288-1a98-446e-a82f-b0f0013e8445&autoplay=false&offerviewer=true&showtitle=true&showbrand=true&captions=false&interactivity=all", height="405", width="720")
Numbers stored as ints or floats
Booleans are True
or False
.is_*()
methods, bool()
function)If you are on the course website, use the table of contents on the left or the arrows at the bottom of this page to navigate to the next learning material!
If you are on Google Colab, head back to our course website for our next lesson!