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

Python Assignment

The document contains a series of Python programming exercises completed by Arun Kumar, a B.Com (Hons) student. Each program addresses different programming concepts, including basic arithmetic, control structures, functions, and data visualization using libraries like NumPy and Matplotlib. The document includes code snippets and expected outputs for each exercise.

Uploaded by

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

Python Assignment

The document contains a series of Python programming exercises completed by Arun Kumar, a B.Com (Hons) student. Each program addresses different programming concepts, including basic arithmetic, control structures, functions, and data visualization using libraries like NumPy and Matplotlib. The document includes code snippets and expected outputs for each exercise.

Uploaded by

devspc28
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

Name: Arun Kumar

Roll No: 25504037


Course: [Link] (Hons)
Subject: Programming Using Python (Electronics)

Program 1: Write a program that takes 2 numbers as


command line arguments and prints its sum.
Code:
n1 = int(input("Enter first Number you want to add: "))
n2 = int(input("Enter second Number you want to add: "))
s = n1 +n2
print("First number is: ",n1)
print("Second number is: ",n2)
print("Sum of the two number is: ",s)

Output:
Program 2: Write a program for checking whether the given
number is an even number or not.
Code:
x = int(input("Enter a number you like: "))
if x%2 ==0:
print("The number",x,"is an even number")
else:
print("The number",x,"is an odd number")
Output:

Program 3: Write a program to purposefully raise an


Indentation Error and Correct it. Show the significance of
indentation in Python code blocks.
Code:

for i in range(3):
print("Inside loop")
print("Outside loop")
Output:
Program 4: Write a program using a while loop that asks the
user for a number, and prints a countdown from that number to
zero.
Code:
n = int(input("enter the number: "))
while n >=0:
print(n)
n-=1
Output:

Program 5: Using a for loop, write a program that prints out


the decimal equivalents of 1/2, 1/3, 1/4, 1/10.
Code:
fractions = [2, 3, 4, 10] # denominators
for f in fractions:
print(f"1/{f} = {1/f}")
Output:
Program 6: Write a program to take the value of temperature
(in Celsius) from the user, calculate the equivalent value of
temperature in fahrenheit and display both the values.
Code:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"Temperature in Celsius: {celsius}")
print(f"Temperature in Fahrenheit: {fahrenheit}")
Output:

Program 7: Write a program to calculate the average of three


numbers and print the results.
Code:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
average = (num1 + num2 + num3) / 3
print(f"The average of {num1}, {num2}, and {num3} is
{average}")
Output:
Program 8: Write a program to define a function that
calculates the average value of the elements of a list variable
of arbitrary length and print the results.
Code:
def calculate_average(numbers):
if len(numbers) == 0:
return 0
return sum(numbers) / len(numbers)
my_list = [10, 20, 30, 40, 50]
average_value = calculate_average(my_list)
print(f"The list is: {my_list}")
print(f"The average value of the elements is:
{average_value}")
Output:

Program 9: Write a program to count the numbers of


characters in the string and store them in a dictionary data
structure.
Code:
s = input("Enter the string: ")
d = {}
c=0
for i in s:
c += 1
d[s] = c
print(d)
Program 10: Write a program to find all prime numbers
between 1 and 20, and display the result.
Code:
for i in range(2, 21):
count = 0
for j in range(2, i):
if i % j == 0:
count = count + 1

if count == 0:
print(i, "is prime")
else:
print(i, "is not prime")

Output:
Program 11: Write a program to generate Fibonacci Series up
to the first 25 terms starting from 0, 1.

Code:
num = int(input("Enter how many terms you want to print: "))
n1, n2 = 0, 1

print("Fibonacci sequence:")
if num >= 1:
print(n1, end=" ") # print 0
if num >= 2:
print(n2, end=" ") # print 1

for i in range(2, num):


n3 = n1 + n2
print(n3, end=" ")
n1, n2 = n2, n3

Output:
Program 12: By considering the terms in the Fibonacci
sequence whose values do not exceed four million, find the
sum of the even-valued terms.

Code:
# Program to find the sum of even Fibonacci numbers up to 4
million
limit = 4000000
a, b = 1, 2
total = 0

while b <= limit:


if b % 2 == 0:
total += b
a, b = b, a + b
print("Sum of even Fibonacci numbers up to 4 million:", total)

Output:
Program 13: Write a program to generate scatter-plot using
numpy and matplotlib libraries.

Code:
import numpy as np
import [Link] as plt
x = [Link](50) # 50 random values for x-axis
y = [Link](50) # 50 random values for y-axis
[Link](x, y, color='blue', marker='o')
[Link]("X-axis values")
[Link]("Y-axis values")
[Link]("Scatter Plot using NumPy and Matplotlib")
[Link]()

Output:
Program 14 : Write a program to generate bar-plot
(histogram) using numpy and matplotlib libraries.

Code:
import numpy as np
import [Link] as plt
data = [Link](1000) # 1000 random values from a
normal distribution
[Link](data, bins=30, color='skyblue', edgecolor='black')
[Link]("Value")
[Link]("Frequency")
[Link]("Histogram using NumPy and Matplotlib")
[Link]()

Output:
Program 15 : Write a program to generate pie-chart using
numpy and matplotlib libraries.

Code:
import numpy as np
import [Link] as plt
sizes = [Link]([20, 30, 25, 25])
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
colors = ['red', 'yellow', 'pink', 'brown']
[Link](sizes, labels=labels, colors=colors, autopct='%1.1f%%',
startangle=90)
[Link]("Pie Chart using NumPy and Matplotlib")
[Link]('equal')
[Link]()

Output:
Program 16 :Write a program to generate 3-dimensional plot
using numpy and matplotlib libraries.

Code:
import [Link] as plt
import numpy as np
[Link]('_mpl-gallery')
n=100
xs=[Link](0,1,n)
ys=[Link](xs*6*[Link])
zs=[Link](xs*6*[Link])
fig, ax= [Link](subplot_kw={"projection": "3d"})
[Link](xs,ys, zs)
[Link](xticklabels=[],
yticklabels=[],
zticklabels=[])
plt. show()

Output:
Program 17: Write a program to count the frequency of
characters in a given file.
Code:
f = open("[Link]", "r")
text = [Link]()
[Link]()
freq = {}
for ch in text:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
for ch in freq:
print(repr(ch), ":", freq[ch])
Output:
Program 18: Write a program to print each line of a file in
reverse order.

Code:
with open("[Link]", "r") as f:
lines = [Link]()

for line in lines:


print(line[::-1])

Output:

You might also like