Min menu

Pages

Numbers in Python

 Numbers in Python

In the beginning, numbers meannumbersin English.
In this lesson, you will learn about types of numbers and ready-made functions in Python to work with them.

Numerology in Python Initially, numbers mean numbers in the English language. In this lesson, you will learn about the types of numbers and ready-made functions in Python to work with.

Technical information on setup in Python

Numerical variables in Python are considered allImmutable, This means that when you define any variable and give it a numerical value, this value reserves a place in memory for it.
But if you give a new value to this variable, it will delete the old value from memory and create a new place in memory and put the new value in it because the same value cannot be modified in the same place in memory.

Technical information

 Numerical variables in Python are all considered immutable, and this means that when you define any variable and give it a scalar value, this value is reserved for it in the memory. But if you give a new value to this variable, it will delete the old value from memory and create a new place in memory and put the new value in it because the same value cannot be modified in the same place in memory.

   Types of numbers in Python

When defining a variable and storing a number in it, the Python interpreter will automatically determine the type of that variable based on the type of scalar value that was assigned to it.
If you put an integer in it, it becomes its type int. And if you put a decimal in it,(i.e. contains a comma), becomes his type floatand so on.


Number types in Python are divided into 3 types as shown in the following table.

Type use it Example
int Used to store integers. x = 3
float Used to store numbers containing a decimal point. x = 1.5
complex It is used to store complex numbers(Complex Number)Which is often needed by engineers when performing complex computing operations.
Note: Here you must put the letter Jor jimmediately after the number so that the Python interpreter knows that you mean a complex number and not a regular number.
x = 4J

In the following example, we have defined three variables, and each variable has a different numerical value in type and value. Then we presented the type of each variable of them.

Example

Test.py
x = 3 # whose value is an integer, x here we have defined a variable named
		  y = 1.5 # Its value is a decimal number, y here we have defined a variable named
		  z = 4J # Its value is a complex number, z here we have defined a variable named
	  
		  print(type(x)) # x Here we have printed the value type of the variable
		  print(type(y)) # y Here we have printed the value type of the variable
		  print(type(z)) # z Here we have printed the value type of the variable
	

We will get the following result when running.

<class 'int'>
<class 'float'>
<class 'complex'>

How to display integers in different forms in Python

In Python you can memorize integers asHexa-Decimaland in the form ofOctal.
Note: You do not have to understand the following code because displaying numbers in different forms you learn in a subject called Computer Architecture and in Networking.

How to display integers in different forms in Python In Python you can save both Hexa-Decimal and Octal integers. Note: You do not have to understand the following code, because displaying numbers in different forms is learned in a subject called computer architecture and in the course of networks.

Example

Test.py
a = 0xA03 # Hexa-decimal whose value is an integer but we wrote it as a, here we have defined a variable named
		  b = 0o42 # Octal Its value is an integer but we wrote it as b, here we have defined a variable named
	  
		  print('a =', a) # a Here we have printed the value of the variable
		  print('b =', b) # b Here we have printed the value of the variable
	  
		  print('type of a is:', type(a)) # a Here we have printed the type of the value of the variable
		  print('type of b is:', type(b)) # b Here we have printed the type of the value of the variable
	

We will get the following result when running.

a = 2563
b = 34
type of a is: <class 'int'>
type of b is: <class 'int'>

We note that when printing the values ​​of the two variables a, bthey are displayed as normal and understandable integers.

We also note that the values ​​of the two variables were considered a, which bare integers int.

   Convert functions of number types in Python

The following table contains ready-made functions in Python that are used to convert number types and to specify the types of numbers that we want to store inside variables.

What are the conversion functions of number types in Python? The following table contains ready-made functions in Python that are used to convert the types of numbers and to define the types of numbers that we want to store inside the variables.

function with its definition
int( x ) Returns the value of the number we pass to the place of the variable xas an integer type int.
int( x, base ) Returns the value of the text we pass in the place of the variable xas an integer type int.
The place of the variable basewe pass a number that represents the way we want to convert the value of the variable x.
float( x ) Returns the value of the variable that we pass it to the place of the variable as a xdecimal number (i.e. contains a comma) of its type float.
complex( real, imag ) Returns the value of the variable realand the variable imagas a complex number(Complex Number).
A complex number by its nature contains two values:
The first is the value of the variable as the realreal value(Real Part).
The second is the value of the variable imagas an imaginary value(Imaginary Part).
Note: If you pass a single value to it, here you are actually entering the real value of the complex number, and the dummy value is 0 .

  Detailed examples of number type conversion functions in Python Read on "

 Functions for working with numbers in Python

In the following table, we mentioned some ready-made functions in Python that are used to deal with numbers.

What are the functions dealing with numbers in Python? In the following table, we have mentioned some ready-made functions in Python that are used to deal with numbers.

