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

#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.


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


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


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

number=int(input("Enter the number: "))

Enter the number = 10
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.


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:


#printing doc of above square function.
   This function is square function. Which simply
   returns the square of any numbers

#printing doc of builtin function: i.e, input()
   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.

    #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: "))
    print(f"Area is {area} sq.meter")
    Enter length: 5
    Enter breadth: 6
    Area is 30 sq.meter