csc305 Lab2
csc305 Lab2
PYTHON: LAB 2
myList = [11, 44, 99.56, 100, 245, 67, 'Mango', 'Turtles', 'Girls', 'Y']
Lists Tuples
Lists are enclosed in brackets ([ ]) Tuples are enclosed in parentheses ( )
Their elements & size can be changed Their elements cannot be updated
Table 3: Differences between lists and tuples for Python
#to add a new key and its value to the existing dictionary
myDictionary["CGPA"] = 3.45
print (myDictionary)
#include<iostream>
using namespace std;
float sphereVol(float);
int main()
{
float radius;
float sphereVol(float r)
{
float vol = 4.0 / 3.0 * PI * r * r * r;
return vol;
}
PI = 3.142
def sphereVol(r):
return 4.0 / 3.0 * PI * radius ** 3
Power of 3
#main function
radius_ = input("Enter the radius: ")
radius = float (radius_)
volume = sphereVol(radius)
print ("The volume of the sphere is ", volume)
Exercise Question #1
['Perlis', 40]
Exercise Question #2
Write a program in Python that is able to display the air pollution level based on
Air Pollutant Index (API) input by the user.
def air_pollution_level(index):
if index <= 50:
print ("Good")
elif index <= 100:
print ("Moderate")
elif index <= 200:
print ("Unhealthy")
elif index <= 300:
print ("Very Unhealthy")
else:
print ("Hazardous")
#main function
API = int(input("Enter API: "))
#func call
air_pollution_level(API)
Exercise Question #3
myDictionary =
{'Name':'Mohd Ali', 'ID Number': '2011889941', 'Group': 'CSD5Af'}
myDictionary["Program"] = "CS110"
myDictionary["Part"] = 5
print(myDictionary)
Exercise Question #4
Convert the given code segment written in C++ into its equivalent code segment in
Python.
char gender[10];
int countF, countM;
countF = countM = 0;
if(strcmp(gender, "male") == 0)
countM++;
else
countF++;
}
countF = countM = 0
if gender.lower() == "male":
countM += 1
else:
countF += 1