A Modular Approach To Program Organization
A Modular Approach To Program Organization
i = 1
while True:
if i%3 == 0:
break
print(i)
i + = 1
OUTPUT
1
2
What will be the output of the following Python code?
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
OUTPUT
0
1
2
What will be the output of the following Python code?
i = 0
while i < 3:
print(i)
i += 1
else:
print(0)
OUTPUT
0
1
2
0
What will be the output of the following Python code?
x="abcdef"
while i in x:
print(i, end=" ")
OUTPUT
NameError: name 'i' is not defined
What will be the output of the following Python code?
x="abcdef"
for i in x:
print(i, end=" ")
OUTPUT
a b c d e f
What will be the output of the following Python code?
x = 'abcd'
for i in range(x):
print(i)
OUTPUT
for i in range(x):
TypeError: 'str' object cannot be interpreted
as an integer
What will be the output of the following Python code?
x = 'abcd'
for i in range(len(x)):
print(i)
OUTPUT
0
1
2
3
What will be the output of the following Python code?
x = 123
for i in x:
print(i)
OUTPUT
for i in x:
TypeError: 'int' object is not iterable
What will be the output of the following Python code?
for i in range(2.0):
print(i)
OUTPUT
for i in range(2.0):
TypeError: 'float' object cannot be interpreted
as an integer
What will be the output of the following Python code?
OUTPUT
m, y, , n, a, m, e, , i, s, , x,
What will be the output of the following Python code?
OUTPUT
my, name, is, x,
Write a python program to count number of characters,
words in the given string
* * * * * for i in range(5):
* * * * for j in range(5):
* * * if(j>=i):
* * print("*",end=" ")
* print()
Write Python code to print following pattern?
* * * * * for i in range(5):
* * * * for j in range(5):
* * * if(j<i):
print(" ",end=" ")
* * if(j>=i):
* print("*",end=" ")
print()
* * * * * for i in range(5):
* * for j in range(5):
if(i==0 or j==0 or i==4 or j==4):
* * print("*",end=" ")
* * else:
* * * * * print(" ",end=" ")
print()
Write Python code to print following pattern?
* * * * * for i in range(5):
for j in range(5):
* * if((i==j)or i==0 or j==0 ):
* * print("*",end=" ")
else:
* * print(" ",end=" ")
* * print()
* * for i in range(5):
* * for j in range(5):
if((i==j)or i==4 or j==4 ):
* * print("*",end=" ")
* * else:
* * * * * print(" ",end=" ")
print()
Write Python code to print following pattern?
* * for i in range(5):
for j in range(5):
* * if((i+j==4)or i==4 or j==0 ):
* * print("*",end=" ")
else:
* * print(" ",end=" ")
* * * * * print()
* * * * * for i in range(5):
* for j in range(5):
if(i==0 or j==0 or i==4 or i==2):
* * * * * print("*",end=" ")
* else:
* * * * * print(" ",end=" ")
print()
A Modular Approach to Program
Organization
Need for Modules
So far, we have learnt 2 ways of executing Python code:
1. By typing Python instructions directly into the
interpreter and getting it executed immediately.
2. By storing Python instructions in a file and running it as
a scripting.
Cont..
>>> help(math)
Help on module math:
NAME
math
MODULE REFERENCE
http://docs.python.org/3.3/library/math
The following documentation is automatically generated from the Python
source files. It may be incomplete, incorrect or include features that are
considered implementation detail and may vary between Python
implementations. When in doubt, consult the module reference at the location
listed above.
DESCRIPTION
This module is always available. It provides access to the mathematical
functions defined by the C standard.
FUNCTIONS
acos(...)
Cont..
• The statement import math creates a variable called
math that refers to a module object.
• In that object are all the names defined in that module.
Each of them refers to a function object:
Cont..
>>> radius = 5
>>> print('area is', math.pi * radius ** 2)
area is 78.53981633974483
Defining Your Own Modules
• Any Python script can be considered to be a module.
• For example, create a module that defines a function
factorial to find the factorial of an integer.
fact.py
import moduleName
Write a program to find the combination using the factorial function of
the fact module
#Program to find the combination of 2 integers
using modules.
import fact
def ncr(n,r):
return int(fact.factorial(n)/
(fact.factorial(r)*fact.factorial(n-r)))
#driver code
x, y = input("Enter 2 integers:").split(' ')
x,y = int(x),int(y)
print(“Combination of {} and {} is {}
“ .format(x,y,ncr(x,y)))
OUTPUT: Enter 2 integers:5 3
The combination of 5 and 3 is 10
Modules, Classes, and Methods
Modules
• A Python module is a file containing Python definitions and
statements.
• A module can define functions, classes, and variables.
• A module can also include runnable code.
• Grouping related code into a module makes the code easier to
understand and use. It also makes the code logically organized.
What Is Object-Oriented Programming?
• The __init__ method is reserved in Python, also known as a constructor, it is called each
time when an object or instance is created from a class. It is commonly used to initialize
the attributes of the class for the created object.
• Each method of the class has the self-identifier, which identifies the instance upon which
a method is invoked. This allows us to create as many objects of a class as we wish.
Class Objects
• We already saw that a class is a blueprint. So, objects also known
as instances are known to be the realization of the blueprint,
containing actual values.
• In example 1 , the line of code
opp = add_sub(x,y)
• A class can have a set of variables (also known as attributes, member variables)
and member functions (also known as methods).
Cont..
brand: ‘Hyundai’
model: ‘Verna’
color: ‘Black’
fuel: ‘Diesel’
Cont..
class Car:
def __init__(self,brand,model,color,fuel):
self.brand=brand
self.model=model
self.color=color
self.fuel=fuel
def start(self):
pass
def halt(self):
pass
def drift(self):
pass
def speedup(self):
pass
def turn(self):
pass
Cont..
blackverna=Car('Hyundai','Verna','Black','Diesel')
This creates a Car object, called blackverna, with the a forementioned attributes.
Now, let’s access its fuel attribute. To do this, we use the dot operator in
Python(.).
blackverna.fuel
Output
‘Diesel’
Modules, Classes, and Methods
Methods
• A Python method is like a Python function, but it must be called
on an object.
• To create method, you must put it inside a class.
• Now in this Car class, we have five methods, namely, start(),
halt(), drift(), speedup(), and turn().
• In this example, we put the pass statement in each of these,
because we haven’t decided what to do yet.
Calling Methods the Object-Oriented Way
Example
class Try:
def __init__(self):
pass
def printhello(self,name):
print(f"Hello, {name}")
return name
obj=Try()
obj.printhello(‘ABCD’)
Output
Hello, ABCD