CSX3001 Class07 1 2024
CSX3001 Class07 1 2024
FUNDAMENTALS OF COMPUTER
PROGRAMMING
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]
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']
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.
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]
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]
5
LIST EXERCISES
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
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
#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****