Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Python Functions

What are functions?

Functions are the block of codes that are written to perform a specific task. If we are working on large projects, we need to write large lines of code. In this case role of functions comes into play. Functions help to break down the large lines of codes into small chunks which also enhance readability and reusability. We can easily write a large number of codes with the use of functions.

Function's syntax:
  • 'def' keyword is used to initialize the function, it is mandatory.
  • The function name is written in the way we write identifiers.
  • Inside the two small brackets, required parameters are written which is optional.
  • Colon represents the closing of the function initialization header.
  • Inside the function's body, all the required part of code is written.

The syntax is given below.


def function_name(parameters):
    #body of the function
Example:

#Greetings function
def greetings(name):
    print("Greetings ", name, "!")
Calling a function

After only writing a function your job is not done yet. The function will work only when it is called. We can call functions from anywhere in the program body. We can call a function by just writing its name and providing the required arguments.

Example:

#calling the Greetings function.
name=input("Enter your name: ")
greetings(name) #the func we just created above

Output:

Enter your name: Ram
Namaste Ram!
 
The Return Statement

The return statement is the value that is evaluated within the function. The return statement will close the function and return to the place where it has been called. If there is nothing to return in the function it can return None.

Syntax of return statement:

def function-name(parameters):
    #body of the function.
    return value

Example:

#Function that adds 5 to the number:
def add_five(num):
    return num+5

number=int(input("Enter the number: "))
    print(add_five(number))
Output:

Enter the number = 10
15
Docstring in Python

Docstring in python refers to the documentation string. It is written inside the triple quotation first in the function. Docstring describes what exactly the function does, and its information. It's like a multiline comment inside a function.

Example:

def square(num):
    """This function is square function. Which simply
    returns the square of any numbers"""
    return num**2

we can simply print the docstring of any built-in or user-defined function as follows:


print(function-name.__doc__)
Example:

#printing doc of above square function.
>>>print(square.__doc__)
   This function is square function. Which simply
   returns the square of any numbers

#printing doc of builtin function: i.e, input()
>>>print(input.__doc__)
   Read a string from standard input. The trailing newline is stripped.

   The prompt string, if given, is printed to standard output without a
   trailing newline before reading input.

   If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
   On *nix systems, readline is used if available.

Basically, there are two types of python functions they are:

  1. Python built-in functions.

    Built-in functions are those functions which are already been written and are ready to use. Python provides a large number of built-in functions with which we can easily write our code.

    Some built-in functions that Python provides are:

    • abs(): Returns the absolute value of any number.
    • bin(): Converts integer to binary.
    • callable(): Checks if the object is callable.
    • dict(): Creates a dictionary.
    • Enumerate(): Creates enumerate objects.
    • filter(): Construct an iterator from elements which are true.
    • getattr(): returns a value of the named attribute of an object.
    • hex(): Converts to Integer to Hexadecimal.
    • id(): Returns Identify of an Object.
    • len(): Returns the length of an object.
    • map(): Applies function and returns a list.
    • next(): Retrieves next item from the iterator.
    • open(): Returns a file object.
    • print(): Prints the Given Object.
    • range(): Return a sequence of integers between the start and stop.
    • str(): returns the string version of the object.
    • type(): returns the type of an object.
    • vars(): Returns the __dict__ attribute
    • zip(): Returns an iterator of tuples.
  2. Python user-defined functions

    User-defined functions are those functions that are written by ourselves, in our own way, for our own requirement satisfaction is called a user-defined functions.

    Example:
    
    #My function to calculate the area of a rectangle
    
    def my_area_calculator(length, breadth):
        return length*breadth
    
    l=int(input("Enter length: "))
    b=int(input("Enter breadth: "))
    
    area=my_area_calculator(l,b)
    print(f"Area is {area} sq.meter")
    Output
    
    Enter length: 5
    Enter breadth: 6
    Area is 30 sq.meter