Lesson 5: Functions, Numbers and Libraries

Learn python programming in 12 lessons


Functions in any programming language are used to remove duplications which simplifies code and make it pleasant to read. For example in the AddThreeNums.py program from the previous post, you would have noticed that we kept asking the user to enter a number three times. We could replace those lines with a function instead that is invoked each time we want a new number.

Functions in Python are identified by a keyword def followed by the name of the function which should not be a reserved word. Code that belong to the function should be indented so that they lie inside def.
The function to ask a user for input can be defined as follow


def getNumber():
    return input(“Enter a number ”)

This function would then be placed at the top of the program before the part where we ask for the user to enter the number.
The new program would then look like this


def getNumber():
    return input("Enter a number: ")

num1 = getNumber()
num2 = getNumber()
num3 = getNumber()

total = eval(num1) + eval(num2) + eval(num3)
average = total/3

print("\nTotal:", total)
print("Average:", average)

You might have noticed that when creating variables in Python, you do not declare the type such as integer, float, string or list. Python seems to accept what ever literal you assign to the variable. One variable can go from holding an integer to holding a list or a string all without changing the variable type. This is because Python is a dynamically typed language which means that a variable is nothing more but a value bound to a name. To illustrate this phenomenon, take a look at this:

>>> x = "strer"

>>> type(x)

<class 'str'>

>>> x = 4

>>> type(x)

<class 'int'>

>>> x = 4.3

>>> type(x)

<class 'float'>

>>> x = {2,4,5}

>>> type(x)

<class 'set'>

>>> x = [1,2,3]

>>> type(x)

<class 'list'>

>>> x = (3,4,5)

>>> type(x)

<class 'tuple'>

>>> x = (1+3j)

>>>type(x)

<class ‘complex'



As you can see, the variable x is not restricted to any one type and this is because Python is a dynamically typed language.

The function type() is used to tell what type of data is stored in the variable.

There are two types of numeric data types that are accepted by Python. One is an integer (no decimal) and the other is a float (has decimal). Floats are only an approximation of the real number it represents and this is because floating point take up a large portion of the memory and they are usually truncated after the 16th decimal point.


>>> import math

>>> math.pi

3.141592653589793

Because of this, numeric data types are handled differently by some some arithmetic operators such as the division. There is one division (/) that gives a floating point answer and the other division (//) strictly gives an integer ignoring the decimal.


>>> 2/3

0.6666666666666666

>>> 2//3

0

>>> 12/5

2.4

>>> 12//5

2

>>> 14/3

4.666666666666667

>>> 14//3

4

Familiarize your self with Python's numeric operators

Take note that the exponent operator for python is ** and not ^


>>> 2^3

1

>>> 2**3

8

>>> 4^2

6

>>> 4**2

16

The ^ in Python represents XOR operator.

When evaluating mathematical expressions in Python, the type of the expression is considered and like for like produced like. An int with an int will produce an int, a float with a float produces a float. An int with a float will produce a float because it is easier to convert an int to a float and this preserves information. If floats are converted to ints in a mixed type expression, information would be lost and inaccurate results would be produced. For example, 1/4 + 2 would produce 2.25 and this is equivalent to 1/4 + 2.0.



There are many other functions that are available for use with Python but in some cases, you have to explicitly avail them by using the import keyword. One such library is the math library and this contains functions such as sin(x), cos(x), exp(x), factorial(x), etc.
Here is a list of all functions available in the math library
Other libraries available in Python are: All Python Modules

Share your thoughts