0% found this document useful (0 votes)
5 views

Programs 6 to 10

The document contains Python programs for various tasks, including printing factors of a number, displaying numbers in a pattern, performing operations on lists, manipulating a list of fruits, and calculating statistical measures for temperatures of cities. Each section includes source code that demonstrates the implementation of these tasks using loops and list methods. The programs cover fundamental programming concepts such as input handling, list operations, and statistical calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Programs 6 to 10

The document contains Python programs for various tasks, including printing factors of a number, displaying numbers in a pattern, performing operations on lists, manipulating a list of fruits, and calculating statistical measures for temperatures of cities. Each section includes source code that demonstrates the implementation of these tasks using loops and list methods. The programs cover fundamental programming concepts such as input handling, list operations, and statistical calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

6) Write a python program to print the factors of a given number using ‘for’ loop

Source Code:

#program to print the factors of a given number


n=int(input("Enter any number"))
r=0
for a in range(1,n+1,1):
r=n%a
if(r==0):
print(a,"is a factor")
a+=1

7. Write a python program to display numbers from 1 to n in a pattern

Source Code:

#Program to display numbers from 1 to n in pattern


# Input number of rows
n = int(input("enter no. of rows"))
# Outer loop for number of rows
for i in range(1, n + 1):
# Inner loop to print numbers
for j in range(1, i + 1):
print(j, end=" ")
# Move to the next line after each row
print()

8) Write a Python program to perform the following operations on two lists:

(a) Define a list ‘list1’ with 5 elements: [12, 56, 89, 10, 33].

(b) Print all elements in ‘list1’.

(c) Print the first element in ‘list1’.

(d) Print the fourth element in ‘list1’.

(e) Print the last element in ‘list1’ using negative indexing.

(f) Print all elements in ‘list1’ using a for loop with indexing.

(g) Print all elements in ‘list1’ using negative indexing in a for loop.

(h) Define another list ‘list2’ with 5 elements: [23, 33, 44, 55, 11].

(i) Add ‘list1’ and ‘list2’ to create a new list ‘list3’ and print it.
Source Code:

#program to do list operations in python


#defining 5 elements in a list1
list1=[12,56,89,10,33]
#print elements in list1:
print("The elements in list1 are :", list1)
#print first element in list1
print("The first element in a list1 is :", list1[0])
#print 4th element in list1
print("The fourth element in list1 is :", list1[3])
#print last element in list1 using negative indexing
print("The last element in a list using negative indexing is :",list1[-1])
#print all elements using for loop
print("The elements in list1 using indexing are :")
for i in range(0,5):
print(list1[i])
#print all elements using for loop with negative indexing
print("The elements in list1 using negative indexing are :")
for i in range(-5,-1,1):
print(list1[i])
#defining 2nd list
print("The elements in list2 are :")
list2=[23,33,44,55,11]
print(list2)
#add first list and 2nd list and print 3rd list
list3=list1+list2
#printing 3rd list
print("The elements in third list after adding list1 and list2 are :")
print(list3)

9. Write a python program to create a list of fruits and perform list operations append,
insert, extend, pop, remove, sort and reverse elements in the fruits list

Source Code:

#program to create a list of fruits and perform list operations


fruits=['apple','grapes','mango']
#adding a fruit 'orange' using ‘append’
fruits.append('orange')
print("list of fruits after appending are :",fruits)
#inserting a fruit 'guava' using ‘insert’ in 2nd position
fruits.insert(2,'guava')
print("list of fruits after inserting are :",fruits)
#replacing a fruit 'banana' in 3rd positon using indexing
fruits[2]='banana'
print("list of fruits after inserting using indexing are :",fruits)
#extending fruits list with another list
fruits2=['pine apple','water melon']
fruits.extend(fruits2)
print("list of fruits after using extend",fruits)
#deleting an element in 'fruits' list using pop
fruits.pop()
print("list of fruits after deleting using pop",fruits)
#deleting element in a list using remove
fruits.remove('grapes')
print("list of fruits after deleting using remove",fruits)
#sorting elements in a list
fruits.sort()
print("display fruits list after sorting", fruits)
#reversing elements in a list
fruits.reverse()
print("display fruits list after reversing: ",fruits)
10. Write a python program to input temperature of 10 cities in a list and calculate
mean, median and mode

Source Code:
#program to input temperature and calculate mean, median, mode
import statistics
temp = [ ]
print('enter temperature of 10 cities :')
#input temperature of 10 cities using for loop
for a in range(0,10,1):
num=float(input(" "))
temp.append(num)
#calculating mean
x=statistics.mean(temp)
print("mean=",x)
#calculating median
y=statistics.median(temp)
print("median=",y)
#calculating mode
z=statistics.mode(temp)
print("mode =",z)

You might also like