Skip to main content

Python crash course: Numbers in python

Numbers in Python!



This is the first lecture of the python crash course series, In this article, we will discuss the following topics:


1.) Types of Numbers in Python

2.) Basic Arithmetic

3.) Differences between classic division and floor division

4.) Object Assignment in Python

Types of numbers

Python has three "types" of numbers (numeric literals), namely

  • integer

  • floating-point

  • complex

But we will mainly focus on integers and floating-point numbers. Because they are the most commonly used types. We will discuss the complex one later in the course.

Integers are 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


1.) 2.0 and -2.1 are examples of floating-point numbers.

2.) 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:

The following slides will contain a walkthrough the numbers in python with some notebook cells.

Basic Arithmetic

In [1]:

'''Addition'''

5 + 7

Out[1]:

12


In [2]:

'''Subtraction'''

7 - 5

Out[2]:

2


In [3]:

'''Multiplication'''

5 * 7

Out[3]:

35


In [4]:

'''Division'''

7 / 5

Out[4]:

1.4


In [5]:

'''Floor Division'''

7 // 5

Out[5]:

1


you must be wondering, What just happened? Last time I divided, 7 by 5 I got 1.4 not 1!

The reason we get this result is that we are using "floor" division or we can say integer division. The "//" operator (two forward slashes) returns the integer part of the division, or we can say it returns the floor value of the division result.

Now the next question is that what if we need the remainder part of the division


In [6]:

'''Modulo'''

7 % 5

Out[6]:

2


When we divide 7 by 5, 5 goes in 7 once and we get the remainder of the division as 2. The "%" operator returns the remainder after division.

Arithmetic continued

In [7]:

'''Power 5'''

7 **5

Out[7]:

16807


In [8]:

'''square root'''

4 **0.5

Out[8]:

2.0


In [9]:

'''Order of Operations followed in Python'''

2 + 10 * 10 + 3

Out[9]:

105


In [10]:

'''Controlling the flow of the operations using the parenthesis 

operators.'''

(2+10) * (10+3)

Out[10]:

156

Variable Assignments

The below slides will introduce you to the variable assignment in python. To assign values to the variables in python we use a single equal sign.

eg- a = 5 Don’t miss understand it with a double equal sign, many programmer do this mistake in programming.

We will also discuss the reassignment of the variables in python.

In [11]:

'''Let's create an object called "a" and assign it the number 5'''

a = 7


Now if I call an in my Python script, Python will treat it as the number 7.

In [12]:

'''Adding the objects'''

a+a

Out[12]:

14

Now let’s talk about reassignment, let see how we can deal with reassignment in python?

In [13]:

'''Reassignment'''

a = 5


In [14]:

'''Check'''

a

Out[14]:

5


Python allows the programmer to overwrite the values in the variables during the program. You can do some operations in the variables and then reassign the result of the operation in the same variable.

Check out the below cells.

In [15]:

'''Check'''

a

Out[15]:

5


In [16]:

'''perform addition in a and a and then assign the result in a'''

a = a + a


In [17]:

a

Out[17]:

10

Every time we create a variable in python we need to keep this fact in mind, that the variable naming in python also needs some rules:

1. Names can not start with a number.

2. There can be no spaces in the name, use _ instead.

3. Can't use any of these symbols:'",<>/?|\()!@#$%^&*~-+

4. It's considered best practice (PEP8) that names are lowercase.

5. Avoid using the characters 'l' (lowercase letter el), 'O' (uppercase letter oh),

   or 'I' (uppercase letter eye) as single-character variable names.

6. Avoid using words that have special meaning in Python like "list" and "str"

 

Giving meaningful names to the variables during assignment can solve a lot of complications as it increases the readability of the program

For example

In [18]:

# Use object names to keep better track of what's going on in your code!

my_salary = 100


tax_rate = 0.1


my_taxes = my_salary*tax_rate


In [19]:

# Show my taxes!

my_taxes

Out[19]:

10.0


What we have learned overall:

We have learned the numbers in python (int, float). The operations in those numbers, the flow of the operations. After that, we learned about the variables in python, how variables are used, what should be the rules to give a name to the variable.

In the next article, we will cover strings! Please ask your queries and report our mistakes in the comment section.

Comments

  1. I loved your python string and number post. These are essential for every programmer.

    ReplyDelete

Post a Comment

RUNTIME