Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pierian-data

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: pierian-data/complete-python-3-bootcamp
Path: blob/master/17-Advanced Python Objects and Data Structures/01-Advanced Numbers.ipynb
Views: 648
Kernel: Python 3

Advanced Numbers

In this lecture we will learn about a few more representations of numbers in Python.

Hexadecimal

Using the function hex() you can convert numbers into a hexadecimal format:

hex(246)
'0xf6'
hex(512)
'0x200'

Binary

Using the function bin() you can convert numbers into their binary format.

bin(1234)
'0b10011010010'
bin(128)
'0b10000000'
bin(512)
'0b1000000000'

Exponentials

The function pow() takes two arguments, equivalent to x^y. With three arguments it is equivalent to (x^y)%z, but may be more efficient for long integers.

pow(3,4)
81
pow(3,4,5)
1

Absolute Value

The function abs() returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.

abs(-3.14)
3.14
abs(3)
3

Round

The function round() will round a number to a given precision in decimal digits (default 0 digits). It does not convert integers to floats.

round(3,2)
3
round(395,-2)
400
round(3.1415926535,2)
3.14

Python has a built-in math library that is also useful to play around with in case you are ever in need of some mathematical operations. Explore the documentation here!