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.

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Python

  1. The Python Language
  2. How to Install Python on Your PC
  3. How to Write a Program in Python
  4. How to Use Python in Interactive Mode
  5. Variables
  6. Numbers
  7. Logical Operators
  8. Iterative Structures (or Loops)
  9. Conditional Structures
  10. Exceptions
  11. Files in Python
  12. Classes
  13. Modules

Miscellaneous

Source