functions in python
Functions in python

Functions in python is easy to write and they allow certain block of code executed many times instead of repeatedly write several block of codes in your compiler.

We will guide you step by step in this process on how to create functions and why. Before that you must know the prior information about functions. your function must include ‘def’ keyword to define a function, right indentation is required with proper structure.

I you are not following the above 3 conditions your function will not run and will show the error in compiler.

A function is like a machine. you give some input and it will return some output according to your function. as you can see in the below diagram. this function or machine returns 2 times of the input value. so, lets see how to write this function using python.

python functions
Functions in python
# A simple code to return double value
def doublemyx(x):
  y=2*x
  return y
doublemyx(5)
>> 10

Easy Right? Now let’s move one step further. we will create a function that will reverse our website name “educatedinfo”. here the name of the website should be in string form. the function only accept the string type.

def reverse(s):
    str = ""
    for i in s:
        str = i + str
    return str
reverse('educatedinfo')

>> ofnidetacude

Just imagine.with the help of functions you can build any thing that returns what you want. you can also give arguments in the functions. if you can see in the below example the function returns girlfriends name of different characters in Naruto. Also read : How to apply joins using pandas

x= input("Type the name of character:")
def girlfriend(x):
  if x== 'naruto':
    return "hinata"
  if x== 'sasuke':
    return "sakura"
  if x== "fugaku":
    return "mikoto"
  if x== 'kiba':
    return "tamaki"
  if x== 'sai':
    return "ino"
  if x== 'asuma':
    return 'kurenai'
  if x== 'shikamaru':
    return 'temari'
  if x== 'minato':
    return "kushina"
  if x== 'obito':
    return "rin"
  else:
    return "girlfriend not found"

girlfriend('obito')

>> rin

One more example we can take to understand the functions more better. what we can do is we will pass a list of numbers and build a function that will tell if there is even number in the list if not then function will return nothing. so let’s see how to write the code for this in python.

def check_even(n_list):
  for number in n_list:
    if number%2== 0:
      return True
    else:
      pass
  return False

check_even([1,5,7])
>> 
check_even([2,4,7])
>> True

Now Lastly we will see the tuple unpacking with functions. after that you will be ready to do any problem related to functions. for your information tuples are immutable objects and we can’t alter their values (just remember it).

Let’s say we have a tuple that contains animation or cartoon movies with their respective ratings on IMDb and you want to create a function that will return highest rated movie between those. so let’s do this with tuple unpacking via functions in python

ratings = [('spirited away', 8.6), ('toy story 3', 8.3), ('finding nemo', 8.2), ('Monsters, Inc.', 8.1)]

def award_winner(ratings):
  min_rating = 0
  name_movie = ''
  for movie,rating in ratings:
    if rating > min_rating:
      min_rating = rating
      name_movie = movie
    else:
      pass
  return (name_movie, min_rating)

award_winner(ratings)
>> ('spirited away', 8.6)

Now, We have successfully completed Functions in Pythons. Thanks for reading.

If you want to learn more you can go here. Thanks for reading Educatedinfo!

LEAVE A REPLY

Please enter your comment!
Please enter your name here