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.
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:
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 + 1Nowadays, most user input is done through:
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 programmer can call input():
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:
a = input("Enter a numeric value: ")
type(a)b = float(a) # convert string to real number type(b)
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:
# 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:
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:
In the next article, we will learn about User defined functions in Python.
No Comments