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

Python Exercises Solutions

Uploaded by

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

Python Exercises Solutions

Uploaded by

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

Python Solutions for Serie 6 Exercises

Exercice 1: Separate Positive and Negative Numbers

N = int(input("Enter the dimension of the array (max 50): "))

T = []

TPOS = []

TNEG = []

print(f"Enter {N} integers:")

for i in range(N):

value = int(input(f"Value {i+1}: "))

T.append(value)

if value > 0:

TPOS.append(value)

elif value < 0:

TNEG.append(value)

print("Original Array T:", T)

print("Positive Numbers TPOS:", TPOS)

print("Negative Numbers TNEG:", TNEG)

Exercice 2: Find Max and Min Values and Their Positions

N = int(input("Enter the number of elements: "))

A = []

print(f"Enter {N} integers:")


for i in range(N):

value = int(input(f"Value {i+1}: "))

A.append(value)

max_value = max(A)

min_value = min(A)

max_position = A.index(max_value) + 1

min_position = A.index(min_value) + 1

print(f"Maximum Value: {max_value} at position {max_position}")

print(f"Minimum Value: {min_value} at position {min_position}")

Exercice 3: Sort Array in Ascending Order

N = int(input("Enter the number of elements: "))

A = []

print(f"Enter {N} integers:")

for i in range(N):

A.append(int(input(f"Value {i+1}: ")))

for i in range(N):

for j in range(0, N-i-1):

if A[j] > A[j+1]:

A[j], A[j+1] = A[j+1], A[j]

print("Sorted Array in Ascending Order:", A)


Exercice 4: Add Two Matrices

N = int(input("Enter the number of rows: "))

M = int(input("Enter the number of columns: "))

print("Enter values for Matrix A:")

A = [[int(input(f"A[{i+1}][{j+1}]: ")) for j in range(M)] for i in range(N)]

print("Enter values for Matrix B:")

B = [[int(input(f"B[{i+1}][{j+1}]: ")) for j in range(M)] for i in range(N)]

C = [[0 for j in range(M)] for i in range(N)]

for i in range(N):

for j in range(M):

C[i][j] = A[i][j] + B[i][j]

print("Resultant Matrix C (A + B):")

for row in C:

print(row)

Exercice 5: Multiply Two Matrices

N = int(input("Enter the number of rows for Matrix A: "))

M = int(input("Enter the number of columns for Matrix A / rows for Matrix B: "))

P = int(input("Enter the number of columns for Matrix B: "))


print("Enter values for Matrix A:")

A = [[int(input(f"A[{i+1}][{j+1}]: ")) for j in range(M)] for i in range(N)]

print("Enter values for Matrix B:")

B = [[int(input(f"B[{i+1}][{j+1}]: ")) for j in range(P)] for i in range(M)]

C = [[0 for _ in range(P)] for _ in range(N)]

for i in range(N):

for j in range(P):

for k in range(M):

C[i][j] += A[i][k] * B[k][j]

print("Resultant Matrix C (A * B):")

for row in C:

print(row)

Exercice 6: Analyze Text

TXT = input("Enter a line of text (max 200 characters): ")

print("Length of the string:", len(TXT))

words = TXT.split()

print("Number of words:", len(words))

uppercase_count = sum(1 for char in TXT if char.isupper())


print("Number of uppercase letters:", uppercase_count)

You might also like