0% found this document useful (0 votes)
3 views14 pages

Name - Jaiminkumar Manishbhai Solanki ROLL NO - 25149

The document contains Python code examples for various matrix operations, string manipulations, and calculations such as addition of matrices, upper and lower diagonal elements, counting characters in strings, and generating salary slips. Each section includes code snippets along with their respective outputs. The document is structured with numbered sections, showcasing different programming tasks and their results.

Uploaded by

known9591
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)
3 views14 pages

Name - Jaiminkumar Manishbhai Solanki ROLL NO - 25149

The document contains Python code examples for various matrix operations, string manipulations, and calculations such as addition of matrices, upper and lower diagonal elements, counting characters in strings, and generating salary slips. Each section includes code snippets along with their respective outputs. The document is structured with numbered sections, showcasing different programming tasks and their results.

Uploaded by

known9591
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

~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

~PYTHON CODE WITH INPUT OUTPUT

10. Addition of Two Matrices

# Program to add two matrices

A = [[1,2,3],[4,5,6],[7,8,9]] B = [[9,8,7],[6,5,4],[3,2,1]] result = [[A[i][j]

+ B[i][j] for j in range(len(A[0]))] for i in range(len(A))] print("Matrix

Addition Result:") for r in result:

print(r)

-Output:

[10, 10, 10]

[10, 10, 10]

[10, 10, 10]

11. Upper Diagonal of Matrix A =

[[1,2,3],[4,5,6],[7,8,9]] print("Upper

Diagonal Elements:") for i in

range(len(A)): for j in

range(len(A)): if i <= j:

print(A[i][j], end=" ") print()

(1)

-Output:
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

123

56

12. Lower Diagonal of Matrix A =

[[1,2,3],[4,5,6],[7,8,9]] print("Lower

Diagonal Elements:") for i in

range(len(A)): for j in

range(len(A)): if i >= j:

print(A[i][j], end=" ") print()

-Output:

45

789

13. Transpose of Matrix

A = [[1,2,3],[4,5,6],[7,8,9]] transpose = [[A[j][i] for j in

range(len(A))] for i in range(len(A[0]))] print("Transpose Matrix:")

for r in transpose:

(2)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

print(r)

-Output:

[1, 4, 7]

[2, 5, 8]

[3, 6, 9]

14. Count Spaces in String s =

"Hello World Python Program" spaces =

[Link](" ") print("Spaces:", spaces)

-Output:

Spaces: 3

15. Count Digits, Alphabets,

Spaces, Special Characters s = "Hello 123

@World" alpha = digit = space =

special = 0 for ch in s: if [Link](): alpha

+= 1 elif [Link]():

digit += 1 elif

[Link]():

(3)

space += 1

else:

special += 1 print("Alphabets:", alpha, "Digits:", digit, "Spaces:", space,

"Special:", special)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

-Output:

Alphabets: 10 Digits: 3 Spaces: 2 Special: 1

16. Count Vowels s = "Hello

World" vowels = "aeiouAEIOU" count =

sum(1 for ch in s if ch in vowels)

print("Vowels:", count)

-Output:

Vowels: 3

17. Prime Number using UDF def

is_prime(n):

if n < 2: return False for i in

range(2, int(n**0.5)+1):

if n % i == 0: return False

return True

(4)

num = 29 print(num, "is Prime?" , is_prime(num))

-Output:

29 is Prime? True

18. Zero, Positive, Negative using UDF def

check_num(n):
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

if n == 0: return "Zero"

elif n > 0: return "Positive"

else: return "Negative"

print(check_num(-5))

-Output:

Negative

19. Sum of n Even Numbers using UDF def

sum_even(n):

return sum([i for i in range(2, 2*n+1, 2)]) print("Sum

of first 5 even numbers:", sum_even(5))

-Output:

Sum of first 5 even numbers: 30

(5)
20. Reverse String without Function s =

"Python" rev = "" for ch in s:

rev = ch + rev print("Reversed:",

rev)

-Output:

Reversed: nohtyP

21. Palindrome Check s = "madam" rev = "" for ch in

s: rev = ch + rev print("Palindrome?", s == rev)


~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

-Output:

Palindrome? True

22. Reverse Number without String num

= 1234

rev = 0 while

num > 0:

(6)

rev = rev*10 + num%10 num //= 10

print("Reversed:",

rev)

-Output:

Reversed: 4321

23. Salary Slip basic = 25000 if basic >

20000:

hra = 0.20*basic; da = 0.10*basic; pf = 0.05*basic else:

hra = 0.15*basic; da = 0.10*basic; pf = 0.02*basic

net = basic + hra + da - pf print("Net Salary:", net)

-Output:

Net Salary: 33750.0

24. Menu Driven Even/Odd/Numbers n = 10 choice


~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

= 1 if choice

== 1:

print([i for i in range(1,n+1) if i%2==0])

(7)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

elif choice == 2: print([i for i in

range(1,n+1) if i%2!=0]) else:

print(list(range(1,n+1)))

-Output (choice=1):

[2, 4, 6, 8, 10]

25. Menu Driven Calculator a, b = 10, 5

choice = 1 if choice == 1:

print("Addition:", a+b) elif choice ==

2: print("Subtraction:", a-b) elif

choice == 3: print("Multiplication:",

a*b) elif choice == 4: print("Division:",

a/b)

-Output (choice=1):

Addition: 15

26. String Operations + Patterns s =

"Hello" print("Lower:", [Link]())

print("Upper:", [Link]())

print("Length:", len(s))

# Pattern 1 for i in

range(1,5):

print(str(i%2)*i)

(8)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

# Pattern 2 for i in

range(1,5):

print("".join(str(j) for j in range(1,i+1)))

# Pattern 3 for i in

range(1,5):

print("".join(chr(64+j) for j in range(1,i+1)))

# Pattern 4 for i in

range(4,0,-1):

print(str(i)*i)

Output (Patterns):

1ST:

00

111

0000

(9) 2ND:

12

123

1234
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

3RD:

AB

ABC

ABCD

4TH:

4444

333

22

27. Average of List lst =

[10,20,30,40] avg = sum(lst)/len(lst)

print("Average:", avg)

(10)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

-Output:

Average: 25.0

28. Marksheet using CSV import

csv with

open("[Link]","w",newline=""

) as f: w = [Link](f)

[Link](["sno","sname","m1","m2","m3"])

[Link]([1,"Dev",80,90,85])

[Link]([2,"Raj",70,75,80]) with

open("[Link]") as f:

r = [Link](f)

for row in r:

print(row)

-Output:

['sno','sname','m1','m2','m3']

['1','Dev','80','90','85']

['2','Raj','70','75','80']

29. Salary Slip with Line Chart

import csv import

[Link] as plt with

open("[Link]","w",newline="")

as f: w = [Link](f)

[Link](["eno","ename","basic"])

(11)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

[Link]([1,"Dev",30000])

[Link]([2,"Raj",25000])

[Link]([3,"Amit",28000])

names, basics = [], [] with

open("[Link]") as f: r=

[Link](f)

print("Salary Slip:") for row

in r:

eno, ename, basic = row["eno"], row["ename"], int(row["basic"])

hra = 0.20 * basic da = 0.10 * basic pf = 0.05 * basic

net = basic + hra + da - pf print(f"Eno:{eno}, Name:{ename},

Basic:{basic}, Net Salary:{net}") [Link](ename)

[Link](basic) [Link](names, basics, marker="o",

color="green") [Link]("Employee Name") [Link]("Basic

Salary") [Link]("Employee vs Basic Salary") [Link](True)

[Link]()

-OUTPUT:

Salary Slip:

Eno:1, Name:Dev, Basic:30000, Net Salary:40500.0

Eno:2, Name:Raj, Basic:25000, Net Salary:33750.0

(12)
~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

Eno:3, Name:Amit, Basic:28000, Net Salary:37800.0

30. Salary Slip for Managers in Computer Dept + Bar Chart

import csv import [Link] as plt with

open("[Link]", "w", newline="") as f:

w = [Link](f)

[Link](["eno","ename","designation","dept_name","basic"])

[Link]([1,"Dev","Manager","Computer",30000])

[Link]([2,"Raj","Clerk","Computer",20000])

[Link]([3,"Amit","Manager","Mechanical",28000])

[Link]([4,"Neha","Manager","Computer",35000])

dept_salary = {} with open("[Link]") as f:

r = [Link](f) print("Salary Slip

(Managers in Computer Dept):") for row in r:

eno, ename, designation, dept, basic = row["eno"], row["ename"],


row["designation"], row["dept_name"], int(row["basic"]) if
[Link]() == "manager" and [Link]() == "computer":

hra = 0.20 * basic

(13)

da = 0.10 * basi pf = 0.05 * basic net = basic + hra +

da - pf print(f"Eno:{eno}, Name:{ename},

Dept:{dept}, Basic:{basic}, Net Salary:{net}")

dept_salary[dept] = dept_salary.get(dept, 0) + basic


~NAME -JAIMINKUMAR MANISHBHAI SOLANKI

~ROLL NO -25149

[Link](dept_salary.keys(), dept_salary.values(),

color="skyblue") [Link]("Department Name")

[Link]("Basic Salary") [Link]("Department vs

Basic Salary (Managers in Computer Dept)")

[Link]()

-OUTPUT:

Salary Slip (Managers in Computer Dept):

Eno:1, Name:Dev, Dept:Computer, Basic:30000, Net Salary:40500.0

Eno:4, Name:Neha, Dept:Computer, Basic:35000, Net Salary:47250.0

(14)

You might also like