Friday 31 August 2018

Python Functions

Python Functions


Function Definition: Function is a group of statements which performs a specific task. It is also known as a Sub-routine or a Procedure or a Method.

In most programming languages two types of functions available:
  • Library Functions (also known as System defined function, for example print(), input(), etc.)
  • User defined functions (written by programmers, for example calculate(), find_area(), etc.)

Let's learn how Python Functions works.

Defining a Python Function

Following simple rules are followed while defining Python Functions:
  • Function blocks begin with the def keyword; followed by the function name and parentheses (  ).
  • Any number of parameters or arguments can be placed within function parentheses.
  • In the beginning of functions, writing a short comments about a function is one of the standard practice. 
  • Function code begins with a colon(:) and has indentation.
  • return statement at the end indicates end of function.

Syntax of Python Function


def function_name( arguments ):
   "function comments"
   function logic / code
   return

By default, arguments or parameters have a positional behavior and you need to write in the same order as they defined.

Python Function Example

The following python function prints line with a specified character as an parameter.

def print_line( chr ):
   "This function prints a line of given character"
   for i in range(0,15)
      print (chr)
   return

print_line('*')
print("")
print(" Welcome to Python Practicals ")
print_line('=')

Output of program

* * * * * * * * * * * * * * * 
 Welcome to Python Practicals 
= = = = = = = = = = = = = = = 

* * * * *

Python Classes

Python Classes

Python File Handling

Python File Handling

Python Arrays

Python Arrays

Wednesday 22 August 2018

What is Python?

Python is one of the widely used high-level programming language invented by Guido van Rossum. It can be used for solving varieties of problems. Python is known for its simplicity and code readability. Programmer can develop application very rapidly using Python.

Python code resembles the English language. To interpret the Python codes, computer needs a special program known as the Python interpreter. Python interpreter is needed to code, test and execute the  Python programs.

Read more about Python Interpreter Installation here.