Mathematics

Usage example

Create a file with the .py extension

For example functions_demo.py

Let us look at the simplest function that does nothing.

To declare it, use the word def then invent a name, for example lazy put round brackets () and a colon

The general syntax is as follows

def mathematics_name(): some code

In our case we get

def lazy(): pass

pass means to simply continue the code without doing anything. It is not used in all functions.

Our function is created, in other words, declared. This means that it exists somewhere in the code and can do something.

But it does not. For a function to do something, you need to call it. In our case it is enough to write its name and round brackets.

lazy()

python functions_demo.py

If no errors are made, the output will be empty

Let us declare and call a function that prints Hello World!

def hello(): print("Hello World!") hello()

python functions_demo.py

Hello World!

A function does not have to be written on one line. It is better to go to a new line after the colon and indent with four spaces.

def hello(): print("Hello World!") hello()

python functions_demo.py

Hello World!

Let us write a function that adds two numbers.

def sum(first, second): print first + second sum(3,4)

python functions_demo.py

7

Banner image

Built-in Mathematics

Mathematics
Solving a quadratic equation in Python
all()
map()

Article author: Andrei Olegovich