Beruflich Dokumente
Kultur Dokumente
Sequential
data
types:
Springs,
Lists
and
Tuples
• the
items
or
elements
of
strings,
lists
and
tuples
are
ordered
in
a
defined
sequence
Difference:
Strings
and
tuple
are
immutable
Lists
are
mutable
Common
operations
for
sequence
data
types:
strings,
lists
and
tuples
seq and
seq1
can
be
a
list,
a
string
or
a
tuple
• Accessing elements in sequential data types: strings, lists and tuples
You
can
access
an
individual
item
on
a
sequential
data
type
by
its
index.
Indices
begin
with
0
[] slice seq[index]
+ concatenation
seq+seq1
* repetition
seq*n
Type
Conversion:
Strings
and
Numbers
str(3)
int('-3')
int('three')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'three’
Type
conversion:
from
string
to
list
s="abcdeaaa"
l=list(s2)
Examples
s2="a:b:c:d:e:a:a:a"
s2.split(":")
s1="a b c d e a aa"
s1.split()
Practice
split/list
s2='abacadabacadabacad'
['a', 'b', 'a', 'c', 'a', 'd', 'a', 'b', 'a', 'c', 'a', 'd', 'a', 'b',
'a', 'c', 'a', 'd']
Join
method
works
with
a
list
of
strings
or
a
tuple
of
strings.
my_list = ['one','two','three','four']
str1 = ''.join(my_list)
str1 = 'WOW'.join(list1)
1) Use the input function and ask the user to enter 3 numbers, separated by a comma.
3) Print the first number from that list and check the type of that first element
l = [1,2,3]
t = tuple(l)
t
l1 = list(t)
l1
Join
method
works
with
a
list
of
strings
or
a
tuple
of
strings.
t=('A','B','B','D')
'*'.join(t)
More
common
operations
for
sequence
data
types:
strings,
lists
and
tuples
Operation Result
L=[1,5,6,3,9,10,33,56,78]
• Calculate the average value of items in tuple T (use len and sum function)
Use
online
documentation
to
find
out
Pyhton3
build-‐in
functions
such
as
sum
• https://docs.python.org/3/library/functions.html