You are on page 1of 7

basic prog

Ianrain N. Buen

G9-Mendeleev

LISTS, TUPLE and SET in Python

Write python codes to manipulate lists and sets in python.

Specific Objectives

1. manipulate values of variables in lists, tuple and set.


2. Write a python code and specify the output.

Materials

 Pen
 Bond paper or intermediate paper

Lesson

What is list in python?

List
Lists are used to store multiple items in a single variable.

1
Lists are one of 4 built-in data types in Python used to store collections of data, the
other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

Lists are created using square brackets:

How do we use list?

Review of previous discussion, this time with numbers.

-6 -5 -4 -3 -2 -1
2 6 654 8 23 765
0 1 2 3 4 5

combining two lists

Note: List is mutable. It means it can change the values.

Insert -- in between

Append -- add in the end

remove --- omit a value in the list

2
pop with index number

--delete/remove element base on index number

pop without an index number

-- delete/remove the last element

delete multiple values

add multiple values

Getting the sum, minimum and maximum value in the list;

3
Sort and Reverse

List can be integer, float or strings.

TUPLES and SETS

- immutable

How to use tuple?

*note: tuple does not support reassigning of values

Count -- number of occurence

Set -- a collection of unique elements

Note: Set does not follow sequence(not sorted)

4
EXERCISES

Directions:Write your answers on the space provided. Make sure to submit your HSMGW
on time.

BASIC
Differentiate lists from tuple.
- Lists are just like the arrays, declared in other languages. Lists need not be
homogeneous always which makes it a most powerful tool in Python. In Python,
the list is a type of container in Data Structures, which is used to store multiple
data at the same time. Lists are a useful tool for preserving a sequence of data and
further iterating over it. Tuple is also a sequence data type that can contain
elements of different data types, but these are immutable in nature. In other words,
a tuple is a collection of Python objects separated by commas. The tuple is faster
than the list because of static in nature.

Lists from set.


- Sets are another standard Python data type that also store values. The major
difference is that sets, unlike lists or tuples, cannot have multiple occurrences of
the same element and store unordered values.
Set from tuple.
- Tuple is a collection of Python objects much like a list. The sequence of values
stored in a tuple can be of any type, and they are indexed by integers. Values of a
tuple are syntactically separated by ‘commas’. Although it is not necessary, it is
more common to define a tuple by closing the sequence of values in parentheses.
Set is an unordered collection of data type that is iterable, mutable, and has no
duplicate elements. The major advantage of using a set, as opposed to a list, is that
it has a highly optimized method for checking whether a specific element is
contained in the set.

INTERMEDIATE

Fill in the table with the correct code/output:


2 points each for Intermediate and 4 points each for Advanced level.

Code Output
>>> xx = [6,23,765,3,65,87,90,34,54,2005] [[6, 23, 765, 3, 65, 87, 90, 34, 54,
>>> yy = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t', 2005], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'u','v','w','x','y','z'] 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
>>>zz = [xx,yy] 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']]

5
>>> xx[1] 23

>>> 'i'
>>> xx.pop() 2005
>>> xx.insert(4,78) [6, 23, 765, 3, 78, 65, 87, 90, 34,
54, 2005]
[6, 23, 765, 3, 78, 65, 87, 90, 34,
>>> xx.append(1) 54, 2005, 1]
3211
>>> sum(xx)
1
>>> min(xx)
>>> max(xx) 2005
[6, 23, 765, 3, 78, 65, 87, 90, 34,
>>> xx[:10] 54]
ADVANCED:
>>> yy[15] + yy[2] + yy[18] + yy[7] + yy[18] pcshs
>>> yy[2] + str(xx[0]) c6
>>>yy[15] + yy[17] + yy[14] + yy[6] + ' ' + str (min(xx)) prog 1
>>>yy[15] + yy[17] + yy[14] + yy[6] + yy[17] + yy[0] + yy[12] + yy[12] programming 1
+ yy[8] + yy[13] + yy[6] + ' ' + str ( min(xx))

pcshs year 2005


>>> yy[15] + yy[2] + yy[18] + yy[7] + yy[18] + ' ' + yy[24] + yy[4] +
yy[0] + yy[17] + ' ' + str (max(xx))

Closure

A. Please check the box if you can do it already.

 I can recognize the arithmetic operators used in the program.


 I can create a program flowchart using these operators.

6
B. If you have questions in mind, please use the space below to address it.
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

References
https://www.guru99.com/variables-in-python.html

You might also like