Predefined functions in Python

The function is one of the most important concepts in programming. It allows you to break down a complex program into a series of simpler programs, which themselves can be divided into smaller parts, and so on.

In addition, a function is reusable. For example, if we have a function that calculates the square root, we can use it anywhere in our program without having to rewrite it each time.

The print() Function

We have already become familiar with this function. It should be noted here that it allows displaying any number of values provided as parameters (that is, inside parentheses).

By default:

  • the values are separated by a space
  • and a newline is added at the end

We can replace the default separator with something else (or even nothing) using the parameter sep.

Example:

print("Hello", "to", "everyone", sep="*")

print("Hello", "to", "everyone", sep="")

We can also replace the newline at the end using the parameter end:

n = 0
while n < 6:
    print("Oops!", end="")
    n = n + 1

Interaction with the User: the input() Function

Nowadays, most user input is done through:

  • entering parameters
  • mouse clicks
  • keyboard presses
  • etc.

In a text-mode script (like the ones we have created so far), the simplest way is to use the input() function.

This function pauses the program and asks the user to enter characters from the keyboard, ending with pressing the Enter key.

When the user presses Enter:

  • the function captures what was typed
  • and this value can be assigned to a variable or converted

The programmer can call input():

  • with empty parentheses
  • or with a parameter containing a message for the user

Examples:

prenom = input("Enter your first name: ")
print("Hello,", prenom)

Or:

print("Please enter any positive number: ", end=" ")
ch = input()
nn = int(ch)   # convert the string to an integer
print("The square of", nn, "is", nn**2)

Note: The input() function always returns a string.

Therefore, if you want the user to enter a numeric value, you must convert the input (which is a string) into the appropriate numeric type:

  • use int() for integers
  • use float() for real numbers

Examples:

a = input("Enter a numeric value: ")
type(a)

b = float(a)   # convert string to real number
type(b)

Calling a Function Module

You have already encountered several functions of the language, such as the function len(), which allows you to determine the length of a string. However, it is not possible to include all functions in standard Python, because there is an unlimited number of functions, and you will soon learn how to create new functions yourself.

The functions in standard Python are relatively few; they include only those that are used very frequently. Others are placed in special files called modules.

Modules are files that contain groups of functions.

You will see later that dividing a program into several files is convenient for maintenance. A Python application consists of a main program accompanied by one or more modules, each containing definitions of additional functions.

There are many Python modules provided by default. You can also find others from various sources. Often, we group related functions into the same module, which we call a library.

For example, the math module contains definitions of mathematical functions such as the square root, etc. To use these features, you simply add the following line at the beginning of your script:

from math import *

This line tells Python to import all functions from the math module into the current program (the symbol * means “all”).

Inside the script, you can then write, for example:

  • racine = sqrt(nombre) → to assign to the variable racine the square root of nombre
  • sinusx = sin(angle) → to assign to the variable sinusx the sine of angle (in radians)

Example:

# Test: using functions from the math module
from math import *
 
number = 121
angle = pi/6 # 30 degrees (pi is also defined in the math module)
 
print("square root of", number, "=", sqrt(number))
print("sine of", angle, "radians =", sin(angle))

When you run this script, you will get:

square root of 121 = 11.0
sine of 0.523598775598 radians = 0.5

This short example clearly illustrates some important properties of functions:

  • The function name must be written followed by parentheses, for example: sqrt()
  • Inside the parentheses, we can place one or more parameters, for example: sqrt(121)
  • The function returns a value, for example: 11.0

Note that the functions described in these pages are only introductory examples. If you quickly look at the Python library documentation, you will find many functions capable of performing numerous tasks, including complex mathematical algorithms (widely used in universities for solving high-level scientific problems).

There is no need here to provide a complete list. Such a list is easily accessible in Python:

  • HTML Documentation → Python documentation → Modules index → math

In the next article, we will learn about User defined functions in Python.

No Comments

Leave a Reply