0% found this document useful (0 votes)
38 views8 pages

CSX3001 Class07 1 2024

Uploaded by

minkhanttin
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)
38 views8 pages

CSX3001 Class07 1 2024

Uploaded by

minkhanttin
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/ 8

CSX3001

FUNDAMENTALS OF COMPUTER
PROGRAMMING

CLASS 07 LISTS IN PYTHON


ARRAYS AND PYTHON LISTS, INDEXING, SLICING, AND LIST UPDATING

PYTHON

1
ARRAYS
In programming, an array is a collection of elements of the same type. Arrays are
popular in most programming languages, such as Java, C/C++, JavaScript, and so
on. However, they are not that common in Python. When people talk about
Python arrays, they often talk about Python lists. Python lists are much more
flexible than arrays. They can store elements of different data types, including
strings. Also, lists are faster than arrays.

Note: arrays of numeric values are supported in Python by the array module.

LISTS
A list is created by placing all the items (elements) inside a square bracket ([ ]),
separated by comma(,)s. List can contain any number of items and they may be
of different types.

# empty list
myList = []

#list of integers
intList = [-5,-3,-1,0,1,3, 5]

#list of mixed datatypes


mixList = [“Hello”, 7.545, True, 23]

2
LIST INDEX

Each item in a list can be accessed via the index operator ([ ]). The index starts
at 0. So, a list with 7 elements will have an index from 0 to 6.

listExample = ['e','x','a','m','p','l','e']

# p will be printed out


print(listExample[4])

# The following code fragment will print all items


for i in range(len(listExample)):
print(listExample[i])

# Different way but produce the same output


for item in listExample:
print(item)

Python allows negative indexing for its sequences. The index of -1 refers to the
last item, -2 to the second last item, and so on.

aList = [3, 6, 9, 12, 15]

# The last item in aList will be printed out, 15


print(aList[-1])

# The first item in aList will be printed out, 3


print(aList[-5])

3
LIST SLICING

Slicing operator(:) can be used with a sequence (list, tuple, string). The list can be
sliced as the same as the string.

bList = ['a',1,'b',2,'c',3,'d',4]

# What will be printed out?


print(bList[::2])
print(bList[1::2])
print(blist[2:5])

LIST UPDATING
List item(s) can be changed by assignment operator (=)

bList = ['a',1,'b',2,'c',3,'d',4]
#update an item
bList[2] = 'B'
#see the result by yourselves
print(bList)
#update some parts of bList
bList[6:8] = ['D',8]
print(bList)

List can be added an item by using append() method or added several items using
extend() method

cList = [0,2,4,6]
#add an item at the end of cList
cList.append(8)
#see the result by yourselves
print(cList)
#add more items at the end of cList
cList.extend([10,12,14])
print(cList)

4
Item(s) can be deleted from a list by using the keyword del or using the methods,
remove() or pop().

listExample = ['e','x','a','m','p','l','e','s']

del listExample[7]
print(listExample)

del listExample[2:4]

What is/are the difference(s) in remove() and pop() method?


How to use these methods?

SOME PYTHON LIST METHOND

Python List Methods

append() - Add an element to the end of the list

extend() - Add all elements of a list to another list

insert() - Insert an item at the defined index

remove() - Removes an item from the list

pop() - Removes and returns an element at the given index

clear() - Removes all items from the list

index() - Returns the index of the first matched item

count() - Returns the count of the number of items passed as an argument

sort() - Sort items in a list in ascending order

reverse() - Reverse the order of items in the list

copy() - Returns a shallow copy of the list

Note: more list methods can be found in the following link,


https://www.programiz.com/python-programming/methods/list

5
LIST EXERCISES

1) Write a Python program to sum all the items in a list

2) Write a Python program to multiply all the items in a list.

3) Write a Python program to get the largest number from a list. You cannot
use the max() and sort() functions.

4) Write a Python program to get the smallest number from a list. You are
not allowed to use the min() and sort() functions.

5) From any list of pre-defined strings, determine how many strings its length
is 2 or higher and the first and last characters are the same.
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result: 2

6) Write a Python program to turn a list of multiple integers into a single


integer.
Example:
Original List: [10,20,30]
Single Integer: 102030

7) Write a Python program to clone or copy a list.

6
Note:
If you want to read multiple integer inputs into a list:
x = [int(x) for x in input("Enter a multiple integer value: ").split()]
print("Number of list is: ", x)

If you want to read multiple inputs (a string data type) and store them in a list:
MyList2 = list((input("Enter a multiple value: ").split()))
print(MyList2)

8) Write a Python code to take several strings (words, numeric, etc.) as inputs
and determine the total number of strings whose length is a minimum of
5, and the first two and the last two characters (in reverse order and case
insensitive) of such string are not the same.
#TestRun-01

Enter words: Social Drive Market Tenet


The number of words that meet the requirement is: 3

#TestRun-02
Enter words: Good morning madam
The number of words that meet the requirement is: 2

7
CLASS#07 ASSIGNMENTS

1. Write a Python code to replace duplicated characters with “*” from the entered string
inputs.

#TestRun-01
Input: a p p l e
Output: a p * l e

#TestRun-02
Input: b a l l o o n
Output: b a l * o * n

------

2. Write Python code to create a list (and print all elements in the list as an output) by
concatenating each individual string (a single character) input with the number of stars
based on the input number. For the output, a character must be capitalized.

#TestRun-01
Input: a b c
Enter a value for n: 3
Output:A* A** A*** B* B** B*** C* C** C***

#TestRun-02
Input: a b c
Enter a value for n: 4
Output:A* A** A*** A**** B* B** B*** B**** C* C** C*** C****

You might also like