## Python

Python is one of the easiest languages to begin one’s coding journey with. It allows one to ramp up quickly with the basics and with it, one can go as far as complex technical ideas can take you. This is why Python has been the first choice for many scientists, especially in recent years. Thanks to Python, even scientists with limited coding experience have been able to write programs needed to aid their research.

This article aims to provide the reader with an improved understanding of some of the basic built-in math functions that come up time and again and are a must-have in the toolkit of a Python programmer. A useful resource for some of the other built-in features of Python can be found here: https://www.youngwonks.com/resources/python-cheatsheet.

### Datatype Conversion

#### 1. int () – Convert to Integer

This is a function that takes a string or a floating-point number. It converts the provided parameter into an integer. In the case of a floating-point number, it truncates the digits after the decimal point. A sign may also be provided along with the parameter.

Syntax: variable = int (parameter) Example: steps = int (‘12’)

steps = int (12.57)

#### 2. float () – Convert to Floating Point Number

This is a function that takes a string or an integer. It converts the provided parameter into a floating-point number with the same value. In the case of an integer, it truncates the digits after the decimal point.

Syntax: variable = float (parameter) Example: temp = int (’98.6’)

temp = int (98)

Number Systems

#### 3. bin () – Converts an integer to binary string

Binary numbers are very important when it comes to digital logic and other number systems. This function takes in an integer as an argument and returns a binary string prefixed with “0b” to indicate the base.

Syntax: variable = bin (parameter) Example: code = bin (5)

code = bin (-11)

#### 4. hex () – Converts an integer to a hexadecimal string

Hexadecimal numbers are useful for representing some very large numbers. It is often used when representing memory addresses. This function takes in an integer as an argument and returns a lowercase hexadecimal string prefixed with “0x” to indicate the base.

Syntax: variable = hex (parameter) Example: code = hex (5)

code = hex (-11)

#### 5. oct () – Converts an integer to octal string

Although not in use as much nowadays, octal numbers still crop up once in a while. This function takes in an integer as an argument and returns a lowercase octal string prefixed with “0o” to indicate the base.

Syntax: variable = oct (parameter) Example: code = oct (9)

code = oct (-15)

### Math Operations

#### 1. abs () – Absolute Value of a Number

#### 2. pow () – Exponent

This function is quite handy to calculate a number raised to the power of another number. It takes two parameters, one for the base and one for the power it should be raised to. The parameters can be either integer or floating-point numbers. It is very similar to using the ** operator.

Syntax: variable = pow (base, exponent) Example: planck = pow (10, -34)

electron = pow (10, -31)

Note: also similar to user the ** operator

#### 3. round () – Rounding Precision

This function provides us with a way to keep or discard digits after the decimal point. A built-in quick way to do it is quite useful. It rounds to the nearest integer by default, but you can specify the number digits after the decimal as a second argument.

#### 4. divmod ()-

This function is very useful when you need both the quotient and the remainder. This function takes two numbers as arguments and returns a pair of numbers consisting of their quotient and remainder.

Syntax: rem, quo = divmod (a, b) Example: c, d = divmod (25, 4)

c, d = divmod (-97, 7)

Note: the result is the same as (a // b, a % b)

#### 5. max () – Maximum

This function returns a maximum of 2 or more objects. Arguments can be integers or strings or even a list. When applied to strings, it uses alphabetical order to sort and calculate the maximum.

Syntax: variable = max (a, b) Example: bigger = max (25, 4)

variable = max (a, b, c…) bigger = max (‘pqtvbg’)

variable = max ( [ a , b ] ) biggest = max( [6,3,8,19,20 ])

#### 6. min () – Minimum

This function returns a minimum of 2 or more objects. Arguments can be integers or strings or even a list. When applied to strings, it uses alphabetical order to sort and calculate the minimum.

Syntax: variable = min (a, b) Example: smaller = max (25, 4)

variable = min (a, b, c…) smaller = min (‘pqtvbg’)

variable = min ([ a , b ] ) smallest = min ( [6,3,8,19,20 ])

#### 7. sum () – Sum

The sum function is very simple and does exactly what it seems like it should. It takes a list and returns the sum of its contents. In the case of joining strings, the ””. join() method is more suitable.

Syntax: variable = sum ([a, b, c….]) Example: total = sum ([2,6,-3,19.8])

**More on this topic:**