# Key Built-in Math Functions in Python ## 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

#### 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:

Coding Is Your Crucial Professional Skill

Previous ArticleNext Article
{"ft_lang_label":"__label__en","ft_lang_prob":0.79340583,"math_prob":0.977095,"size":4791,"snap":"2021-43-2021-49","text_gpt3_token_len":1154,"char_repetition_ratio":0.12847295,"word_repetition_ratio":0.13856813,"special_character_ratio":0.26612398,"punctuation_ratio":0.13171759,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9958056,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-01T00:38:11Z\",\"WARC-Record-ID\":\"<urn:uuid:2576a95b-1692-40fa-bcc5-5e4970214825>\",\"Content-Length\":\"94300\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6f97a47b-33b2-405b-82c1-1546eaab9bef>\",\"WARC-Concurrent-To\":\"<urn:uuid:f6e45f1d-0598-4290-bb81-b381a86464a0>\",\"WARC-IP-Address\":\"172.67.160.124\",\"WARC-Target-URI\":\"https://cupertinotimes.com/key-built-in-math-functions-in-python/\",\"WARC-Payload-Digest\":\"sha1:2DYNO4VHEJ63MOXZJQAOSFQFHPYD67P7\",\"WARC-Block-Digest\":\"sha1:PL4PAWMVBW3BZFHRBEUPKSQHZMUADGNI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964359082.76_warc_CC-MAIN-20211130232232-20211201022232-00062.warc.gz\"}"}