Red Security

Full Version: Python 12: Functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Image: 1*RJMxLdTHqVBSijKmOO5MAg.jpeg]
Functions:
         We have mentioned a lot of functions in our program even though the print statement is also a function. We have used a lot of functions in our previous code but now we will understand deeply how these functions are created and used and how you can make your own functions. Function is a block of code which performs some specific task. To revise, here are some example function..

Quote:print("Hello world!")
int(5.12)
range(0,10)


Functions divide our code into smaller parts which are really helpful in debugging (Process of finding and removing errors). The word before parenthesis is the name of the function and inside the parenthesis are attributes. Function makes our code manageable when we are working on a complex program.
We have built-in functions for lists and strings; these are known as built-in functions.
Let’s see some other functions we can use with lists.

index(): This function returns the index of an element in the list.

Code:
# Program to demonstrate index function
number = [1,2,3,4,5,6,7,8,9]
print(number.index(8))


max(list): This function will return the maximum number in the list

Code:
# Program to demonstrate max function
number = [1,34,12,54,89,342]
print(max(number))


min(list): This will return the minimum number of list
sort(list): This will sort the list

To check more function check out this link
Quote:https://www.w3schools.com/python/python_ref_list.asp

Now let’s check out some string method/functions

splitlines(): Splits the string at line breaks and returns a list
startswith(): Returns true if a string starts with a specified value
swapcase(): It will swap the case of string if it is lower it will convert it into capital and vice versa

To know more about string functions check out the link below
Quote:https://www.w3schools.com/python/python_ref_string.asp


Now, let’s talk about user-defined functions. These are the functions defined by programmers you can create your own function by using a keyword ‘def’ now let’s write code

Code:
# Program to make a user defined function
def myName():
    for i in range(5):
        print("Covid")
myName()

You can call a function by its name as in above program function is called once. But you can call a function as many times as you want. You can also pass arguments to your function. Arguments are the variables only in that specific function. Let’s check the code for this concept


Code:
# Program to find factorial of a number
def fact(num):

    fac = 1
    i = 1

    while i <= num:
        fac = fac * i
        i = i + 1

    print("factorial of ", num, " is ", fac)

fact(6)

That’s it for today. It is the last thread for python beginners. Congrats for completing beginners for Python. After that we will be diving into more cool concept of Python This is just a beginning.. 

"So, Keep Good care of your Health
 Allah Hafiz and Good Bye Till the next post"
"Keep Learning and Keep exploring"


Thanks again for an amazing tutorial
(04-26-2022, 07:40 PM)Mr.Kurd Wrote: [ -> ]Thanks again for an amazing tutorial

Thank you very much  Heart