Python For Beginners

Python For Beginners

Introduction to functions in Python

Functions are named blocks of code designed to do one specific job. With functions, we write code once, and run it whenever we need to accomplish that specific task.

For example, if we create a function called open_bank_account() then:

  1. Whole logic for opening a bank account would be implemented inside this function.
  2. We would call this function whenever we need it (any time when we want to open a bank account for some customer).

Functions can take in the information they need, and return the information they generate.  Using functions effectively makes your programs easier to write, read, test, and fix.

Definition/identity of a function

The first line of a function is its definition, marked by the keyword def. The name of the function is followed by a set of parentheses and a colon. The content within a function, a logic itself, is indented one level.

In this example we coded a say_hallo() function which prints “Hallo!” in console. As simple as that.

def say_hallo():

There are much more use cases for functions

  • Function which accepts list as an argument
  • Function which modifies a list
  • Function which collects an arbitrary number of arguments
  • Function which collects an arbitrary number of keyword arguments
  • Functions which is stored in a module
  • Function imported from a module
  • Function with an alias
  • Selective importing of functions from a module

These topics are not covered in this lesson.

Important reminder

As any other programming language, Python too has a naming conventions, provided at the official Python site.

These naming conventions are not the same as in the other programming languages.

  • For example, in Java or PHP, we would name functions with camel-case, where in Python we do it in small-caps separated by underscore.
  • As a result, function in Java or PHP would be called openBankBalance(), but in Python will be open_bank_balance().

Let’s make one thing clear, both function names would work in all programming languages. The only difference is that Java or PHP sees openBankBalance() as pretty and clean, while Python sees open_bank_balance() as pretty and clean.

If you wonder which approach is correct? Well, both and none of them. 🙂

Simply use the convention of the programming language that you are working with.


Leave a Comment