0% found this document useful (0 votes)
14 views4 pages

Python Programming Exercises and Solutions

This document contains a case study with 4 questions about Python lists, tuples, and basic operations like append, count, and summation. The questions involve updating lists by appending new elements, adding a list to a list, using the count() method to count elements in a tuple, and computing the sum of elements in tuples within a list.

Uploaded by

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

Python Programming Exercises and Solutions

This document contains a case study with 4 questions about Python lists, tuples, and basic operations like append, count, and summation. The questions involve updating lists by appending new elements, adding a list to a list, using the count() method to count elements in a tuple, and computing the sum of elements in tuples within a list.

Uploaded by

Amirul Haikal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CASE STUDY 1 ( CHAPTER 4)

ANS THIS QUESTION IN GROUP OF TWO

Question 1
Given the list of an animal below, write down a command to add animal name to the list by
using append command.

# animals list
animals = [‘cat’, ‘dog’, ‘rabbit’]

Output

Updated animals list: ['cat', 'dog', 'rabbit', 'guinea pig']

Question 2
Write a program to add list to a list

# flower list
flowers = ['rose', 'vanilla', 'tulip']

# list of wild flowers


wild_flowers = ['lily', 'orchid']

Output
Updated flowers list: ['rose', 'vanilla', 'tulips', ['lily', 'orchid']]

Question 3

Write a program by using the count() method returns the number of times element appears
in the tuple below.
Dengan menggunakan arahan count(), tuliskan program untuk mengira berapa kali element
vowel wujud dalam tuple dibawah.

# vowels tuple
vowels = ('a', 'e', 'i', 'o', 'i', 'u')

Output :

The count of i is: 2


The count of p is: 0
Question 4

Write a Python program to compute the sum of all the elements of each tuple stored
inside a list of tuples.
Tulis program python untuk mengira jumlah element dalam setiap list tuple seperti
dalam paparan pada output.
Original list of tuples:
[(1, 2), (2, 3), (3, 4)]

Output :
Sum of all the elements of each tuple stored inside the said list of tuples:
[3, 5, 7]

You might also like