What is a Python function?
A Python function is essentially a piece of code that we assign a name and we can use over and over again in the rest of our program.
Creating a Python function
In Python, a function is created using the keyword “def” followed by the name of the function and a pair of parentheses. When coming up with a name for a function we should always use something that makes sense or and kind of fits in with what your code is doing. This makes your code more readable.
Example one
In this article, we are going to name the Python function myfunction because it is just an example. After the name of the function and the parentheses, we should do a full colon. It is important to note that after we hit enter everything that happens in this function has to be indented.
Inside the body of the function, we are going to print out “this is my function”. If we save this program and run it we will not get any output because we have not yet called the function. To call a function in python type the name of the function followed by parentheses which just has this one line of code in. It prints out ”this is in my function” and there we go:
We can also use a return statement to return things out of our Python function. To demonstrate that we are going to take away the print statement from the body of the function. Instead, we are going to assign the string “this is my function” to the variable “name”.
Now if we run this nothing will happen because we have removed the print statement. Now to return something out of our Python function, we simply type the word “return” and then whatever it is, we are returning, in this case, we are returning ”name”.
In order to actually get some output when we run this function, we should put a print statement around where we are calling myfunction(). So it’s important to make sure that if you’re always returning something from your function.
Example two
In this example, we are going to create a function by the name “add10” and then pass it one parameter x in between the parentheses. Now, this basically means that when we call for this function we have to put in this parameter or argument in here that basically says we need a value for x.
Now inside the body function place, y = x+10 and then return y on the next line. This function takes the value that we pass to it and then adds a 10 to it. Finally, print out the function add10() and then pass an argument inside the parentheses, in this case, we are going to pass 5.
So when we run this it is going to execute the function, take the 5 as x and then apply it and come out of the other end we get 15. As shown in the code below.
Example three
In this example, we are going to write a function that splits a sentence into words and returns how many words were in that sentence. We are going to define a function by the name splitandcount() and then we’re going to pass a parameter by the name “sentence”.
Now in the body of the Python function we are going to declare a variable s and assign it a method split() as shown here s= sentence.split().
We are now going to return the length of s. Now when we split a sentence or a string it splits it into words, individual words And then the length is basically the length of the list, s.
We can now go ahead and print the splitandcount(“hello welcome to codeberry”) and now when we run this we should get four out of there.
Summary
This is how Python functions work in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
