Introduction To Python Programming - part 5

akinnimimanuel

Akinnimi Stefan Emmanuel

Posted on August 15, 2023

Introduction To Python Programming - part 5

Hello, and welcome to the last part of the series “Introduction to Python Programming.” If you have gone through the series chronologically, you are now on your way to becoming a Python Ninja.

If you have not gone through the previous episode, kindly find the links below.
Introduction to Python Programming - part one
Introduction to Python programming - part two
introduction to Python programming - part three
introduction to Python programming - part four

Python Function

A function only executes when called. You can supply parameters or data, to a function. As a result, a function may return data.

Creating a Function

The Python def keyword is used to define functions:

def my_function():
  print("Hello World")
Enter fullscreen mode Exit fullscreen mode

Calling a Function

def my_function():
  print("Hello World")

my_function()
Enter fullscreen mode Exit fullscreen mode

Arguments

Information can be passed into functions as arguments. Add as many arguments as you like; just be sure to space them out with a comma.
The function in the next example only takes one argument, "Myname". The function receives a name as a parameter when it is called, and uses it to print the "Hello + name":

def my_function(Myname):
  print("Hello", + Myname)

my_function("Emmanuel")
my_function("Stefan")
Enter fullscreen mode Exit fullscreen mode

Parameters or Arguments?

Both parameters and arguments can be used to describe data that is passed into a function.

A parameter is listed inside the parentheses, while an argument is the value sent to the function when it is called.

Number of Arguments

By default, the correct number of parameters must be used to invoke a function. In other words, you must call your function with 2 arguments, not more or less, if it needs 2 arguments.

#A function expects two arguments and gets two arguments
def my_function(FirstName, LastName):
  print(Firstname + " " + Lastname)

my_function("Emmanuel", "Akinnimi")
Enter fullscreen mode Exit fullscreen mode

An error will occur if you attempt to call the function with just one or three arguments:

def my_function(FitstName, LastName):
  print(Firstname + " " + Lastname)

my_function("Emmanuel")
Enter fullscreen mode Exit fullscreen mode

Arbitrary Arguments, *args

If you don't know the number of arguments your function will receive, add a * before the parameter name.
The function will then be provided with a tuple of parameters and will have access to the parts as required:

def my_function(*colour):
  print("The colour of the ball is " + colour[2])

my_function("red", "Green", "white")
Enter fullscreen mode Exit fullscreen mode

Keyword Arguments

You can send parameters with the key = value syntax as well. The arguments' chronological arrangement is irrelevant.

def my_function(food1, food2, food3):
  print("I love eating" + food2)

my_function(food1 = "rice", food2 = "beans", food3 = "noodles")
Enter fullscreen mode Exit fullscreen mode

Arbitrary Keyword Arguments, **kwargs

If you are unclear about the number of keyword arguments your function will accept, place two asterisks before the parameter name in the function definition: **.

In doing so, the function will be able to effectively access the objects after receiving a dictionary of arguments:

def my_function(**city):
  print(city["city2"]+ " is located in europe")

my_function(city1 = "Lagos", city2 = "Paris", city3 = "California")
Enter fullscreen mode Exit fullscreen mode

Default Parameter Value
Without arguments, the function will use its default value.

def my_function(country = "Belgium"):
  print("I am from " + country)

my_function("Nigeria")
my_function("India")
my_function()
my_function("South korea")
Enter fullscreen mode Exit fullscreen mode

Passing a Tuple as an Argument

You can supply any data type as an argument in Python (list, tuple, dictionary, strings, numbers, etc.).

If a Tuple is given as an argument, for example, it will still be a Tuple when it reaches the function:

def my_function(Country):
  for a in Country:
    print(a)

cities = ["New York", "London", "Cairo", "Algiers"]
my_function(cities)
Enter fullscreen mode Exit fullscreen mode

Return Values

To make sure your function return a value or data, you will use the return keyword.

def my_function(a):
  return 10 * a


print(my_function(200))
print(my_function(50))
print(my_function(98))
Enter fullscreen mode Exit fullscreen mode

The pass Statement

If your function won't contain any data, you can use the pass keyword to bypass the error Python will throw for an empty function.

def myfunction():
    pass
Enter fullscreen mode Exit fullscreen mode

congratulations image

Congratulations to everyone for completing this series with me. We have discussed so many things in Python, and you have written a lot of code.

Next step

The beauty of Python is that it can be used in virtually all departments of programming. After this introduction to Python, you can deepen your knowledge in Python by building a lot of projects, this will help solidify what you have learnt so far. Deepen your understanding of programming concepts by learning data structures, algorithms, and design patterns. Dive into web development using frameworks like Django or Flask. Explore data science and machine learning with libraries like NumPy, Pandas, and Scikit-Learn. Venture into GUI application development using tools like PyQt or Tkinter. Learn about cloud computing with platforms like AWS or Azure. Expand your knowledge with other languages like JavaScript for front-end web development or C++ for system programming. Ultimately, your choice should align with your interests and career goals, enabling you to create innovative applications and solutions.

💖 💪 🙅 🚩
akinnimimanuel
Akinnimi Stefan Emmanuel

Posted on August 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related