Exploring Python Functions and Conditions

Fortune Edema
4 min readFeb 17, 2023

--

As a cybersecurity enthusiast, my journey towards becoming a penetration tester and ethical hacker has been full of challenges. One of the major hurdles I face is schooling in a remote area, which limits my access to resources like a fast network and consistent power supply. However, I haven’t found ways to overcome these obstacles but i must continue on my path.

Recently, I returned home for a month-long election break, and I am thrilled to have enough time to skill up, prepare my seminar presentation, and search for a good final year project. I am especially excited about the faster network and increased power supply available to me while at home. These may seem like small things, but they can make a big difference when trying to pursue a career in cybersecurity.

On Valentine’s Day, I woke up to say my confessions and prayers. At 10:48 am, opted to continue learning Python programming language. I had previously talked about “Basic operations on data types”, and today I was ready to learn about “The basics on Functions and Conditions” in Python.

Python comes with some built-in functions that perform specific tasks. However, there are times when you need to create your own functions. For example, Python has a built-in function for summation (sum) and getting the number of items or length of items (len), but it does not have a function for finding the mean or average. So, I set out to create a function that would find the mean of a list of values.

python built in functions

To create a function in Python, you use the “def” keyword to define the function, followed by a name for the function, and a set of parentheses with a parameter inside. A parameter is an input that the function will process and display as an output. In this case, the input was a list named “mylist”. I declared a variable called “my_mean”,which was equal to the sum of the list input divided by the length of the items. Then, I told Python to return the values to “my_mean” before printing the solution and giving an input to the function or rather parameters.

result

After creating my first function, I decided to pass in a dictionary of student grades instead of a list. However, I soon discovered that the function could not process dictionaries, only lists.

This is where “conditionals” came into play. Conditionals are statements that check if different conditions or states are met before proceeding to solve a particular problem.

A simple Conditional Statement in python

I took a break to get some lunch and watch one of my favorite music videos by ASAKE titled “ORGANISE” and “Yoga”. Taking breaks is essential on this journey, and I learned to make sure I take them.

Explaining the code above i wrote before my break, I defined the function named “mean” and passed a parameter “value” to it at “line 1”. Under the function definition at “line 2”, I used a conditional statement “if” to tell the function that if the type of input given to the parameter in the function is equal to a dictionary, then “my_mean” is equal to the sum of the “value” in the key-value pair divided by the length of the “value” in “line 3”. Otherwise, at “line 4”, “my_mean” is equal to the sum of the “value” divided by the length of the “value” (this is the part of the code that solves for a list). Then at “line 7”, it returns the mean. At “line 9”, the student grade is declared and passed to a dictionary named “student_grades” and finally, the function call is made at “line 10”.

result

I hope you enjoyed this journey with me as I shared my experiences as a cybersecurity enthusiast learning Python. The journey is not always easy, but with determination, hard work, and taking breaks, we can all achieve our career goals. Till we meet again. Happy Reading!

--

--

Fortune Edema
Fortune Edema

Written by Fortune Edema

Information Security Associate ISO IEC 27001 ||Tech Enthusiast||InfoSec Researcher|| Jnr SOC Analyst ||Security Awareness||Computer Science Student

No responses yet