Professional Documents
Culture Documents
SP21 BCS 059 (Theory)
SP21 BCS 059 (Theory)
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.