Programming in
python
Modules
What is a Module?
Consider a module to be the same as a code library.
A file containing a set of functions you want to include in your application.
Create a Module
To create a module just save the code you want in a file with the file extension .py:
Example
Save this code in a file named mymodule.py
def greeting(name):
print("Hello, " + name)
Use a Module
Now we can use the module we just created, by using the import statement:
Example
Import the module named mymodule, and call the greeting function:
import mymodule
mymodule.greeting("Jonathan")
DECISION MAKING STATEMENTS/CONDITIONAL STATEMENTS IN PYTHON
1. Conditional or Branching statements:- It is based on a particular condition set of
statements selected for execution. It helps us to check conditions and change the behavior of
the program.
I. if:
a) simple if : It will execute if the statement’s condition is True. And the statements will be
skipped if the condition is false
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
Other programming languages often use curly-brackets for this purpose.
b) Alternative Execution/ if-else statements : There will be two possibilities and conditions
for this, it determines which one gets executed. If the if statement is not executed , then the
else statement will be used for alternative execution of the program.
II. if..elif..else (chained condition) : It tells about sequence of statements and we have
more than conditions from which one turns out to be true and rest turns false
a) Nested if statement:
Syntax:
if test_expression1:
Inner if test_expression: Body of if
else:
Body of else
else:
Body of else
Switch case in Python
● A switch statement is a very useful and powerful programming feature.
● It is an alternate to if-else-if ladder statement and provides better performance
and more manageable code than an if-else-if ladder statement.
● Most of the programming languages (like Java, C, etc.) offer switch statements,
but the Python
● language does not have any switch statements.
There is a very efficient way to implement a switch case statement feature in Python.
Use of if-else-if ladder – An alternate to switch case statement. In this example, we
have used the if-else-if ladder statement to get the day of the week. For the same use
case, we will also see the implementation of switch state in Python.
def num_in_words(no):
if(no==1):
print("One")
elif(no==2):
print("Two")
elif(no==3):
print("Three")
else:
print("Give input of numbers from 1 to 3")
num_in_words(3)
num_in_words(4.5)
Here output will be
Three
Give input of numbers from 1 to 3
2. Implementation of switch statement using Dictionary of Strings- We can use a
dictionary to implement the switch case. We know that the dictionary holds the key-value
pairs. It can have the keys as the condition/ element to be checked and the values as the
elements to be printed or the functions to be executed.
def vowel(num):
switch={
1:'a',
2:'e',
3:'i',
4:'o',
5:'u'
}
return switch.get(num,"Invalid input")
vowel(3) # o/p i
vowel(0) # o/p invalid input
3. Using functions/lambda functions as the values of the dictionary-We can also use the
functions as the values of the dictionary. Then we can use the get() of the dictionary to call
that particular function. This will help us write more operations using the switch, than just
printing an element on finding a matching key.
def add():
return n+m
def subs():
return n-m
def prod():
return n*m
def div():
return m/n
def rem():
return m%n
def operations(op):
switch={
'+': add(),
'-': subs(),
'*': prod(),
'/': div(),
'%': rem(),
return switch.get(op,'Choose one of the following operator:+,-,*,/,%')
operations('*')
operations('^')
Implementing Python Switch using Class-Besides using the dictionary, we can also use
Python classes to implement the switch construct. The classes in simple words are the
collections of variables and functions that act as a single unit. We can define a class using a
‘class’ keyword’.
Eg.
class Month(object):
def month_1(self):
print("January")
def month_2(self):
print("February")
def month_3(self):
print("March")
def month_4(self):
print("April")
def month_9(self):
print("September")
def month_10(self):
print("October")
def month_11(self):
print("November")
def month_12(self):
print("December")
def getMonth(self,no):
name_of_method="month_"+str(no)
method=getattr(self,name_of_method,lambda :'Give an input as an integer from 1 to 12')
return method()
mo=Month()
mo.getMonth(5)
mo.getMonth(0.5)
Here we get output as
May
‘Give an input as an integer from 1 to 12’