Using number variables correctly in Python

ViscousPotential
Analytics Vidhya
Published in
3 min readJan 7, 2021

This is part four in a series discussing everything required to get an all-encompassing grasp of the Python programming language in as short a time as possible. Whether you are a beginner or an expert, I intend for you to learn something new. I will not be going into details on packages that need to be downloaded and so everything will be vanilla Python.

Numbers in python are written mostly as expected. Integers, or whole numbers, are written exactly as normal and floating point, or decimal numbers, are written with a normal period as the decimal point. Floating point numbers also support scientific notation in python:

1.11 x 101

In python this notation is represented by the number before the multiplication sign being written as normal, followed by the letter “e” followed by the index of the 10. This can be seen in the example code, if you require more reference.

Python also has support for complex, binary, octal and hexadecimal numbers. Complex numbers are written with the complex part being followed by a “j”.

Binary, octal and hexadecimal numbers are each written with their own respective prefixes, which are as follows:

Binary — 0b

Octal — 0o

Hexadecimal — 0x

Basic Operations

Python, by default, supports the +, -, / and * operators. If you have never come across those last two operators, the forward slash means division and the asterisk means multiplication. Using a double asterisk, on the other hand, simply means to the power of. The example above is exactly equivalent to the mathematical notation:

10³

The last two operators are related to each other. In mathematics, when two integers do not divide by one another to create a whole number result (e.g: 10 / 3), the result is considered to consist of two parts, the quotient and the remainder. The quotient is always a whole number and is the number of times the divisor goes into the dividend. The remainder can be represented in a number of ways, sometimes shown as a decimal, fraction or also even as a whole number remainder. The first of the two operators returns the quotient of a division and the second returns the remainder as a whole number. In the above example:

10 // 3 gives the quotient of 10 / 3
= 3
and
10 % 3 gives the remainder of 10 / 3
= 1

Inbuilt functions

Python only has a few inbuilt functions regarding numbers and they are very simple to use.

abs() will most likely be the most commonly used out of these and simply, returns an absolute value of the input. The example would return the following:

2

pow() does the exact same thing as the double asterisk operator and simply puts the first input to the power of the second. The example would return the following:

8

round() takes the first input and rounds it to as many decimal places as specified in the second input. The example would return the following:

2.6

Math Module Functions

The math module in Python has almost 50 inbuilt functions and so there is little point in learning them all. Here are a few that I tend to use more often than the rest.

.ceil() and .floor() simply take the given number and round it up or down, respectively, to the nearest integer. The example would return the following:

12
11

.radians() and .degrees() are also very simple, in that they take the input and convert it from degrees to radians and vice versa, respectively. The example would return the following:

0.19373154697137057
635.9831525952137

.sin(), .cos() and .tan() apply the sin, cos and tan functions to the input values respectively. .asin(), .acos() and .atan() apply the arc sin, arc cos and arc tan functions to the input values respectively. The example would return the following:

-0.9945525882039892
0.1001674211615598
0.10423602686569687
1.4706289056333368
-9.541351662275295
0.09966865249116204

The next part in the series will be about List Variables

Originally published at https://kgdavidson.co.uk on January 07, 2021.

--

--

ViscousPotential
Analytics Vidhya

A developer and electronics enthusiast wanting to share his experiences