0% found this document useful (0 votes)
13 views8 pages

Python Practical Exam: Code Examples

The document contains Python code examples demonstrating various Python programming concepts like arithmetic operators, assignment operators, comparison operators, logical operators, lists, conditional statements, functions, plotting charts etc. It includes 23 sections with code snippets and explanations to perform operations like calculating compound interest, factorials, plotting line charts and scatter plots using matplotlib library, reading and displaying data from CSV files.

Uploaded by

Dylan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Python Practical Exam: Code Examples

The document contains Python code examples demonstrating various Python programming concepts like arithmetic operators, assignment operators, comparison operators, logical operators, lists, conditional statements, functions, plotting charts etc. It includes 23 sections with code snippets and explanations to perform operations like calculating compound interest, factorials, plotting line charts and scatter plots using matplotlib library, reading and displaying data from CSV files.

Uploaded by

Dylan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical Exam

1. Arithmetic Operations

#WAP to demonstrate Arithmetic Operators


firstvar = int(input(" Enter the first number"))
secondvar = int(input(" Enter the second number"))
print('firstvar + secondvar =',firstvar+secondvar)
print('firstvar - secondvar =',firstvar-secondvar)
print('firstvar * secondvar =',firstvar*secondvar)
print('firstvar / secondvar =',firstvar/secondvar)
print('firstvar // secondvar =',firstvar//secondvar)
print('firstvar ** secondvar =',firstvar**secondvar)

2. Assignment Operators

#WAP to demonstrate Assignment Operators


firstvar = int(input(" Enter the first number"))
firstvar+=5
print('firstvar = firstvar + 5 =>',firstvar)
firstvar-=5
print('firstvar = firstvar - 5 =>',firstvar)
firstvar*=5
print('firstvar = firstvar * 5 =>',firstvar)
firstvar/=5
print('firstvar = firstvar / 5 =>',firstvar)
firstvar%=5
print('firstvar = firstvar % 5 =>',firstvar)
firstvar//=5
print('firstvar = firstvar // 5 =>',firstvar)
firstvar**=5
print('firstvar = firstvar ** 5 =>',firstvar)

3. Comparison operators

#WAP to demonstrate Comparison Operators


firstvar = int(input(" Enter the first number"))

Practical Exam 1
secondvar = int(input(" Enter the second number"))
print('firstvar == secondvar=>',firstvar == secondvar)
print('firstvar != secondvar =>',firstvar != secondvar)
print('firstvar > secondvar =>',firstvar > secondvar)
print('firstvar < secondvar =>',firstvar < secondvar)
print('firstvar >= secondvar =>',firstvar >= secondvar)
print('firstvar <= firstvar =>',firstvar <= secondvar)

4. Logical Operators

#WAP to demonstrate Logical Operators


firstvar = True
secondvar = True
print('firstvar AND secondvar =>',firstvar and secondvar)
print('firstvar OR secondvar =>',firstvar or secondvar)
print('Not firstvar =>',not (firstvar))

5. Compound Interest

#Code to calculate the Compound Interest


principle_amount=float(input("Enter the Principal amount : "))
roi=float(input("Enter the rate of interest : "))
time=float(input("Enter the time period : "))

compound_interest=principle_amount*((1+(roi/100))**time)
print("principle amount : ", principle_amount)
print("rate of interest : ", roi)
print("time : ", time)

print("value of compound interest : ", compound_interest)

6. Factorial of 10

num=10
#The factorial of a number is the product of all the integers from 1 to that number.
factorial=1*2*3*4*5*6*7*8*9*10
#print("The Factorial of "+str(num)+" is "+str(factorial))
print("The Factorial of ",num," is",factorial)

7. nsquare ncube

Practical Exam 2
n = int(input("Enter n: "))
n2, n3, n4 = n ** 2, n ** 3, n ** 4
print("n =", n)
print("n^2 =", n2)
print("n^3 =", n3)
print("n^4 =", n4)

8. WAP TO FIND THE SUM AND AVERAGE OF THE NUMBERS IN LIST


num=[1,2,3,4,5]
sum=num[0]+num[1]+num[2]+num[3]+num[4]
print("The first 5 natural numbers are ",num)
print("The sum of ", num, " is ",sum)
avg=sum/5
print("The average of ", num, " is ",avg)

9. list of first 5 natural numbers and the squares of odd numbers


num=[1,2,3,4,5]
print("The first 5 natural numbers are ", num)
square=[num[0]**2, num[2]**2, num[4]**2]
print("The squares of the odd numbers from first 5 natural numbers are : ",square)

10. list of first 5 multiples of number input by the user


num=int(input("Enter any number : "))
List=[num1, num2, num3, num4, num*5]
print("The first 5 multiples of ", num, " are ", List)

11. swap first and last element of the list using inbuilt function [Link]()

List=[24, 27, 17, 10, 28]


print("Initial List:")
print(List)

first=[Link](0)
last=[Link](-1)

[Link](0,last)
[Link](first)
print("\nNew swaped List:")
print(List)

Practical Exam 3
12. #Program to add an element at specified index in a list

list = [10, 20, 30]

print (list)
[Link] (1, "ABC")
print (list)

[Link] (3, "PQR")


print (list)
[Link] (5, "XYZ")
print (list)

[Link] (len (list) -1, 99)


print (list)

13. WAP to sort the integer numbers,float and string in the list

num = [10, 30, 40, 20, 50]

[Link]()
print (num)
fnum = [10.23, 10.12, 20.45, 11.00, 0.1]

[Link]()
print (fnum)
str = ["Banana", "Cat", "Apple", "Dog", "Fish"]

[Link]()
print (str)

14. WAP to find the position of min and max elements of a list in Python

list = [10, 1, 2, 20, 3, 50]

min = [Link] (min(list)


max = [Link] (max(list))

print ("position of minimum element: ", min)


print ("position of maximum element: ", max)

15. to check whether the entered number is odd or even.


num=int(input("Enter any number "))

Practical Exam 4
rem=num%2
if num==0:
print("The number is",num)
elif rem==0:
print(num, "is an even number")
else:
print(num, "is an odd number")

16. print empty lists

l=[]

if len(l)==0:
print("list is empty")
else:
print("list is not empty")

17. maximum of the given 3 numbers


num1=int(input("Enter the first number: "))
num2=int(input("Enter the second number: "))
num3=int(input("Enter the third number: "))
if(num1>num2 and num1>num3):
print(num1, "is maximum")
elif(num2>num1 and num2>num3):
print(num2, "is maximum")
else:
print(num3, "is maximum")

18. Calculator

menus
print("Calculator")
print("[Link]")
print("[Link]")
print("[Link]")
print("[Link]")

Practical Exam 5
input choice
ch=int(input("Enter Choice(1-4): "))

if ch==1:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a+b
print("Sum = ",c)
elif ch==2:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a-b
print("Difference = ",c)
elif ch==3:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a*b
print("Product = ",c)
elif ch==4:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a/b
print("Quotient = ",c)
else:
print("Invalid Choice")

19. import numpy and stats tools-manage packs


import numpy as np
a = [1,2,2,4,5,6]
print(" The mean of the list is", ([Link](a)))
print(" The median of the list is",([Link](a)))
import stats
m = [1,2,2,2,4,5,6,6]
x = [Link](m)
print(" The mode of the list is",x)

20. WAP to draw a linechart to find the unemployment rate

Practical Exam 6
import [Link] as plt
year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
unemployment_rate = [9.8, 12, 8, 7.2, 6.9, 7, 6.5, 6.2, 5.5, 6.3]

[Link](year, unemployment_rate)
[Link]('unemployment rate vs year')
[Link]('year')
[Link]('unemployment rate')
[Link]()

21. WAP to display a scatter chart for the following points (2,5),(9,10),(8,3),(5,7),(6,18).
import [Link] as plt
x= [2,9,8,5,6]
y= [5,10,3,7,18]
[Link](x,y)
[Link]()

22. to display line chart from (2,5) to (9,10).


import [Link] as plt
x = [2,9]
y = [5,10]
[Link](x, y)
[Link]('X vs Y')
[Link]('X')
[Link]('Y')
[Link]()

23. WAP to Read csv file saved in your system and display its information

import csv
with open('[Link]') as file:
reader = [Link](file)
for row in reader:
print(row)

24. to identify the shape using Python


import cv3
image = [Link]('[Link]')
print("The shape of the image is",[Link])

Practical Exam 7
print("The size of the image is",[Link])
print("The data type of the image is",[Link])

25. to read an image and display using Python


from PIL import Image
#Read image
im = [Link]( '[Link]' )
#Display image
[Link]()

Practical Exam 8

Common questions

Powered by AI

Using 'pop()' and 'insert()' allows for efficient removal and re-insertion of elements. Swapping elements like the first and last in a list using these methods ('first = List.pop(0)', then 'List.insert(0, last)' and vice versa for 'last') helps dynamically alter the sequence, which is essential in situations like data reformation and optimized storage management .

Arithmetic operators in Python allow for operations such as addition, subtraction, multiplication, division, floor division, and exponentiation between two integers. If you have two integers, say 5 and 3, the results would be: addition (5 + 3 = 8), subtraction (5 - 3 = 2), multiplication (5 * 3 = 15), division (5 / 3 ≈ 1.67), floor division (5 // 3 = 1), and exponentiation (5 ** 3 = 125).

The 'cv2.imread()' method reads the image, and using attributes like '.shape', '.size', and '.dtype' tells you its dimensions, total number of pixels, and data type respectively. These attributes are essential in image processing for tasks like image manipulation, transformations, and applying correct algorithms .

The method to identify the index of a minimum or maximum value in a Python list is using 'list.index(min(list))' for the minimum and 'list.index(max(list))' for the maximum. This functionality is beneficial in data analysis for locating significant points within datasets efficiently .

The compound interest in Python is calculated using the formula 'P * (1 + r/n)^(nt)', where P is the principle amount, r is the annual interest rate, and t is the time in years. This is significant as it provides a more accurate growth estimation of an investment over time, considering interest compounding .

Using Python's CSV library, data from a file is read with 'csv.reader(file)'. Rows are iterated through for display or other operations. This process is crucial for handling large datasets efficiently in data analytics, ensuring structured data parsing and manipulation for insights extraction .

Sorting integers, floats, and strings in Python can be done using the 'list.sort()' method, which orders them in ascending order. Sorting is crucial for data organization and efficient retrieval, especially in operations that depend on ordered data, such as binary search algorithms .

Using 'matplotlib.pyplot', trends like unemployment rates are graphically represented through line charts by plotting years against unemployment rates. This is crucial for visually decoding economic patterns, aiding analysts and policymakers in making informed decisions .

To determine if a list is empty in Python, you can use the conditional 'if len(list) == 0'. If true, it indicates that the list is empty. This is useful in scenarios where operations depend on the presence of elements in the list, allowing for checks before proceeding with computational tasks .

Logical operators are used to combine Boolean values and evaluate expressions. In Python, with two Boolean values such as True and False, the logical AND operator results in False (since both need to be true for the result to be true), the logical OR operator results in True (as at least one true value is enough), and the NOT operator inverts the truth value, making NOT True evaluate to False .

You might also like