0% found this document useful (0 votes)
2 views6 pages

csc305 Lab2

The document is a lab report for CSC305 Programming Paradigms, focusing on Python programming concepts including strings, lists, tuples, dictionaries, and functions. It provides code examples for various operations and methods associated with these data types, along with exercises for practice. Additionally, it includes comparisons between C++ and Python code for specific tasks.

Uploaded by

sereinco23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

csc305 Lab2

The document is a lab report for CSC305 Programming Paradigms, focusing on Python programming concepts including strings, lists, tuples, dictionaries, and functions. It provides code examples for various operations and methods associated with these data types, along with exercises for practice. Additionally, it includes comparisons between C++ and Python code for specific tasks.

Uploaded by

sereinco23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSC305 – PROGRAMMING PARADIGMS

PYTHON: LAB 2

NAME : HANIS ADIBAH BINTI RIZA


STUDENT NO. : 2022465666
GROUP : RCDCS1104B

===== LESSON 1: STRING =====

myString = "This is a Python program."

#to print the whole string


print (myString)

#to print the elements in index 0 until 9


print (myString[0:10])

#to print the elements in index 15 until end of string


print (myString[15:])

#to print the elements from index 0 until 14


print (myString[:15])

#to print the string twice


print (myString * 2)

#to print the concatenation of 2 strings


print (myString + " And it is fun.")

Code 1: Several string operations in Python

Method Description Syntax

lower The lower() method returns the lowercased string.lower()


string from the given string. It converts
all uppercase characters to lowercase.

If no uppercase characters exist, it


returns the original string.

upper The upper() method returns the uppercased string.upper()


string from the given string. It converts
all lowecase characters to uppercase.

If no lowercase characters exist, it


returns the original string.

capitalize The capitalize() returns a string with string.capitalize()


first letter capitalized and all other
characters lowercased. It does not modify
the original string.

Table 1: Some built-in methods in Python for string manipulation

===== LESSON 2: LIST =====

myList = [11, 44, 99.56, 100, 245, 67, 'Mango', 'Turtles', 'Girls', 'Y']

PREPARED BY: NYCJ@KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU 1|P a g e


SOURCE FROM: [1] WWW.PROGRAMIZ.COM
CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB 2

#to print the whole list


print (myList)

#to print the elements in index 0 until 6


print (myList[0:7])

#to print the elements in index 5 until end of list


print (myList[5:])

#to print the elements from index 0 until 6


print (myList[:7])

#to print the list twice


print (myList * 2)

#to print the concatenation of 2 lists


print (myList + ['Pineapple', 'Fox', 98.78, 'P', 7])

Code 2: Several list operations in Python

Method Description Syntax

append The append() method adds a single item to list.append(item)


the existing list. It does not return a new
list; rather it modifies the original list.

insert The insert() method inserts an element to list.insert(index,


the list at a given index. element)

remove The remove() method searches for the given list.remove(element)


element in the list and removes the first
matching element.

reverse The reverse() method reverses the elements list.reverse()


of a given list.

Table 2: Some built-in methods in Python for list manipulation

===== LESSON 3: TUPLE =====

myTuple = ("Orange", "Honeydew", 89.08, 11.45, 678, 'A', 'c', 98.65,


"Earth", "Pluto")

#to print the whole tuple


print (myTuple)

#to print the elements in index 0 until 6


print (myTuple[0:7])

#to print the elements in index 5 until end of tuple


print (myTuple[5:])

#to print the elements from index 0 until 6


print (myTuple[:7])

#to print the tuple twice


print (myTuple * 2)

PREPARED BY: NYCJ@KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU 2|P a g e


SOURCE FROM: [1] WWW.PROGRAMIZ.COM
CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB 2

#to print the concatenation of 2 tuple


print (myTuple + ('Y', 'T', 'Cat', 65.11, 0.98))

Code 3: Several tuple operations in Python

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

===== LESSON 4: DICTIONARY =====

myDictionary = {"ID": 1011, "Name": "Aminah", "Part": 3, "Program":


"CS110"}
Key (must be Value
#to print the whole dictinary
print (myDictionary) unique)

#to print all the keys


print (myDictionary.keys())

#to print all the values


print (myDictionary.values())

#to print the value of ID


print (myDictionary["ID"])

#to add a new key and its value to the existing dictionary
myDictionary["CGPA"] = 3.45
print (myDictionary)

Code 4: Several dictionary operations in Python

===== LESSON 5: FUNCTION =====

#include<iostream>
using namespace std;

float sphereVol(float);

const float PI = 3.142; //global variable

int main()
{
float radius;

cout<<"Enter the radius: ";


cin>>radius;

float volume = sphereVol(radius);


cout<<"The volume is "<<volume<<endl;
}

float sphereVol(float r)
{
float vol = 4.0 / 3.0 * PI * r * r * r;

PREPARED BY: NYCJ@KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU 3|P a g e


SOURCE FROM: [1] WWW.PROGRAMIZ.COM
CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB 2

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)

Code 5: A function in C++ and its equivalent in Python

Exercise Question #1

Predict the output for the given codes:

myList = ['UiTM', 'Perlis', 40, 'Tahun']


print (myList[1:3])

['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.

Table given below might help you in writing your program:

API Air Pollution Level


0 - 50 Good
51 - 100 Moderate
101 - 200 Unhealthy
201 - 300 Very unhealthy
301+ Hazardous

Here are the requirements for the program:


i) The user will input the API in the main function
ii) A function named air_pollution_level() with determine the air pollution
level and display the appropriate message to 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")

PREPARED BY: NYCJ@KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU 4|P a g e


SOURCE FROM: [1] WWW.PROGRAMIZ.COM
CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB 2

else:
print ("Hazardous")

#main function
API = int(input("Enter API: "))

#func call
air_pollution_level(API)

Exercise Question #3

Given the following dictionary definition:

myDictionary =
{'Name':'Mohd Ali', 'ID Number': '2011889941', 'Group': 'CSD5Af'}

Expand the codes by:


i) Adding a new key named ‘Program’ whose value is ‘CS110’ to the dictionary
ii) Adding a new key named ‘Part’ whose value is ‘5’ to the dictionary
iii) Print all the items in the dictionary

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;

for (int counter = 0; counter < 10; counter++)


{
cout<<"Enter the gender: ";
cin.getline(gender, 10);

if(strcmp(gender, "male") == 0)
countM++;
else
countF++;
}

cout<<"\nNumber of male in the class = "<<countM<<endl;


cout<<"Number of female in the class = "<<countF<<endl;

countF = countM = 0

for counter in range(0,10):


gender = input("Enter the gender (male or female): ")

PREPARED BY: NYCJ@KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU 5|P a g e


SOURCE FROM: [1] WWW.PROGRAMIZ.COM
CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB 2

if gender.lower() == "male":
countM += 1
else:
countF += 1

print("The number of male in the class = ",{countM})


print("The number of female in the class = ", {countF})

PREPARED BY: NYCJ@KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU 6|P a g e


SOURCE FROM: [1] WWW.PROGRAMIZ.COM

You might also like