Function in Python - ProgrammerTech
Language:

Search

Function in Python

  • Share this:
Function in Python

Functions in Python

Function The code according to our request (its main function is to execute the code for us) we may have code that includes more than 30 lines and we do not want to write it every time we put a function that executes this code for us, the Python language has many ready-made tools that we used in previous lessons such as (print, max, min, clear).

 

Rules for writing functions in Python

  1. It is not useful to start a function name with a number such as (5,8,8,2,8).
  2. Beginning its name with a symbol such as (#,$,@) does not work.
  3. It is not useful to divide the function into two or more words, such as (Ahmed Kaissar), but we separate it by Bandiscore _.

Define a new function in Python

def functionname():
    function_suite

whereas:

  1. The word def is for defining a new function.
  2. The word functionname: We put in its place the name that we give the function through which we can call it.
  3. Parentheses (): Inside the parentheses we put parameters, and you must put two points: immediately after the parentheses and then go down on a new line to write the commands that will be executed when the function is called.
  4. And function_suite: are the commands that we will put in the function and that are executed when it is called.

Important Alert

While using functions, you are forced to put 4 blank spaces, i.e. press the tab button before the commands that you will place in the function in order for the Python interpreter to know that these commands are inside the function, and in order to arrange and write the code as other programmers do, add Two blank lines after the function definition.

Example 1

def ahmed_kaissar (name ="kaissar")

In this case the name is called a parameter and the kaissar is called an ergoment.

Example 2

>>>def info ():
    print("ahmed kaissar")
    print("data")
>>>info()
ahmed kaissar
data

As we noticed in the example, we put a function named info and gave it two lines of printing tasks, and when we called the function with its name info () it printed what was inside this function, as the run command was info() and the result will be when run in the form.

ahmed kaissar
data

Example 3

>>>def info (name,age) :
    print ("your name is {0} and your age is {1}".format(name,age))
>>>info ("data","26")

Here we created a function named info with two parameters name and age and we arranged to print the data in format, and then we called the function and passed it values ​​for the parameters.

And when we run the above code, we will get the result

your name is data and your age is 26

Example 4

def get_sum(c, s):
    return c + s;
db = get_sum(26, 28)
print(db)

We created a function called get_sum with two parameters, c and s, then we said that this function returns the sum of two numbers, c and s. 26 and 28.

Then we run the print command to print what this function carries, and the result is adding the two numbers, and it will give us the result.

54

 

Setting default values ​​for parameters in Python functions

Python allows setting default values ​​for parameters in functions, so when you call your function you have the option to pass the parameter values ​​in place instead of being forced to.

def print_language(language='France'):
    print('Your language is:', language)
print_language()
print_language('Arabic')

We created a function named print_language and it is possible to pass a value in the language parameter or we can not pass any value because it has a default value which is French.

Then we called the print_language function with its name, and then we called it a second time, passing a second value, which is Arabic, which means that its value will become Arabic, and when we run, we will get the result:

Your language is: France
Your language is: Arabic

 

Defining functions in Lambda style

If you want to define a function that consists of only one line and returns a value when it is called, you can define it as an anonymous function and then use the word lambda to define it and not the word def.

Lambda style function definition in Python

lambda [arg1 [,arg2,.....argn]]:expression

whereas:

  • lambda for defining a new function that does not have a name.
  • and [arg1 [,arg2,.....argn]]: are the parameters you can set for the function noting that you must put a comma between each of the two parameters.
  • And :expression: is the command that will return the value when the function is executed, indicating that a colon must be placed: is mandatory.

When we define an anonymous function, we define a function that does not have a name, and since a function can only be called by calling it by its name, the solution here is to refer to the function with a variable name, that is, you define a variable and make it equal = the function that you In its definition, then the variable name will be considered as the name of the function through which it can be called.

Example 5

resulting = lambda price, tax: price - tax
print(resulting(2022, 1994))

We have defined an Anonymous Function, and this function works when it is called, we pass two numbers to it and it will return the result of subtracting the first number from the second number, and we assign the function to an object named resulting so that we can call it through it.

And when we press the enter button, we get the result:

28

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com