v Tuple
v String
Tuple
Tuple
a sequence of objects, like list
defined with or without using () t = (1,2,3,4)
cannot change the values t = 1,2,3,4
(immutable data type) print(t[3])
append, extend, remove, reverse,
[Link](5) ⇒ ERROR
sort functions cannot be used
Tuple
Mutable and Immutable Data Types
Numbers, strings, tuples, boolea Lists are mutable objects
ns are immutable objects
a = (1,2) a = [1,2]
b=a b=a
b = (3,4) [Link](3)
print(a) print(a)
Mutable/Immutable Data Types
Immutable Data Types
Frames Objects
number, string, boolean, tuple
1
E.g.,
1. a = (1,2) 2
2. b = a
3. a = (1, 2, 3)
3
[Link]
Mutable/Immutable Data Types
Mutable Data Types
Frames Objects
list, set, dictionary
1
E.g.,
1. a = [1,2] 2
2. b = a
3. [Link](3)
3
[Link]
Mutable/Immutable Data Types
Why mutable and immutable?
Mutable objects are great to use when you need to change the size of
the object
Immutables are used when you need to ensure that the object you
made will always stay the same
Immutable are quicker to access than mutable objects and consumes
less memory
Mutable/Immutable Data Types
List Copy
Frames Objects
To keep the original list, use list()
function 1
E.g.,
1. a = [1,2] 2
2. b = list(a)
3. [Link](3)
3
String
String
is a sequence of characters
>>> a = 'abcdefgh'
immutable data type >>> a[0]
'a'
[0] [1] [2] [3] [4] [5] [6] [7] >>> a[5]
a
'f'
‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ >>> a[1] = 'x'
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
a[1] = 'x'
TypeError: 'str' object does not support it
em assignment
Slicing
String
Slicing is to get a substring
[0] [1] [2] [3] [4] [5] [6] [7]
a[ s : e ] a ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’
slices a sequence starting from
index s to index e-1
a[1:5] =>
a[s:] a[5:] =>
a[:e] a[:3] =>
a[:] a[:] =>
String and While
Example: Month Abbreviation
Program replaces the name m = ['January','February','March','April','May',
of each month with its three- 'June','July','August','September','October',
letter abbreviation. 'November','December']
[Run]
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']
String
bao = ['Le Bao', ‘Ai Bao', 'Fu Bao', 'Hui Bao', 'Rui Bao']
in print('Fu Bao' in bao)
family = “'Le Bao', Ai Bao', 'Fu Bao', 'Hui Bao', 'Rui Bao'”
x in y checks if x is included in y print('Fu Bao' in family)
a = 'Fu' in bao
can be used for sequence data
b = 'Fu' in family
type, list, tuple, string print(a)
print(b)
[Run]
False
True
String
String methods
[Link](), [Link]() -- returns the lowercase or uppercase version of the string
[Link]() -- returns a string with whitespace removed from the start and end
[Link]()/[Link]()/[Link]() -- tests if all the string chars are in the various
character classes
[Link]() -- returns True if the string contains only alphabets or digits, and return
False otherwise
[Link]('other'), [Link]('other') -- tests if the string starts or ends with the
given other string
[Link]('other') -- searches for the given other string within s, and returns the first index
where it begins or -1 if not found
[Link] ('old', 'new') -- returns a string where all occurrences of 'old' have been
replaced by 'new'
[Link]
String and While
Example: digit number
Write a program that gets a s = input('Enter a string: ')
string from the user and
prints ‘digit’ if the string
contains numbers only and
prints ‘not a digit’ otherwise.
[Run]
Enter a string: 123
digit
Enter a string: 12a
String and While
s = input('Enter a string: ')
Example: digit number digit = True
i=0
Write a program that gets a while i < len(s):
string from the user and if s[i] not in '1234567890':
prints ‘digit’ if the string digit = False
break
contains numbers only and i=i+1
prints ‘not a digit’ otherwise.
if digit :
print('digit')
else:
print('not a digit')
[Run]
Enter a string: 123
digit
Enter a string: 12a
String
Split
[Link]('delimiter') returns a s = 'Le Bao, Ai Bao, Fu Bao, Hui Bao, Rui Bao’
list of substrings separated bao = [Link](',')
by the delimiter print(bao)
[Link]() splits on all
whitespace characters
[Run]
['Le Bao', ' Ai Bao', ' Fu Bao', ' Hui Bao', ' Rui Bao']
String
Join
[Link](list) joins the elements bao = ['Le Bao', 'Ai Bao', 'Fu Bao', 'Hui Bao', 'Rui Bao']
in the given list together x = ','.join(bao)
using the string as the print(x)
delimiter
[Run]
Le Bao,Ai Bao,Fu Bao,Hui Bao,Rui Bao
Summary
Mutable and Immutable Data types
Copy of List
Tuple
String methods