Skip to content
Home » PYTHON JUMPSTART COURSE Section 2 – Data Types and Operators, Lesson 2 – Numeric Types

PYTHON JUMPSTART COURSE Section 2 – Data Types and Operators, Lesson 2 – Numeric Types

Spread the love

Let’s start our exploration of data types in Python with the numeric types. Numbers are very important in Python.

Built-in Numeric Types:

There are three basic built-in numeric types:

– integers (ints)

– floating-point numbers (floats)

– complex numbers (not discussed here)

Let’s look at the first two types one at a time.

Integers

Integers are positive or negative whole numbers.

Integers can be of almost unlimited size. The only limit is the memory available in your computer.

Floating-Point Numbers

Floats are real numbers. They are written with a decimal point separating the integer part from the fractional part.

Example:

Numbers are widely used in arithmetic operations. Let’s explore the main operators we can use to this end.

Arithmetic Operators

Addition, subtraction and multiplication are pretty straightforward:

There are two kinds of division operators:

1) true division /

2) floor division //

In true division the result of dividing two integers is a float:

In floor division the result is truncated down, so the result is the floor – the largest integer number smaller than the result of the true division:

We use the ** operator for powers:

We use the  % character for the modulus operator which returns the remainder of a division:

The modulus operator is often used to check whether a given number is divisible by another number. It is if the remainder is 0:

There are lots of mathematical functions we can use for calculations.

Mathematical Functions

Python offers us quite a bunch of ready-made mathematical functions that we can use to perform all the basic mathematical operations:

abs(x) – the absolute value of x

The absolute value of x is the distance between x and 0. It’s always positive. abs(x) works with ints, floats and complex numbers:

max(x, y, z, ...) – the maximum value

This functions returns the greatest of its arguments:

min(x, y, z, ...) – the minimum value

This functions returns the smallest of its arguments:

round(x, n) – x rounded to n decimal places

The parameter n is optional. Its default value is 0, so round(x) has the same meaning as round(x, 0):

and with negative numbers:

Next, let’s have a look at how numbers can be assigned.

Assignment Operators

The basic assignment operator is ‘=’. It assigns the value of the operand on the right to the operand on the left:

Sometimes we need to change the value of a variable by performing an operation on it and assigning the result back to it:

In such cases we can use the augmented assignment statements which combine arithmetic operators with the basic assignment operator:

By analogy we have:

Here’s a summary:

assignment operators

Finally, let’s talk about random numbers for a while.

Random Numbers

We can use the random module to generate pseudo-random numbers.

First let’s import all the functions from this module:

Then we can use the random() function which generates a random float number between 0.0 and 1.0, the upper bound not being included:

To generate a random integer between x and y, we use the function randint(x, y). Both x and y are included. The number is different each time:

We use the uniform(x, y) function to generate a random floating point number between x and y:

QUIZ

1. Which of the following is a floating point number:
    A) -12
    B) 12
    C) 12.0
 
2. In true division the result of dividing two integers is:
    A) an integer
    B) a float
    C) a float or an integer if the first integer is divisible by the second
 
3. The exponentiation operator is:
    A) ^
    B) $
    C) **
 
4. The modulus operator is:
    A) mod
    B) %
    C) $
 
5. We use the … function to generate a random floating point number between x and y.
    A) random(x, y)
    B) randint(x, y)
    C) uniform(x, y)
 

TRUE OR FALSE?

1) Complex numbers is a built-in numeric type.
2) Floats are real numbers.
3) The exponent must be a positive integer.
4) The modulus returns the remainder of a division.
5) The modulus operator can be used to check whether a number is divisible by another number.
6) The abs(x) function returns the absolute value of a number.
7) The basic assignment operator is ‘==
8) Augmented assignment statements combine arithmetic operators with the basic assignment operator.
9) a *= b stands for a = a * b
10) The largest number the random() function can generate is 1.0.
11) The second argument of the function randint(x, y) is not included.

WHAT’S THE OUTPUT?

1)
>>> 6 / 3
2)
>>> 12 // 5
3)
>>> 16 ** 0.5
4)
>>> 20 % 6
5)
>>> if 10 % 3 >= 0:
...     print("x")
... else:
...     print("y")
...
6)
>>> max(4, 2, 7, 1) + min(-2, 3, 0, 9, 5, 8)
7)
>>> round(-6.218, 2)
8)
>>> a = 3
>>> a **= 3
>>> a
9)
>>> a, b = 15, 4
>>> a %= b
>>> a
10)
>>> a, b = 3, 5
>>> a += b
>>> b += a
>>> a + b
11)
>>> from random import *
>>> random() - 1 < 0
12)
>>> round(uniform(2.2114, 2.2365), 1)

SOLUTION

QUIZ

1. Which of the following is a floating point number:
    A) -12
    B) 12
    C) 12.0
 
2. In true division the result of dividing two integers is:
    A) an integer
    B) a float
    C) a float or an integer if the first integer is divisible by the second
 
3. The exponentiation operator is:
    A) ^
    B) $
    C) **
 
4. The modulus operator is:
    A) mod
    B) %
    C) $
 
5. We use the … function to generate a random floating point number between x and y.
    A) random(x, y)
    B) randint(x, y)
    C) uniform(x, y)
 

TRUE OR FALSE?

1) Complex numbers is a built-in numeric type. True
2) Floats are real numbers. True
3) The exponent must be a positive integer. False
4) The modulus returns the remainder of a division. True
5) The modulus operator can be used to check whether a number is divisible by another number. True
6) The abs(x) function returns the absolute value of a number. True
7) The basic assignment operator is ‘==False
8) Augmented assignment statements combine arithmetic operators with the basic assignment operator. True
9) a *= b stands for a = a * b True
10) The largest number the random() function can generate is 1.0. False
11) The second argument of the function randint(x, y) is not included. False

WHAT’S THE OUTPUT?

1)
>>> 6 / 3

Output:
2.0
2)
>>> 12 // 5

Output:
2
3)
>>> 16 ** 0.5

Output:
4.0
4)
>>> 20 % 6

Output:
2
5)
>>> if 10 % 3 >= 0:
...     print("x")
... else:
...     print("y")
...


Output:
x
6)
>>> max(4, 2, 7, 1) + min(-2, 3, 0, 9, 5, 8)

Output:
5
7)
>>> round(-6.218, 2)

Output:
-6.22
8)
>>> a = 3
>>> a **= 3
>>> a


Output:
27
9)
>>> a, b = 15, 4
>>> a %= b
>>> a


Output:
3
10)
>>> a, b = 3, 5
>>> a += b
>>> b += a
>>> a + b


Output:
21
11)
>>> from random import *
>>> random() - 1 < 0


Output:
True
12)
>>> round(uniform(2.2114, 2.2365), 1)

Output:
2.2

PROJECT

PROJECT SOLUTION


Spread the love

Leave a Reply