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

Practical Python Programs

The document contains a series of Python programs demonstrating various functionalities such as finding maximum numbers, checking Armstrong numbers, generating multiplication tables, and more. It also includes Python viva questions covering basic concepts like variables, lists, and statistical terms like mean and median. Each program is accompanied by sample outputs to illustrate their functionality.

Uploaded by

nkffsmaintenance
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)
4 views8 pages

Practical Python Programs

The document contains a series of Python programs demonstrating various functionalities such as finding maximum numbers, checking Armstrong numbers, generating multiplication tables, and more. It also includes Python viva questions covering basic concepts like variables, lists, and statistical terms like mean and median. Each program is accompanied by sample outputs to illustrate their functionality.

Uploaded by

nkffsmaintenance
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

Python Programs with Sample Output

1. Write a program to find the maximum number out of the given three different numbers?
n1=int(input("Enter theNumber1:"))
n2=int(input("Enter the Number2:"))
n3=int(input("Enter the Number3:"))
if n1>n2 and n1>n3:
print(n1, " - Number 1 is greater")
elif n2>n1 and n2>n3:
print(n2, " - Number 2 is greater") elif n3>n1 and n3>n2:
print(n3, " - Number 3 is greater") else:
print("All are same")

2. Write a program to check whether the entered number is Armstrong or not?


n=int(input("Enter number to check:"))
#Store the original number into temporary variable t=n
s=0
#Computing the sum of cube of each digit and iterating until n=0
while n!=0:
r=n%10
s=s+(r**3)
n//=10
#Checking & displaying whether armstrong or not
if t==s:
print(s," is Armstrong number")
else:
print(s," is not an Artmstrong number")
3. Write a program to print a multiplication table of the entered number?
num=5
for a in range(1,11);
print(num,’x’ ,a, ‘=’,num*a)
Output:
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
5x10=50

4. Write a program to input age from user and print if as per thr input-age,one is eligible to vote or
not?

Age= int (input(“Enter age:”))


If age>=18;
Print(“18 and more you are eligible to vote”)
else:
Print(“under 18,you are not eligible to vote”)
Output:
Enter age 20
18 and more you are eligible to vote
Enter age 16
Under 18,you are not eligible to vote

5. Write a program to print squares of all numbers present in a list [8, 2, 7, 11, 20, 6]?
Numbers=[8,2,7,11,20,6]
Sq=0
For num in numbers;
Sq=num*num
Print(“Number:”,num,”square:”,sq)
Output:
Number:8 square:64
Number:2 square:4
Number:7 square:49
Number:11 square:121
Number:20 square:400
Number:6 square:36

6. Write a program to input two numbers and swap them?


n1=int(input(“Enter number 1:”))
n2=int(inpt(“Enter number 2:”
Print(“original numbers:”,N1,N2)
n1,n2=n2,n1
Print(“After swapping:”N1,N2)
Output:
Enter the number 1=17
Enter the number 2=28
Original numbers:17 28
After swapping:28 17

7. Write a program to input a number and print its cube?


Num=int(input(“Enter a number:”))
Cube=num*num*num
Print(“Number is”, num)
Print(“Its cube is”,cube)
Output:
Enter a number:7
Number is 7
Its cube is 343

8. Write a program to input two numbers and check if the two numbers are equal?
a=int(input(“Enter number 1:”)
b=int(input(“Enter number 2:”)
if a==b:
print(“Both numbers are equal”)
else
print(“Numbers are not equal”)
Output:
Enter number 1:7
Enter number 2:7
Both numbers are equal
Enter number 1:7
Enter number 2:17
Number are not equal

9. Program that takes number and check whether the given number is odd or even?
Num=int(input(“Enter an integer:”)
If num%2==0:
Print (num,”is Even number”)
Else
Print(num,”is ODD number”)
Output:
Enter an integer:8
8 is Even number

[Link] a program to calculate factorial of a number, the factorial of a positive number “n” is?
Num=int(input(“Enter a number:”)
If num<0;
Print(“No factorial for Negative number”)
elif num==0;
print(“The factorial of 0 is 1”)
else
factorial=1
for i in range(1,num+1);
factorial=factorial*i
print(“The factorial of”,num,”is”,factorial)
Output:
Enter a number:-5
No factorial for negative numbers
Enter a number:0
The factorial of 0 is 1
Enter a number:5
The factorial of 5 is 120
[Link] a program to input a number and print how many digits are there in the given number?
num=int(input(“Enter a number(>100):”)
count=0
while num !=0;
num=num // 10
count=count+1
print(“Total digits are:”,count)
Output:
Enter a number(>100):765
Total digitsl are:3
Enter a number(>100):89456
Total digits are:5
Enter a number(>100):8900
Total digits are :4

[Link] to add the elements of the two lists


list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]
result = [a + b for a, b in zip(list1, list2)] print("Resultant List:", result)
Sample Output:
Resultant List: [11, 22, 33, 44, 55]

[Link] to calculate mean, median and mode using Numpy


import numpy as np from scipy import stats
data = [1, 2, 3, 4, 4, 5, 6]
mean = [Link](data) median = [Link](data)
mode = [Link](data, keepdims=True).mode[0] print("Mean:", mean)
print("Median:", median) print("Mode:", mode)
Sample Output:
Mean: 3.5714285714285716
Median: 4.0
Mode: 4

[Link] to display line chart from (2,5) to (9,10)


import [Link] as plt x = [2, 9]
y = [5, 10]
[Link](x, y, marker='o')
[Link]("Line Chart")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Sample Output:
Line chart window with a line from point (2,5) to (9,10).

[Link] to display scatter chart


import [Link] as plt x = [2, 9, 8, 5, 6]
y = [5, 10, 3, 7, 18]
[Link](x, y, color='red', marker='o')
[Link]("Scatter Plot")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Sample Output:
Scatter chart window with given points plotted.

[Link] CSV file and display 10 rows


import pandas as pd
df = pd.read_csv("your_file.csv")
print([Link](10))
Sample Output:
First 10 rows of the CSV file displayed in the console.

[Link] CSV file and display its information


import pandas as pd
df = pd.read_csv("your_file.csv")
print([Link]())
Sample Output:
Data Frame info such as column names, non-null counts, and dtypes.

[Link] an image and display using Python


import cv2
image = [Link]("your_image.jpg")
[Link]("Image", image)
[Link](0)
[Link]()
Sample Output:
Image window displaying the selected image.

[Link] an image and identify its shape


import cv2
image = [Link]("your_image.jpg")
print("Image shape:", [Link]) # (height, width, channels)
Sample Output:
Image shape: (1080, 1920, 3)

[Link] Detection?
labels = ["cat", "dog", "car"]
frame = "A small dog is running"
detected = [obj for obj in labels if obj in [Link]()]
print("Detected:", detected)
Sample Output:
Detected: ['dog']
Python Viva Questions
1. What is Python?

Python is a high-level, easy-to-read programming language used for AI, data science,
and application development.
2. What is a variable?

A variable is a name that stores a value.


Example: x = 10
3. What is the use of indentation in Python?

Indentation shows the block of code. Python uses spaces instead of { }.


4. What is a list?

A list is a collection of items.


Example: [1, 2, 3]
5. What is the difference between list and tuple?

List is editable (mutable)


Tuple is not editable (immutable)
Statistics Data Viva Questions
6. What is mean?

Mean is the average of all numbers.


7. What is median?

Median is the middle value when data is arranged in order.


8. What is correlation?

Correlation shows the relationship between two variables (positive, negative, or zero).
9. What is frequency?

Frequency tells how many times a value appears in the dataset.

You might also like