0% found this document useful (0 votes)
11 views6 pages

Python Programs for Basic Calculations

Uploaded by

Pradnya Trimbake
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)
11 views6 pages

Python Programs for Basic Calculations

Uploaded by

Pradnya Trimbake
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

Python assignment 1

Program 1

Write a Python Program to Calculate the Average of Numbers in a Given List.

li=[1,2,3,4,5,6,7,8,9,10]
print(li)
sum=0
length=len(li)
for i in range(0,length):
sum=sum+i
print("sum=",sum)
avg=sum/length
print("average of list=",avg)

or
Example 1
numbers = [30, 55, 3, 10, 2]

average = sum(numbers)/len(numbers)

print("Average of list: ", round(average,3))

or
import statistics

list = [2, 4, 6, 6, 8, 10]


average = [Link](list)
print("The mean of the list is:", mean)

or
import numpy as np

list = [2, 4, 6, 6, 8, 10]


mean = [Link](list)
print("The mean of the list is:", mean)
or
list = [2, 4, 6, 8, 10].
sum = 0
for num in list:
sum += num
average = sum / len(list)
print("The average of the list is:", average)
Program2
Write a program which accepts 6 integer values and prints “DUPLICATES”
if any of the values entered are duplicates otherwise it prints “ALL
UNIQUE”.
Example: Let 5 integers are (32, 10, 45, 90, 45, 6) then output
“DUPLICATES” to be printed

values = []
for i in range(6):
value = int(input("Enter an integer value: "))
[Link](value)

# Check for duplicates


if len(values) != len(set(values)):
print("DUPLICATES")
else:
print("ALL UNIQUE")

or
list=[]
for i in range(0,6):
n1=int(input("enter the value"))
[Link](n1)
print(list)
if(len(list) != len(set(list))):
print("duplicate")
else:
print("unique")
Program 3

Program 4:
Write a program which finds sum of digits of a number.
Example n=130 then output is 4 (1+3+0).

n=int(input("enter the no"))


li=[]
while(n>0):
r=n%10
print(r)
n=n//10
print(n)
[Link](r)
print(li)
sum=0
for i in li:
sum=sum+i
print(sum)
program 5
5) Write a program which prints Fibonacci series of a number.

n=int(input("enter the n"))


f1=1
f2=1
print(f1,f2,end=' ')
for i in range(1,n):
f3=f1+f2
print(f3,end=' ')
f1=f2
f2=f3

Set B
1 Write a program which accept an integer value „n‟ and display all prime
numbers till „n‟.

n=int(input("enter the n"))


i=1
j=2
print(i,j,end=' ')
for i in range(3,n):
print(end=' ')
f=0
for j in range(2,i):
if(i%j==0):
f=1
if(f==0):
print(i,end= ' ')

2) Write a program that accept two integer values and if both are equal then prints
“SAME identity” otherwise prints, “DIFFERENT identity”.

v1=int(input("enter the value"))


v2=int(input("enter the value"))
v3=v1
if(v1==v2):
print("same identity")
else:
print("diffrent identity")

3) Write a program to display following pattern.


1234
123
12
1

n=int(input("enter the n"))


i=n+1
j=1
for i in range(i,1,-1):
print()
for j in range(1,i):
print(j,end=' ')

4) Write a program to reverse a given number.


n=int(input("enter the n"))
li=[]
while(n>0):

r1=n%10
[Link](r1)
n=n//10
for i in li:
print(i,end='')

OR
n1 = 1234
rev_num = int(str(n1)[::-1])

print(n1)
print(rev_num)
OR
n1 = 6789
rev = 0

while n1 > 0:
digit = n1 % 10
rev = rev * 10 + digit
n1 //= 10

print(6789)
print(rev)

OR

n1 = 6789
rev = 0

for _ in str(n1):
digit = n1 % 10
rev = rev * 10 + digit
n1 //= 10

print(6789)
print(rev)

Set C]
1) Write a Sequential search function which searches an item in a sorted list. The
function should return the index of element to be searched in the list

li=[1,2,3,4,5,6,7,8,9]
print(li)
key=int(input("enter the key val"))
f=0
for i in li:
if(key==i):
print("no is found",i)
f=1
break
if(f==0):
print("not found")

Common questions

Powered by AI

The program identifies duplicate integers by appending input values to a list and then comparing the list's length with a set created from that list. If the lengths differ, it indicates the presence of duplicates. This approach leverages Python's set data structure, which inherently removes duplicate entries, indicating the efficiency of sets in handling uniqueness by comparison of sizes between list and set .

The importance of using 'while' loops in Python programs, as demonstrated, is due to their ability to iterate based on a condition that could change dynamically during execution, suitable for situations where loop count is not predetermined, like reversing numbers or summing digits . 'For' loops are preferred when the number of iterations is known beforehand, such as processing known list items or generating sequences like the Fibonacci series. 'While' loops offer flexibility, whereas 'for' loops provide more concise syntax for fixed iterations.

The document describes various methods for reversing a number: (1) iterative digit extraction with a modulus operation and list append, (2) converting the number to a string, reversing it, and converting back to an integer, and (3) an iterative approach that reconstructs the number by adding extracted digits in reverse order . Converting to a string may be less memory efficient due to string manipulations, while direct arithmetic reversal is more space-efficient and can handle larger integers without the overhead of datatype conversion.

The program determines if two given integer values are the same by using a simple equality operator `==`. When the values are equal, it prints "SAME identity"; otherwise, it prints "DIFFERENT identity" . Potential applications of such a comparison function include validation checks in authentication systems, ensuring data integrity in data transfer, and simplifying control flow decisions in larger programs.

The sequential search function works by iterating through each element of the sorted list and checking if it matches the target element. If found, it prints the element and breaks the loop, otherwise it continues until the element is located or the list ends . This method is inefficient for larger datasets because it has a linear search time complexity of O(n), requiring traversal of up to all list elements, compared to more efficient logarithmic search methods like binary search for sorted lists.

The process for generating the Fibonacci series up to a given number involves initializing the first two terms of the series, `f1` and `f2`, to 1. A loop is used to calculate each subsequent term by adding the preceding two numbers, then printing the new term. Variables are updated accordingly by reassigning the values of `f1` and `f2` for the computation of the next term .

The algorithm to find the sum of a number's digits involves iteratively separating the digits by taking the modulus of 10, appending the digits to a list, and summing the list elements . For large numbers, this process could be optimized by using generator expressions to avoid explicit list creation for holding digits, thus reducing memory usage and potentially improving speed, especially when processing streams or larger datasets.

The program for calculating the average of numbers in a list handles different data sets by iterating through each element of the list, summing them, then dividing by the length of the list to compute the average. For illustration, Python's built-in `sum()` and `len()` functions are used directly in some examples . Libraries such as `statistics` and `numpy` are also used, where `statistics.mean()` and `numpy.mean()` can directly compute the mean of a list, showcasing more efficient approaches with external libraries .

The logic behind forming the displayed pattern involves using nested loops: the outer loop decrements from `n+1` to 1, while the inner loop prints numbers from 1 up to but not including the current outer loop index. This creates lines of numbers with decremental length, forming a triangle-like pattern .

The program finds prime numbers up to a specified integer by using a nested loop structure. The outer loop iterates over potential prime candidates, while the inner loop checks divisibility from 2 to the candidate minus one. If no divisors are found within this range, the number is considered prime. This approach has computational implications as it uses a naive trial division method, where its time complexity is approximately O(n^2) for n numbers, indicating inefficiency for large inputs .

You might also like