You are on page 1of 4

Introduction to ICT

Submitted To: Mam Beenish Noor.


Submitted By: Ibrahim Akbar.
Reg no: SP21-BCS-059
Class/Section: BCS-1(B1)
Department: Computer Science.

Comsats University Islamabad, Wah Campus.


Question 1:
Part A
Create a list of temperatures in degrees Celsius with the values 25.2, 16.8, 31.4, 23.9, 28,
22.5, and 19.6, and assign it to a variable called temps.

a) Using one of the list methods, sort temps in ascending order


Solution:
temps=[25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6]
print("Celsius list:",temps)
temps.sort()
print("Sorted list:",temps)

b) Using slicing, create two new lists, cool_temps and warm_temps, which contain the
temperatures below and above 20 degrees Celsius, respectively.
Solution:
cool_temps=temps[:2]
warm_temps=temps[2:]
print("Cool temperature list:",cool_temps)
print("Warm temperature list:",warm_temps)

c) Using list arithmetic, recombine cool_temps and warm_temps in into a new list called
temps_in_celsius.
Solution:
temps_in_celsius=cool_temps+warm_temps
print("Temperature in celsius list:",temps_in_celsius)
temps_in_fahrenheit=[]

d) Write a for loop to convert all the values from temps_in_celsius into Fahrenheit, and
store the converted values in a new list temps_in_fahrenheit. The list temps_in_celsius
should remain unchanged.
Solution:
s = float(9/5)
for m in range (len(temps_in_celsius)):
fahrenheit = ((temps_in_celsius[m] *s) + 32)
temps_in_fahrenheit.append(fahrenheit)
print("Temperature in Fahrenheit =" , temps_in_fahrenheit)
Part B
Create a function that performs the following tasks. Make separate user defined functions
for each task and call them in main () function.
a) Adds up all the positive values in a list of integers.
b) Remove duplicates from the list.
c) Count all the even numbers in that list.
d) Count all the odd numbers in list.

a) Adds up all the positive values in a list of integers.


Solution:
def add_pos(mylist):
    list_of_positive=[]
    for i in mylist:
        if i>0:
           list_of_positive.append(i)
    print("List of positive integers:",list_of_positive)

b) Remove duplicates from the list.


Solution:
def duplicates(mylist):
    unique=[]
    for i in mylist:
        if i not in unique:
            unique.append(i)
    print("List of unique integers:",unique)

c) Count all the even numbers in that list.


Solution:
def count_even(mylist):
    count=0
    for i in mylist:
        if i%2==0:
          count+=1
    print("Count of even numbers:",count)

d) Count all the odd numbers in list.


Solution:
def count_odd(mylist):
    count=0
    for i in mylist:
       if i%2!=0:
           count+=1
    print("Count of odd numbers:",count)

You might also like