Skip to main content

Python functions and modules

Functions are a block of code that performs the task specified. They either just perform some computation and display the result or returns the result to the main function.
In python, functions are defined with the keyword 'def'.

The syntax for defining a function in python is :

def function_name :
      #Block of statements

The keyword def is followed by the name of the function. It is a good practise to name the functions meaningfully representing the task done by it. The function name is followed by a colon (:).
In python, Comments are represented by the symbol #. So here #Block of statements is commented and hence it will not be executed by the compiler.
# is used to comment one line of code.
''' .......''' is used to comment a block of code.

For example,

#this is a commented line of code.
''' this is a block of commented code
Everything in this code will not be looked into by the compiler
This is helpful in commenting many lines of a program
'''
a=5
b=6
print(a+b)

Output :

11

Let us consider a program that prints the sum of two numbers whose sum is calculated and printed inside a function.

def add(a,b):
      print(a+b)
add(5,5)

Output:

10



Here you must note that the print statement is indented by a tab space.
The indented code represents the function block.
In the next line, we call the function add() and it executes the block inside the function and prints the result as 10.

This is how functions are implemented in python.

Books to learn the basics of python programming :

1. Python: Programming Basics for Absolute Beginners: Volume 1 (Step-By-Step Python)

2. Learning with Python

Python modules :

Let me give a small introduction on working with modules in python.
Modules in python are similar to the header files in c. Python contains many inbuilt modules which can be used for specific purposes.

Simply, module is a file with  python code. By importing the module, we import that python code in our program.

Some of the default modules that come with python are date module, time module, etc
Let us see an example implementing the python modules.

Code :

import time
print("The current system time is :")
print(time.asctime(time.localtime(time.time())))

This code accesses the time module and displays the current system time in readable format.

Output:

The current system time is :
Fri Apr 21 23:07:11 2017



This is how we import modules in python and use it in the program.

Thank you.

                >> Next : Introduction to Pygame >>

Comments

Popular posts from this blog

Moving Objects

Hi everyone ! In this post, let us see how to move objects on the game screen. In the previous post, we learnt how to draw objects and fill colors. Now we will see how to move those objects from one place to another. To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons. import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False rect_x = 100 rect_y = 100 while not game_loop:     for event in pygame.event.get():         if event.type == pygame.QUIT:             game_loop = True         if event.type == pygame.KEYDOWN :             if event.key == pygame.K_LEFT :                 rect_x -= 10             if event.key...

Drawing shapes And Filling colours

Till now, we have written a python program to open a blank black screen and the QUIT event is handled. In this post, let's see how to add colours to the game surface and draw shapes onto the screen. Filling colours : By default, pygame has no predefined colours. We have to define the colours by giving the RGB(red,green,blue) value of the colour.   import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False while not game_loop:     for event in pygame.event.get():         if event.type==pygame.QUIT:             game_loop=True     display.fill(blue)     pygame.display.update() pygame.quit() quit() Here i have defined the basic colours by giving the rgb value of those colors. The values are given as a tuple here. Since white is a mixture o...