chapter 6:
data structure (Array, list,
tuple, set)
2025/2026 [Link]@[Link]
Introduction :
● In Python, every value has a datatype:
○ Numbers are used to represent numeric values such as integers, floating numbers.
Numbers are the basic data type used for calculations, measurements, counting, and
other mathematical operations in Python..
○ List []is a collection which is ordered and changeable. Allows duplicate members.
○ Tuple ()is a collection which is ordered and unchangeable. Allows duplicate
members.
○ Set{} is a collection which is unordered, unchangeable, and unindexed. No duplicate
members.
○ Dictionary{} is a collection which is ordered and changeable. No duplicate members.
○ Strings are defined as an array of characters. 2
String
String
Definition:
A string is a data structure used to group a finite number of
characters under the same variable name, a finite number of
characters that can be in a single I/O operation.
Note: A character string can be used as a string variable, in which case you can read a word
in one step (Read(name)), or as an array of characters: browse the string character by
character.
4
String
Representation
5
Char
● A character is a type that
allows for the representation
of numeric characters,
alphabetic characters, and
special characters.
● Any character is represented in
memory as a numerical code
known as the ASCII code.
● The numerical codes of all
characters are collected in the
ASCII table.
6
Char
Coding in ASCII:
Each character has a hexadecimal
code and a decimal equivalent.
● Upper case letters have different
ASCII codes than lower case
letters.
○ Upper case letters have
decimal codes [65, 90].
○ Lowercase letter codes are in
the decimal range [97, 122].
○ The difference between an
upper and lower case letter is
always 32.
7
Char
Assignment of a character
● So that a character value is not confused with a variable name, it is enclosed in quotes: ‘ ‘.
● A character can be assigned to a variable of type character as to a variable of type integer
At the End of this algorithm:
● The variable a will contain the character 'c'
● The variable b will contain the ASCII code
of the character 'c' that is 99
8
String
Assignment of String
● So that a string value is not confused with a variable name, it is enclosed in quotation marks: “ ”
9
String in python
● One way to think about a string is as a list/collection of characters:
❏ name = "MSE Manouba"= ['M','S''E',' ''M',a'/n',o'/u',b','a']
❏ string in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
❏ You can assign a multiline string to a variable by using three quotes:
10
String in python
● Like many other popular programming languages, string in Python are arrays of bytes representing
unicode characters.
M
S
E
13
11
String in python
● Like many other popular programming languages, string in Python are arrays of bytes representing
unicode characters.
Yes, 'free' is present.
No, 'expensive' is NOT
present.
12
String in python
● We can convert a character to its ascii code using ord()
● the chr() function is used to convert an integer representing a Unicode code point into its
corresponding character.
13
String in python
● We can extract substring :
❏ Get the characters from position 2 to position 5:
❏ Get the characters from the start to position 5:
❏ Get the characters from position 2, and all the way to the end:
14
String in python
● We can extract substring :
❏ Use negative indexes to start the slice from the end of the string
orl
❏ The split() method splits the string into substring if it finds instances of the separator:
['Hello', 'World!']
❏ The find method is used to get the position. If the string does not exist so the method show -1
15
String in python
● We can Modify String
❏ The upper() method returns the string in upper case:
❏ The lower() method returns the string in lower case:
❏ The strip() method removes any whitespace from the beginning or the end:
❏ The replace() method replaces a string with another string:
16
Exercises
Exercises
18
Exercises
19
Exercises
20