## Python Data type : Numbers

** **Python is designed to be an easy-to-use and easy-to-read programming language. Consequently, it’s also relatively easy to learn, which is part of why it’s so popular.

Comments will be used throughout the code, which can be included in a line after the # character like this: # this is a comment . We can also create multi-line comments with three quotation marks, like so:

"""

multi-line

comments

can be done like this

"""

These multi-line comments are actually multi-line strings that we simply don’t assign to a variable.

**Numbers
**When we think of what to use programming for, one of the first things
that comes to mind is math. A lot of what we do with Python for data science is
math, so understanding how to use numbers in Python is crucial. Most of the
time, we only care about two types of numbers in Python: integers and floats.
Integers are whole numbers

without a decimal place, such as 2. Floats have a decimal place, such as 2.0. We can check the type of an object in Python with the function type() , such as type(2) , which outputs int . With numbers, we can perform the usual math operations: addition, subtraction, multiplication, and division, as shown here:

2 + 2# addition2 – 2

# subtraction2 * 2

# multiplication2 / 2

# division2 // 2

# integer division

The last line shows the integer
division operator, // .
This rounds the division result down to the nearest integer, also known as
floor division. The division operator, /, is a “float” division, which

returns a number as a float (with a decimal value).

We can exponentiate numbers, such
as 23, which means 2 * 2 * 2. For example, to raise 2 to the 3rd power as we
just showed, we do:

2 ** 3

If we wanted to take a square root of a number, one way is to
raise the number to 0.5 as an exponent, such as 2 ** 0.5 .

There is also a special operator,
% , called the **modulo operator**.
This returns the remainder of integer division. For example, if we integer divide
5 by 2, we get an answer of 2 but have a remainder of 1. We can perform modulo
calculations like so:

5 % 2# this outputs 1

*
*Lastly, we can round a number in a few ways. One easy way is simply
to convert a number to an integer with int() . For example,int(5.1) returns 5. This will always return the value rounded down to the
nearest integer. If we wanted, we can also convert integers to floats with float().

There is a round()
function, which rounds a number to the nearest integer by default.
This function also has an argument, ndigits , which allows us to round a number out to a certain number of decimal
digits. We will cover arguments shortly, but know that for now, these are
parameters we specify when calling a function such

as round() . For example, to
round 2.11 to 2, we can use the built-in function round
, such as round(2.11) , while to round 2.11 to 2.1, we can use round(2.11,
ndigits=1). We will cover the use of
functions in

more depth soon.

Within Python, there are several built-in modules and packages that add functionality. One that relates to numbers is the math module, which has various mathematical functions and constants. For example, if we want to get the value for the constant pi, we can use the math module:

import math

math.pi

The line import math loads the math module, and the math.pi line returns the value of pi (roughly 3.14) from that module.

A few new features of the math module were added in Python 3.9, such as math.lcm() for the least common multiple of numbers; for example:

math.lcm(2, 3, 5)# least common multiple

*
*This returns 30, since that is the least common multiple of 2, 3,
and 5.

## No responses yet