0% found this document useful (0 votes)
3 views

Python Practical

Uploaded by

kaviikaviya2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Practical

Uploaded by

kaviikaviya2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

# Python program to display personal information

def display_information():

# Personal details

name = "John Doe"

address = "123 Main Street, Springfield, State, ZIP"

mobile_number = "+1-234-567-8901"

college_name = "Springfield University"

course_subjects = ["Mathematics", "Computer Science", "Physics",


"Chemistry"]

# Display the information

print("Personal Information:\n")

print(f"Name: {name}")

print(f"Address: {address}")

print(f"Mobile Number: {mobile_number}")

print(f"College Name: {college_name}")

print("Course Subjects:")

for i, subject in enumerate(course_subjects, start=1):

print(f" {i}. {subject}")

# Call the function to display the information

display_information()
OUTPUT:

Personal Information:

Name: John Doe

Address: 123 Main Street, Springfield, State, ZIP

Mobile Number: +1-234-567-8901

College Name: Springfield University

Course Subjects:

1. Mathematics

2. Computer Science

3. Physics

4. Chemistry

2. # Python program to find the largest of three integers using if-else

# Input three integers


num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
num3 = int(input("Enter the third integer: "))

if num1 >= num2 and num1 >= num3:


largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3

# Display the result


print(f"The largest number is: {largest}")
Example Run:

Enter the first integer: 45


Enter the second integer: 78
Enter the third integer: 23
The largest number is: 78

# Python program to display a series of positive numbers and their sum

# Initialize an empty list to store the numbers


numbers = []

print("Enter a series of positive numbers. Enter a negative number to stop:")

while True:
num = float(input("Enter a number: "))
if num < 0:
break
numbers.append(num)

# Display the numbers in order


print("\nNumbers entered:")
for number in numbers:
print(number)

# Calculate and display the sum


total_sum = sum(numbers)
print(f"\nThe sum of the numbers is: {total_sum}")

Example Run:

Enter a series of positive numbers. Enter a negative number to stop:


Enter a number: 10
Enter a number: 5.5
Enter a number: 3.2
Enter a number: -1

Numbers entered:
10.0
5.5
3.2

The sum of the numbers is: 18.7


4. # Python program to find the product of two matrices

# Input dimensions for matrices


m = int(input("Enter the number of rows in matrix A: "))
p = int(input("Enter the number of columns in matrix A / rows in matrix B: "))
r = int(input("Enter the number of columns in matrix B: "))

# Input matrix A
print("\nEnter the elements of matrix A (row by row):")
A = []
for i in range(m):
row = list(map(int, input(f"Row {i+1}: ").split()))
A.append(row)

# Input matrix B
print("\nEnter the elements of matrix B (row by row):")
B = []
for i in range(p):
row = list(map(int, input(f"Row {i+1}: ").split()))
B.append(row)

# Initialize the result matrix


C = [[0 for _ in range(r)] for _ in range(m)]

# Perform matrix multiplication


for i in range(m):
for j in range(r):
for k in range(p):
C[i][j] += A[i][k] * B[k][j]

# Display the resulting matrix


print("\nThe product of matrices A and B is:")
for row in C:
print(" ".join(map(str, row)))
Example Run:

Input:

mathematica
Copy code
Enter the number of rows in matrix A: 2
Enter the number of columns in matrix A / rows in matrix B: 3
Enter the number of columns in matrix B: 2

Enter the elements of matrix A (row by row):


Row 1: 1 2 3
Row 2: 4 5 6

Enter the elements of matrix B (row by row):


Row 1: 7 8
Row 2: 9 10
Row 3: 11 12

Output:

The product of matrices A and B is:


58 64
139 154

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy