Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name: Jayanth Jaidev

Student id:94929

Program assignment-1 Due: Oct-10 at 11:59 PM

1- Design a program that asks the user to enter a series of 20


numbers. The program should store the numbers in a list then
display the following data:

 The lowest number


 The highest number
 The total of the numbers in the list
 The average of the numbers in the list

(Answer)

(i) Python program to find the smallest number in the series:

# Python program to find smallest number in a list

l=[l for l in input("List:").split(",")]


print("The list is ",l)

# Assign first element as a minimum.


min1 = l[0]

for i in range(len(l)):

# If the other element is min than first element


if l[i] < min1:
min1 = l[i] #It will change
print("The smallest element in the list is ",min1)

if (i<len(l)):
print(“enter”,len(l)-I,”more numbers.”)
elif(i>len(l))
print(“You have reached the limit of the needed numbers.”)

Output:
List:1,2,3,4,5,6,7,8,9,12,32,21,46,33,43,55,66,77,76,43
The list is ['1', '2', '3', '4', '5', '6', '7', '8', '9', '12', '32', '21', '46', '33', '43', '55', '66', '77', '76', '43']
The smallest element in the list is 1

Screenshot:

(ii) Python program to find the lowest number:

def myMax(list1):

# Assume first number in list is largest


# initially and assign it to variable "max"
max = list1[0]

# Now traverse through the list and compare


# each number with "max" value. Whichever is
# largest assign that value to "max'.
for x in list1:
if x > max :
max = x

# after complete traversing the list


# return the "max" value
return max

# Driver code
list1 = [1,2,0,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,122]
print(list1)
print("Largest element is:", myMax(list1))

Output:
>>>
= RESTART: C:/Users/Jayanth Jaidev/AppData/Local/Programs/Python/Python38-32/greatest.py
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 122]
Largest element is: 122

Screenshot:

(iii) Python program to find the sum of the numbers in the list:

# Python code to demonstrate the working of


# sum()

numbers = [1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,88,99,145]

# start parameter is not provided


Sum = sum(numbers)
print("Sum of the numbers in the list is:", Sum)

Output:
Sum of the numbers in the list is: 685

Screenshot:

(iv) Python program to find the average of the numbers:

# Python code to demonstrate the working of


# sum()

numbers = [1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,88,99,145]

# start parameter is not provided


Sum = sum(numbers)
print("Sum of the numbers in the list is", Sum)
Average= Sum/20
print("Average is", Average)

Output:
Sum of the numbers in the list is 685
Average is 34.25

Screenshot:
2- In a program, write a function that accepts two arguments: a list
and a number n. Assume that the list contains numbers. The
function should display all the numbers in the list that are greater
than the number n.

(Answer) #call the main function

def main():
#declares local variables

n = 14

number_list = [1,2,3,6,5,8,11,7,12,16,18,23,45,65,87,77]

#displays the number

print('Number:', n)

#displays the list of numbers

print('List of numbers:\n', number_list, sep='')

#Display the list of numbers that are larger than the given number

print('List of numbers that are larger than',number, ':', sep='')

#Call the larger_than_n_list function,

#passing a number and number list as arguments.

display_larger_than_n_list(number, number_list)

# The display_larger_than_n_list function accepts two arguments:

#a list, and a number. The function displays all of the numbers

#in the list that are greater than that number.

def display_larger_than_n_list(n, n_list):

#Declare an empty list

larger_than_n_list = []

#Loop through the values in the list.

for value in n_list:

#Determine if a value is greater than n.

# dont know what goes here


#if so, append the value to the list

if (value > n):

larger_than_n_list.append(value)

#Display the list.

print(larger_than_n_list)

#Call the main function

main()

Output:
Number: 14
List of numbers:
[1, 2, 3, 6, 5, 8, 11, 7, 12, 16, 18, 23, 45, 65, 87, 77]
List of numbers that are larger than 14:
[16, 18, 23, 45, 65, 87, 77]

Screenshot:
3- Write a turtle graphics program to display 20 circles centered at
location 0, 0 (all of them). Increase the radius of each
consecutive circle by 20. First circle should have radius 20,
second radius 40, and so on.
import turtle

turtle.penup()
for i in range(1, 400, 20):
turtle.right(90)

# Move one radius


turtle.forward(i)

# Back to start heading


turtle.right(270)

# Put the pen back down


turtle.pendown()
# Draw a circle
turtle.circle(i)

# Pen up
turtle.penup()

# Head back to the start position


turtle.home()

Screenshot:

You might also like