Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/00-Python Object and Data Structure Basics/01-Numbers.ipynb
Views: 648
Numbers and more in Python!
In this lecture, we will learn about numbers in Python and how to use them.
We'll learn about the following topics:
Types of numbers
Python has various "types" of numbers (numeric literals). We'll mainly focus on integers and floating point numbers.
Integers are just whole numbers, positive or negative. For example: 2 and -2 are examples of integers.
Floating point numbers in Python are notable because they have a decimal point in them, or use an exponential (e) to define the number. For example 2.0 and -2.1 are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is also an example of a floating point number in Python.
Throughout this course we will be mainly working with integers or simple float number types.
Here is a table of the two main types we will spend most of our time working with some examples:
Examples | Number "Type" |
---|
Now let's start with some basic arithmetic.
Basic Arithmetic
Whoa! What just happened? Last time I checked, 7 divided by 4 equals 1.75 not 1!
The reason we get this result is because we are using "floor" division. The // operator (two forward slashes) truncates the decimal without rounding, and returns an integer result.
So what if we just want the remainder after division?
4 goes into 7 once, with a remainder of 3. The % operator returns the remainder after division.
Arithmetic continued
Variable Assignments
Now that we've seen how to use numbers in Python as a calculator let's see how we can assign names and create variables.
We use a single equals sign to assign labels to variables. Let's see a few examples of how we can do this.
Now if I call a in my Python script, Python will treat it as the number 5.
What happens on reassignment? Will Python let us write it over?
Yes! Python allows you to write over assigned variable names. We can also use the variables themselves when doing the reassignment. Here is an example of what I mean:
The names you use when creating these labels need to follow a few rules:
Using variable names can be a very useful way to keep track of different variables in Python. For example:
So what have we learned? We learned some of the basics of numbers in Python. We also learned how to do arithmetic and use Python as a basic calculator. We then wrapped it up with learning about Variable Assignment in Python.
Up next we'll learn about Strings!