Logo

Numbers

The interpreter functions as a simple calculator: you can enter an expression and it will return the result. The expression syntax is simple: the operators +, -, *, and / function in the same way that they do in most other languages.

 

 

Integer numbers (– for example, 2, 4, 20) have the type int, while fractional numbers (such as, 5.0, 1.6) have the type float.

The division operator (/) always returns a float. To divide the floor and get an integer result (while discarding any fractional results), use the / operator; to calculate the remainder, use percent: Below demo shows the same.

 

 

The ** operator can be used to compute powers.

 

 

To assign a value to a variable, use the equal sign (=). Following that, no result is shown before the next interactive prompt:

 

 

If a variable is not "defined" (assigned a value), attempting to use it will result in the following error:

 

 

Floating point is fully supported; operators with mixed type operands convert the integer operand to floating point:

 

 

The last printed expression is assigned to the variable in interactive mode. This means that when using Python as a desk calculator, it is a little easier to continue calculations.

Python also supports numbers other than int and float, such as decimal and fraction. Python has furthermore built-in support for complex numbers, and the imaginary part is denoted by the j or J suffix (e.g. 3+5j).

 


________________________________________________________________________________________________________________________________
Footer
________________________________________________________________________________________________________________________________

Copyright © 2022-2023. Anoop Johny. All Rights Reserved.