Professional Documents
Culture Documents
COURSE MATERIAL
QUESTION PATTERN
PART A 10 x 2 Marks 20
PART B 5 x 6 Marks 30
Decision Making – Loops – If, Else, Elif – While Loops – For Loops – Nested Loop –
Break – Continue Methods – Functions – Built in Functions
UNIT - I
1. Define operators and briefly explain any 4 types of operators. CO 1 - U -
2. Write a program to calculate the area of a circle with radius value of CO 2 - AP PO1
10cm.
3. Compare tuples and list with syntax & examples. CO 1 - U -
4. Explain briefly about length of the string, string slicing, indexing with CO 1 - U -
examples.
Assignment operators
= x=5
+= x = x+3
-= x = x-3
*= x = x*3
Comparison operators
Equal (==) x == y
Not Equal (!=) x != y
Greater than (>) x >y
Less than (<) x <y
Logical operators
and x < 5 and x < 10
or x < 5 or x < 4
not not(x < 5 and x < 10)
2. Write a program to calculate the area of a circle with radius value of 10cm.
Program:
r = int(input(“Enter the radius value:”))
a = 3.14*r*r
print(“Area of the Circle is: ”,a,“ sq.m”)
Output:
Enter the radius value: 4
Area of the Circle is: 50.24 sq.m
Lists:
Lists can be thought of the most general version of a sequence in
Python. Unlike strings, they are mutable, meaning the elements inside a
list can be changed! Lists are constructed with brackets [] and commas
separating every element in the list.
For example:
list1 = [1,2,3]
list2 = [„Mechanical‟, „Engineering‟]
P Y T H O N
0 1 2 3 4 5
Example:
a = “Python”
b = a[0]
c = a[1]
d = a[2]
e = a[3]
print("Print the 0th index: ",b)
print("Print the 1st index: ",c)
print("Print the 2nd index: ",d)
print("Print the 3rd index: ",e)
Output:
Print the 0th index: P
Print the 1st index: y
Print the 2nd index: t
Print the 3rd index: h
String Slicing:
String slicing is used to grab everything from a string up to a
designated point. Slicing can be done by using : within the [] bracket
UNIT - II
1. Write a python program to print the word “WATERMELON” in vertical manner using for
loop.
Program:
a = int(input(“Enter the word:”))
b=len(a)
for i in range(1,b+1):
print(i)
Output:
Enter the word: WATERMELON
W
A
T
E
R
M
E
L
O
N
elif condition2:
code block 2
else:
code block 3
Where,
If condition1 evaluates to true, code block 1 is executed.
If condition1 evaluates to false, then condition2 is evaluated.
If condition2 is true, code block 2 is executed.
If condition2 is false, code block 3 is executed.
Example:
Program:
a=0
if a > 0:
print("Positive number")
elif a < 0:
print(''Negative number')
else:
print('Zero')
Output:
Zero
Output:
Shutting down
Shutdown aborted
Sorry
Program:
import random
List1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is: ",List1)
random.shuffle(List1)
print("The shuffled list is: ",List1)
Output:
The original list is: [1, 2, 3, 4, 5]
The shuffled list is: [2, 4, 5, 3, 1]
UNIT - IV
1. Define a class square using side and compute the area of square using constructor method.
Take side = 20 cm.
Program:
class Square:
def _ _init_ _(self):
self.L = int(input("Enter Length Value: "))
self.A = self.L * self.L
print("The Area of square is: ",self.A,"sq.cm")
obj = Square()
Output:
Enter Length Value: 20
The Area of square is: 40.0 sq.cm
3. Define a class rectangle using length and width and compute the area of rectangle using
constructor method. Take length = 20 cm, width = 10 cm.
Program:
class Rectangle:
def _ _init_ _(self):
self.L = int(input("Enter Length Value: "))
self.W = int(input("Enter Width Value: "))
self.A = self.L * self.W
print("The Area of rectangle is: ",self.A,"sq.cm")
obj = Rectangle( )
Output:
Enter Length Value: 20
Enter Width Value: 10
The Area of rectangle is: 200 sq.cm
UNIT - V
.Format method:
print('{0:<8} {1:^8} {2:>8}'.format(1,2,3))
print('{0:<8} {1:^8} {2:>8}'.format(4,5,6))
print('{0:<8} {1:^8} {2:>8}'.format(7,8,9))
Output:
1 2 3
4 5 6
7 8 9
F-string method:
x = 'John'
print(f'He said his name is {x}')
Output:
He said his name is John
Department of Mechanical Engineering, Sethu Institute of Technology Page 19
3. Explain Python notations with examples.
Handling an exception:
If you have some suspicious code that may raise an exception, you
can defend your program by placing the suspicious code in
a try: block. After the try: block, include an except: statement,
followed by a block of code which handles the problem.
Try and except statements are used to catch and handle exceptions
in Python. Statements that can raise exceptions are kept inside the try
clause and the statements that handle the exception are written inside
except clause.
Example Program: