0% found this document useful (0 votes)
12 views17 pages

Python Assignment SBTE

This document is a Python programming assignment for a Diploma in Computer Science & Engineering course, covering 15 programs across various units. Each program includes code snippets and their corresponding outputs, focusing on fundamental concepts such as data types, control structures, string and list operations, functions, and NumPy basics. The assignment is prepared for SBTE Bihar by Deepak.

Uploaded by

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

Python Assignment SBTE

This document is a Python programming assignment for a Diploma in Computer Science & Engineering course, covering 15 programs across various units. Each program includes code snippets and their corresponding outputs, focusing on fundamental concepts such as data types, control structures, string and list operations, functions, and NumPy basics. The assignment is prepared for SBTE Bihar by Deepak.

Uploaded by

satyamk95087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Python Programming Assignment

Course: Diploma in Computer Science & Engineering - Semester III

Topic: Complete syllabus coverage — 15 programs with outputs

Prepared for: SBTE Bihar

Prepared by: DEEPAK


Table of Contents
1. 1. Program 1: Hello, Data Types & Input/Output (Unit 1)
2. 2. Program 2: Operators and Precedence Calculator (Unit 1)
3. 3. Program 3: Grade Calculator using Conditional Statements (Unit 2)
4. 4. Program 4: Factorial (for loop) (Unit 2)
5. 5. Program 5: Prime Checker (while, break/continue) (Unit 2)
6. 6. Program 6: String Operations (Unit 3)
7. 7. Program 7: List Operations (search, count) (Unit 3)
8. 8. Program 8: Tuple demonstration (Unit 3)
9. 9. Program 9: Set Operations (union, intersection, difference) (Unit
3)
10. 10. Program 10: Dictionary operations (CRUD) (Unit 3)
11. 11. Program 11: Functions: default args, *args, **kwargs (Unit 4)
12. 12. Program 12: Recursive Fibonacci (Unit 4)
13. 13. Program 13: Modules & Packages (math, random) (Unit 4)
14. 14. Program 14: NumPy basics and linear solve (Unit 5)
15. 15. Program 15: NumPy array operations (Unit 5)
Program 1: Hello, Data Types & Input/Output (Unit 1)
Code:

# Program 1: Hello, Data Types & Input/Output


name = 'Deepak'
age = 20
pi = 3.14159
print('Hello,', name)
print('Age:', age)
print('Type of pi:', type(pi))

Output:

Output:

Hello, Deepak
Age: 20
Type of pi: <class 'float'>
Program 2: Operators and Precedence Calculator (Unit 1)
Code:

# Program 2: Operators and Precedence Calculator


a = 10; b = 3; c = 2
res = a + b * c # multiplication before addition
print('a + b * c =', res)
res2 = (a + b) * c
print('(a + b) * c =', res2)

Output:

Output:

a + b * c = 16
(a + b) * c = 26
Program 3: Grade Calculator using Conditional Statements (Unit 2)
Code:

# Program 3: Grade Calculator using Conditional Statements


marks = 78
if marks >= 90:
grade = 'A+'
elif marks >= 75:
grade = 'A'
elif marks >= 60:
grade = 'B'
elif marks >= 50:
grade = 'C'
else:
grade = 'F'
print('Marks =', marks, 'Grade =', grade)

Output:

Output:

Marks = 78 Grade = A
Program 4: Factorial using for loop (Unit 2)
Code:

# Program 4: Factorial using for loop


n = 6
fact = 1
for i in range(1, n+1):
fact *= i
print('Factorial of', n, 'is', fact)

Output:

Output:

Factorial of 6 is 720
Program 5: Prime Checker (Unit 2)
Code:

# Program 5: Prime Checker using while loop


n = 29
if n < 2:
print(n, 'is not prime')
else:
i = 2
is_prime = True
while i * i <= n:
if n % i == 0:
is_prime = False
break
i += 1
print(n, 'is prime' if is_prime else 'is not prime')

Output:

Output:

29 is prime
Program 6: String Operations (Unit 3)
Code:

# Program 6: String operations


s = 'Hello, Python!'
print('Length:', len(s))
print('Slice [7:13]:', s[7:13])
print('Upper:', s.upper())
print('Replace:', s.replace('Python','World'))

Output:

Output:

Length: 14
Slice [7:13]: Python
Upper: HELLO, PYTHON!
Replace: Hello, World!
Program 7: List Operations (Unit 3)
Code:

# Program 7: List operations - search and count frequency


lst = [3, 5, 2, 3, 7, 3, 9]
print('List:', lst)
print('Index of 7:', lst.index(7))
print('Count of 3:', lst.count(3))
# Linear search for 9
found = False
for i, val in enumerate(lst):
if val == 9:
found = True
print('9 found at index', i)
break
if not found:
print('9 not found')

Output:

Output:

List: [3, 5, 2, 3, 7, 3, 9]
Index of 7: 4
Count of 3: 3
9 found at index 6
Program 8: Tuple demonstration (Unit 3)
Code:

# Program 8: Tuple demonstration (immutability)


t = (1, 2, 3)
print('Tuple:', t)
try:
t[0] = 10
except TypeError as e:
print('Error (immutable):', e)

Output:

Output:

Tuple: (1, 2, 3)
Error (immutable): 'tuple' object does not support item assignment
Program 9: Set Operations (Unit 3)
Code:

# Program 9: Set operations


a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print('Union:', a | b)
print('Intersection:', a & b)
print('Difference a - b:', a - b)

Output:

Output:

Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference a - b: {1, 2}
Program 10: Dictionary operations (Unit 3)
Code:

# Program 10: Dictionary CRUD and frequency mapping


d = {'apple': 3, 'banana': 5}
print('Initial dict:', d)
d['orange'] = 2 # create
d['banana'] = 6 # update
print('After add/update:', d)
del d['apple'] # delete
print('After delete:', d)
# frequency mapping from list
fruits = ['apple','banana','apple','orange','banana','banana']
freq = {}
for f in fruits:
freq[f] = freq.get(f, 0) + 1
print('Frequency map:', freq)

Output:

Output:

Initial dict: {'apple': 3, 'banana': 5}


After add/update: {'apple': 3, 'banana': 6, 'orange': 2}
After delete: {'banana': 6, 'orange': 2}
Frequency map: {'apple': 2, 'banana': 3, 'orange': 1}
Program 11: Functions: default args, *args, **kwargs (Unit 4)
Code:

# Program 11: Functions with default args, *args, **kwargs


def greet(name, msg='Hello'):
return f'{msg}, {name}!'
def summation(*args):
return sum(args)
def info(**kwargs):
return kwargs

print(greet('Deepak'))
print(greet('Deepak', 'Hi'))
print('Sum:', summation(1,2,3,4))
print('Info:', info(age=20, branch='CSE'))

Output:

Output:

Hello, Deepak!
Hi, Deepak!
Sum: 10
Info: {'age': 20, 'branch': 'CSE'}
Program 12: Recursive Fibonacci (Unit 4)
Code:

# Program 12: Recursive Fibonacci


def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)

print([fib(i) for i in range(8)]) # first 8 Fibonacci numbers

Output:

Output:

[0, 1, 1, 2, 3, 5, 8, 13]
Program 13: Modules & Packages (Unit 4)
Code:

# Program 13: Modules & Packages (math, random)


import math
import random
print('sqrt(16)=', math.sqrt(16))
print('random choice from list:', random.choice([10,20,30,40]))
# demonstrating try/except on import (safe practice)
try:
import statistics
print('statistics module available')
except ImportError:
print('statistics not available')

Output:

Output:

sqrt(16)= 4.0
random choice from list: 20 # (example - actual may vary)
statistics module available
Program 14: NumPy basics and linear solve (Unit 5)
Code:

# Program 14: NumPy basics and linear solve


import numpy as np
A = np.array([[3,1],[1,2]])
b = np.array([9,8])
x = np.linalg.solve(A, b)
print('A:\n', A)
print('b:', b)
print('Solution x:', x)

Output:

Output:

A:
[[3 1]
[1 2]]
b: [9 8]
Solution x: [2. 3.]
Program 15: NumPy array operations (Unit 5)
Code:

# Program 15: NumPy array operations


import numpy as np
arr = np.array([1,2,3,4])
print('arr:', arr)
print('arr * 2 =', arr * 2)
print('arr + arr =', arr + arr)
print('mean =', arr.mean())

Output:

Output:

arr: [1 2 3 4]
arr * 2 = [2 4 6 8]
arr + arr = [2 4 6 8]
mean = 2.5

You might also like