Python Working With Numbers¶
Python has four basic number types. 1. Integers(int), 2. Floating point numbers(float), 3. Long integers(long), 4. Complex Numbers(complex). All these numeric types are immutable data types, it means changing the value of a numeric data type will results in a newly creation of an object.
- An integer is a whole number (not a fractional number) that can be positive, negative, or zero.
- Examples of integers are: -5, 1, 5, 8, 97, and 3,043 etc.
- Working with integers in python
Floating point numbers(float)
- These are real numbers and are written with a decimal point dividing the integer and fractional parts.
- Examples of floating point numbers are 1.223, -123.55, 0.123, 125.66 etc.
- These are just like integers the only difference is long type integer has unlimited size.
- "int" can only maintain upto 9223372036854775807 but long can store number with any length of size.
- Examples: 9223372036854775807323424, 9223372036854775807323424324342342490, etc.
- These are a real numbres of the form "a + bj", where "a" and "b" are real numbers, and "j" represents the unit imaginary numbers equal to the positive square root of -1.
- Examples: 5 + 8j, -10 + 10j , -10 - 10.5j, etc.
Summery of operations¶
|x + y||sum of x and y|
|x - y||difference of x and y|
|x * y||product of x and y|
|x / y||quotient of x and y|
|x // y||(floored) quotient of x and y|
|x % y||remainder of x / y|
|abs(x)||absolute value or magnitude of x|
|int(x)||x converted to integer|
|long(x)||x converted to long integer|
|float(x)||x converted to floating point|
|complex(re,im)||a complex number with real part re, imaginary part im. im defaults to zero.|
|c.conjugate()||conjugate of the complex number c|
|divmod(x, y)||the pair (x // y, x % y)|
|pow(x, y)||x to the power y|
|x ** y||x to the power y|