Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 12: Functions

2 Replies, 928 Views

[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"


Love me like you do Heart
Thanks again for an amazing tutorial
Rs
* Thankful to Allah *
Kurdy
(04-26-2022, 07:40 PM)Mr.Kurd Wrote: Thanks again for an amazing tutorial

Thank you very much  Heart
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,178 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,535 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,569 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,774 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,541 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,772 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,725 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,737 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,757 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,728 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)