Numbers - zamaniamin/python GitHub Wiki

There are three numeric types in Python: int float complex. Variables of numeric types are created when you assign a value to them:

x = 1  # int
y = 2.8  # float
z = 1j  # complex

Int

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. they cant start with zero: 015

The _ mean: 31_000_000 = 31000000 . It's purely there for my sake, my human sake, not for python’s sake.

whole_number = (1)
whole_number2 = 1
whole_number3 = 11_05
positive_number = 11
negative_number = -11

Float

Float, or "floating point number" is a number, positive or negative, containing one or more decimals.

float_number = 1.10
float_number2 = 1_500.55
float_number3 = 1.23e+15
float_number4 = 1.23 - 45
float_number5 = -.2
float_number6 = +2.1
float_number7 = 0.0
negative_float = -3.5

Float can also be scientific numbers with an e to indicate the power of 10.

float_number8 = 35e3
float_number9 = 87.7e100

Complex

Complex numbers are written with a "j" as the imaginary part:

complex_number = 3 + 5j
complex_number2 = 5j
complex_number3 = -5j

Type Conversion

You can convert from one type to another with the int() , float() , and complex() methods:

Note: You cannot convert complex numbers into another number type.

Convert from one type to another:

int_num = 1  # int
float_num = 2.8  # float
complex_num = 1j  # complex

# convert from int to float:
a = float(int_num)

# convert from float to int:
b = int(float_num)

# convert from int to complex:
c = complex(int_num)

Random Number

Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers.

Import the random module, and display a random number between 1 and 9:

import random

print(random.randrange(1, 10))