Sie sind auf Seite 1von 11

Summary

 
Sequential  data  types:  Springs,  Lists  and  Tuples

Strings,  lists,  tuples,  have  some  underlying  concepts  in  common:  

• the  items  or  elements  of  strings,  lists  and  tuples  are  ordered  in  a  defined  
sequence

• the  elements  can  be  accessed  via  indices

• special  operations:  concatenation  and  repetition

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]

[:] range  slice  


seq[start:end] # items start through end-1
seq[start:] # items start through the rest of the array
seq[:end] # items from the beginning through end-1
seq[:] # a copy of the whole list

[::]  Extended  slice  


seq[start:end:step] # start through not past end, by step
seq[-1] # last item in the array
seq[-2:] # last two items in the array
seq[:-2] # everything except the last two items
Common  operations  for  sequence  data  types:  strings,  lists  and  tuples
seq and  seq1  can  be  a  list,  a  string  or  a  tuple

• Special  operation  of  sequential  data  types

+ 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

list() function # it will convert a string in a list where each


character (including spaces) of a string becomes an item of the
list

s="abcdeaaa"
l=list(s2)

str.split(sep) ## very important string method! It will


split a string in words based on a separator (sep) and return a
list of these words

str.split() # if sep is not specified, whitespace strings (one


or more than one) is the separator.

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

Make  this  string

s2='abacadabacadabacad'

Now  use  type  conversion  method  or  function  to  produce

['aba', 'adaba', 'adaba', 'ad']

['a', 'b', 'a', 'c', 'a', 'd', 'a', 'b', 'a', 'c', 'a', 'd', 'a', 'b',
'a', 'c', 'a', 'd']

Make  this  string  


s3='123,456,230,450'

use  the  split  method  to  produce  this  list

['123', '456', '230', '450']


Type  Conversion:  List    to  String
'sep'.join(list) -> string
#return a string which is the concatenation of the items in the
list. sep will be the separator between items

Join  method  works  with  a  list  of  strings  or  a  tuple  of  strings.
my_list = ['one','two','three','four']

str1 = ''.join(my_list)

str1 = ' '.join(my_list)

str1 = 'WOW'.join(list1)

L=[1,2,3,4,5] # list of numbers


str2 = ' '.join(L)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
Practice
Make  a  script  called  ex.py and  in  it  do  the  following:

1)  Use  the  input  function  and  ask  the  user  to  enter  3  numbers,  separated  by  a  comma.

2)  Make  a  list  with  these  numbers

3)  Print  the  first  number  from  that  list  and  check  the  type  of  that  first  element

4)  Now  add  the  three  numbers


Type  Conversion:  Tuples  and  Lists

l = [1,2,3]
t = tuple(l)
t

l1 = list(t)
l1

Type  Conversion:  Tuples  and  strings


S.join(items) -> string #return a string which is the
concatenation of the items. S will be the separator between
items

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

seq and  seq1  can  be  a  list,  a  string  or  a  tuple

Operation Result

len(seq) length of seq


min(seq) smallest item of seq - works for homogeneous items
max(seq) largest item of seq - works for homogeneous items
s.count(x) returns total number of occurrences of x in seq

s.index(x) returns index of x in seq


Practice
Define  this  list  of  numbers

L=[1,5,6,3,9,10,33,56,78]

• Calculate  the  maximum  value  of  list  L

• Convert  list  to  tuple  and  store  it  in  variable  T

• 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

Das könnte Ihnen auch gefallen