Numbers in Python
Using numbers in Python is quite similar to other programming languages.
Mathematical Operators
Here are the primary mathematical operators in Python:
addition: a + b
subtraction: a - b
multiplication: a * b
division: a / b
exponentiation: a ** b
How to Calculate Roots
To calculate roots, use the exponentiation operator with a fractional exponent.
Example
The square root of a number can be found by raising it to the power of 0.5 (or 1/2).
a ** 0.5
Decimal Numbers
In Python, numerical variables are typically floating-point decimal numbers.
However, they can also be represented in other number systems (binary, hexadecimal, octal).
Binary Numbers
To assign a binary number to a variable, use the prefix 0b before the binary digits.
Example
print(0b111)
This command will display the number 7, which is the decimal equivalent of the binary number 111.
Octal Numbers
To work with octal numbers, use the prefix 0o before the octal digits.
Example
print(0o77)
This command will print the decimal value 63, which corresponds to 77 in octal.
Hexadecimal Numbers
To use hexadecimal numbers, use the prefix 0x before the hexadecimal digits.
Example
print(0xAA)
This command will display the decimal number 170, which is represented as AA in hexadecimal.