An Introduction to Interactive Programming in Python (Part 2)
An Introduction to Interactive Programming in Python (Part 2)

Control Flow

The for loop

The for loop in Python has the ability to iterate over items of any sequence such as list or string.

It steps through the items in any ordered sequence i.e, String, List, Tuples, the keys of dictionaries and other iterable terms. The python for loop starts with a keyword for followed by a variable attribute, which will hold the values of the following sequence object.

Syntax of for loop



In this case, we just print the value in the block of statements.


The While loop

A while loop statement in Python programming language repeatedly executes a target statement as long as the given condition is true. Unlike the for loop, the while loop will not run n times, but until a defined condition is met.

Syntax of While loop


The Range Function

The range function in Python generates a list of numbers, which can be used to iterate over for loops and in other few cases.

Now let’s see how range function works

range(n) generates, the integer numbers starting with 1 and ending with (n-1).

range(begin, end) generates the integer numbers starting with begin and ending with end-1.


>>> range(8)

range(0, 8)


Printing the squares of numbers iterating by range function.



Function is a block of reusable code that performs a specific task. It is a small unit of computation which may take arguments and may return values.

Note: Function body must be indented like ‘if’ statement.

Declaring a function

The keyword def introduces a function definition followed by the function name and the parenthesised list of formal parameters. The statements that form the body of the function start at the next line and must be indented.


Note: Declaring a function doesn’t run the function. In order to run, you have to call the function using the function name.



In this program we created a function named greet with no parameters such as empty parenthesis. The function is defined to print a string called “Hello ! Welcome to the party.”

So calling the function just prints the given string.

Functions with parameters.

A function can take parameters which are values you supply to the function so that the function can do something utilising those. Parameters are specified within the pair of parentheses in the function definition separated by commas.




Also see Recursion, Scope of Variable.

Introduction to Object Oriented Python

In all the programs we wrote till now, we have designed our program around functions i.e. blocks of statements which manipulate data. This is called the procedure-oriented way of programming. There is another way of organising your program which is to combine data and functionality and wrap it inside something called a class. This is called the object oriented programming paradigm.

A quick glance at the basics of the Object Orientation terminology.

Class : A class is the blueprint from which individual objects are created.

Object : A real World entity which have state and behavior.

Let’s create a class Person with a class method say in the class.

Now let’s create an object instance for the class.

Extending the plot further lets us create two methods, hello and bye that take arguments.


Note: Self is a default argument for all instance method

This needs us to repeat the instance variable for every class method instead of creating an object with the instance variables. It’s now time to work with constructors.


Constructors in Python are written under a special method __init__.

Now let us write a constructor for an object. In this, example let’s create an object with instance variables name and year_of_birth.This process of writing a constructor in a class eliminates the repeating of the instance variables for every instance method.


Classes and Objects Example :



☞ Data Science and Machine Learning Bootcamp with R

☞ The Complete Python Masterclass: Learn Python From Scratch

☞ Python 3: A Beginners Quick Start Guide to Python

☞ Learning Python for Data Analysis and Visualization

☞ Coding for Entrepreneurs: Learn Python, Django, and More.


  1. and did yourself notice that there is a No cost shuttle in direction of and versus the competition towards the customer heart at 6th & marketplace both of those Sat & Sunlight… consequently astounding. verify it out.


Please enter your comment!
Please enter your name here