function with its definition
abs( x ) Returns the absolute value of the number we pass to the place of the parameter x.
round( x [, n] ) Returns the closest integer to the number we pass to it in place of the parameter x.
max( x1, x2, ... ) Returns the largest number of the set of numbers we pass to it asArguments.
min( x1, x2, ... ) Returns the smallest number of the set of numbers we pass to it asArguments.

Detailed examples of functions for working with numbers in Python Read on "

Meaning of the word Modulein Python

In Python, the wordModuleIt means a normal Python file that can contain variables, functions, classes, etc..
Let's call this file(i.e. theModule)In our program we simply do it importand thus we are able to access the things in it and use them in our program.

a wordModuleYou write a model in Arabic and we will adopt it in the explanations.

Meaning of the word Module in Python In Python, the word "Module" means a normal Python file that can contain variables, functions, classes, etc. Let's call this file (ie the Module) in our program, simply we do import for it and thus we will be able to access the things in it and use them in our program. The word "Module" is written in the Arabic language and we will adopt it in the explanations.

note

We will explain how to createModuleAnd deal with it in detail in a special lesson later in this session.
In this course, you will also learn about manyModulesThose in Python, and in this lesson we will learn about two of them, namely mathand random.

Note 

We will explain how to create a Module and deal with it in detail in a special lesson later in this course. Also, in this course, you will learn about many of the Modules in Python, and in this lesson we will learn about two of them, math and random.

What are model functions and constants mathin Python?

mathIt is a ready-made model in Python that contains functions used in arithmetic and engineering operations, some of them are mentioned in this table.

What are the math functions and constants in Python? math is a ready-made template in Python that contains functions used in mathematical and engineering operations, some of which are mentioned in this table.

function with its definition
ceil( x ) Returns the integer greater or equal to the number we pass to it in the place of the parameter x.
floor( x ) Returns the integer smaller or equal to the number we pass to it in the place of the parameter x.
pow(x, y) Returns the result of doubling the value of the number we pass to it in the place of the parameter, xa multiple of the value of the parameter y.
sqrt( x ) Returns the value of the squared islands(square root)For the number that we pass it has the place of the parameter x.
exp( x ) Returns the value of .exponentialFor the number that we pass it has the place of the parameter x.
log( x [,base] ) Returns the value of .LogarithmThe number that we pass has the place of the parameter xwith the possibility of specifying the type of unit for this number.
log2( x ) Returns the value of .Logarithm Base 2For the number that we pass it has the place of the parameter x.
This function can be called instead of calling the function log(x, 2).
log10( x ) Returns the value of .Logarithm Base 10For the number that we pass it has the place of the parameter x.
This function can be called instead of calling the function log(x, 10).
sin( x ) Returns the value of .sinFor the number that we pass it has the place of the parameter x.
cos( x ) Returns the value of .cosineFor the number that we pass it has the place of the parameter x.
tan( x ) Returns the value of .TangentFor the number that we pass it has the place of the parameter x.
asin( x ) Returns the value of .arc sinFor the number that we pass it has the place of the parameter x.
acos( x ) Returns the value of .arc cosineFor the number that we pass it has the place of the parameter x.
atan( x ) Returns the value of .arc tangentFor the number that we pass it has the place of the parameter x.
degrees( x ) Converts the value of the number we pass into the place of the parameter xtoDegrees.
radians( x ) Converts the value of the number we pass into the place of the parameter xtoRadians.
gcd( x, y ) Returns an integer representing the greatest common divisor(Greatest Common Divisor)Between the two numbers we pass it has the place of the parameters xand y.

Detailed examples of math functions in Python Read on "



The model mathalso contains the following constants which are related to mathematics.

fixed name tariff
E contains the value ofExponential.
PI contains the value ofPI.

  What are the model functions randomin Python?

randomIt is a ready-made model in Python that contains functions used in arithmetic and engineering operations, some of them are mentioned in this table.

What are the random model functions in Python Random is a ready-made model in Python that contains functions used in mathematical and engineering operations, some of which are mentioned in this table.

function with its definition
random() Returns a random decimal between 0.0 and 1.0 .
uniform( a, b ) Returns a random decimal between the value we pass in the place of the parameter aand the value we pass in the place of the parameter b.
randrange ( [start,] stop [,step] ) Returns a random integer between 0 and the value we pass to have the parameter's place stop.
Or it returns a random integer between the value we pass has the place of the parameter startand the value we pass has the place of the parameter stop.
choice( seq ) Returns a random value from among the elements of any string(Sequence)We pass it on to her.
The string can be an array of numbers, or plain text (ie a string of characters).
For example, if we pass it an array of numbers, it returns a random number from within this array. And if we pass a text to it, it returns a random character from within that text.
shuffle( x ) We pass an array of the parameter 's position xto it, and it randomly switches the positions of its elements.

Detailed examples of the random model handling functions in Python Read on "