# Introducing Python

Back To The Index

MyFirstProgram

Practicing by writing programs, is undoubtedly the best way to learn a programming language. And its almost a convention to start with ‘Hello World’ program, which simply prints “Hello World” to the screen. Python is a powerful tool, so a ‘Hello World’ would take just one line of code, so we start with something not as trivial. Following is our choice of first program. This has been designed to be simple yet it contains almost all the components of how a well structured code should look like. This program establishes a primary driving function called ‘main’, which inturn calls two other functions namely the ‘first_function‘ and the ‘second_function‘.

We will assume that the reader is already fimiliar with a bit of python, and understands the importance of correly indenting the code.

In [22]:
#############################################
# Filename: MyFirstProgram                  #
# Purpose: A simple demonstration of        #
# python program structure.                 #
# Author : Niel S.                          #
#  The English Tea Company LLC.             #
#############################################

def main():
'''
This is our main function. It
controls the flow of execution of the
program.
'''
#Print something out.
print ('Printing from main, Hellow World!!!')

#Calls the first function
first_function()

#Call the second function
second_function(greet='Aloha World !!!')

#Define first_function
def first_function():
'''
First function
'''

print ('I am first function')

#Define second function
def second_function(greet):
'''
Second function
'''
print ('I am second function ',greet)

#Execution Starts Here
if __name__ == '__main__':
main()

Printing from main, Hellow World!!!
I am first function
I am second function  Aloha World !!!


# Learning by Dissecting¶

## Component 1¶

The # sign indicate that rest of the line is a comment. Comments are used to include short explanations, and are meant for helping the readability of the program for humans. Following block is an cosmeticized description of some essential information about the program that is about to follow.

In [17]:
#############################################
# Filename: MyFirstProgram.py               #
# Purpose: A simple demonstration of        #
# python program structure.                 #
# Author : Niel S.                          #
#  The English Tea Company LLC.             #
#############################################


## Component 2¶

Following block of the code initiates the definition of a function. A function is a group of python statements which are given a collective name. The python keyword for beginning a function’s definition is def. In this case, function being defined has the name ‘main’. Note that unlike C/C++ Python does not require a ‘main’ function in order to run. But since we’re learning Python along with best practices, we will stick as close to the conventions of functional programing as we can. So just like C/C++, we have arranged the ‘main’ to be the driving function of this program.

In [18]:
#A function definition starts with keyword def
def main():
'''
This is our main function. It
controls the flow of execution of the
program.
'''
#Print something out.
print ('Printing from main, Hellow World!!!')

#Calls the first function
first_function()

#Call the second function
second_function(greet='Aloha World')


The ‘main’ function:

• Begins with a multiline explanation enclosed in tripple quotes. This is called a doc string.
• Prints of a statement : ‘Printing from main, Hellow World’ (Note the syntax)
• Calls a function ‘first_function’, whose definition comes later down the program.
• Calls a function ‘second_function’, and passes the string ‘Aloha World’ to it.

## Component 3¶

Following block defines a function named first_function. The empty parentheses () after function name indicate that first_function is not expecting any input data to be passed at the time of calling.

In [19]:
#Define first_function
def first_function():
'''
First function
'''

print ('I am first function')


In  first_function  and main above, the entire body can be seen as made of two parts.

• The declaration bloc: def first_function():.
• The code bloc  ''' First function ''' print ('I am first function') 

Note that colon (‘:’) marks the end of declaration block and beginning of the code block. The interprette will expect atleast one statement in the code block or will throw up and error. All lines encountered after declaration untill the one at outer indentation, are part of the same function’s code block. For example, the declaration def first_function(): is at an outer indentation level compared to statements in main hence the interpreter knows that its not a part of main.

## Component 4¶

Following block defines another function named second_function. The identifier greet between the parentheses indicate that second_function expects some input data at the time of calling. The data so passed along will be available through the identifier  greet  inside the code block of the function.

In [20]:
#Define second function
def second_function(greet):
'''
Second function
'''
print ('I am second function ',greet)


The second_function prints a statement containing a standard message along with the data passed along to it. Note how this function is called from main :  second_function(greet='Aloha') 

This means that string ‘Aloha’ is passed along, and is available inside code block through identifier ‘greet’. Hence this call to second_function results in following output:

 I am second function Aloha !!

Note that, the identifier  greet  is parameter of second_function and the string  Aloha !!  is the argument of this particular call of second_function.

## Component 5¶

Following bloc of the code tells interpreter to run main as the MAIN DRIVER of the program, thus giving a C/C++ like structure to our file.

In [21]:
#Execution Starts Here
if __name__ == '__main__':
main()

Printing from main, Hellow World!!!
I am first function
I am second function  Aloha World

Out[21]:
''

If you’ve made this far, then Congratulations ! you’ve just been introduced to a very large fraction of what you need to become a Python developer with a clear and lucid style. Next tutorial will be about the structures for controlling the flow of execution.

Back To The Index ﻿