====== Computer Lab 04: strings, tuples, lists ======
In Lab 04 we will focuse on **compound data types** which are data types which consist of smaller pieces:
* **strings**: consists of single characters (strings with length 1)
* **tuples**: consists of elements which may have different types (integers, strings, floats, tuples etc.)
* **lists**: typically consists of elements of the same type (all elements are for example integer numbers)
We will practice:
* indexing, slicing
* traversals (for loops, while loops)
* useful methods
* how to work with mutable (lists) and immutable (tuples, strings) data types
==== strings ====
* immutable
s = "Hello World"
print("indexing:")
print(s[0])
print(s[-1])
print(s[0:5])
print(s[::2])
print("\nstring method examples:")
print(s.lower())
print(s.isdecimal())
print(s)
print("\nmodifying?:")
s = s[:5] + "_" + s[6:]
print(s)
==== tuples ====
* immutable
* heterogeneous data structure
# packing
b = ("Bob", 19, "CS")
#unpacking
(name, age, studies) = b
name
age
studies
==== lists ====
* mutable
* typically homogenous data structure
def pow2(lst):
for i in range(len(lst)):
lst[i] = lst[i]**2
return lst
a = [2,5,6]
b = pow2(a)
print(a)
print(b)
# Wentworth (2012)
students = [
("John", ["CompSci", "Physics"]),
("Vusi", ["Maths", "CompSci", "Stats"]),
("Jess", ["CompSci", "Accounting", "Economics", "Management"]),
("Sarah", ["InfSys", "Accounting", "Economics", "CommLaw"]),
("Zuki", ["Sociology", "Economics", "Law", "Stats", "Music"])]
# Count how many students are taking CompSci
counter = 0
for (name, subjects) in students:
if "CompSci" in subjects:
counter += 1
print("The number of students taking CompSci is", counter)
===== Practical work =====
Implement the following functions and test them appropriately.
def count_chars(string, char):
"""Return the number of occurances of char in string.
:param string: a string. Example "hello world"
:param char: character. Example "l"
:return: int number of char occurences in string
>>> count_chars("hello","l")
2
"""
def replace_chars(string, char_original, char_replacement):
"""Return a new string with all occurences of char_original replaced by char_replacement.
:param string: string in which some chars shall be replaced
:param char_original: character to be replaced
:param char_replacement: character to be inserted instead of char_original
:return: a new string with replaced characters
>>> replace_chars("hello","h","H")
'Hello'
"""
def reverse_string(string):
"""Return a reversed version of string.
:param string: string to be reversed
:return: reversed string
>>> reverse_string("hello")
'olleh'
"""
def is_palindrom(string):
"""Return True if and only if the string is equal to its reversed version.
:param string: string to check for palindrom
:return: boolean
>>> is_palindrom("aka")
True
>>> is_palindrom("hello")
False
"""
def circle_chars(radius):
"""Return a circumference and an area of a circle.
:param radius: int radius of a circle.
:return: (int, int) tuple containing circumference and area (in this order)
>>> circle_chars(3)
(18.84955592153876, 28.274333882308138)
"""
def remove_odd(lst):
"""
Return a copy of the input list with all odd numbers removed.
:param lst: The input list containing integer numbers
:return: A new list containing only the even numbers from the original list
>>> remove_odd([8, 6, 3, 2, 1, 9, 6, 2])
[8, 6, 2, 6, 2]
"""
===== Weekly Homework 04 =====
Required filename: ''04_weekly_hw.py''.
def remove_duplicates(items):
"""Remove duplicate items.
:param items: input list, can contain anything
:return: (unique_list, duplicates)
unique_list - a list with unique items only
duplicates - a list with items that were in items more than once (not unique list)
>>> remove_duplicates(['h','e','l','l','o'])
(['h', 'e', 'l', 'o'], ['l'])
>>> remove_duplicates([3,2,2,2,1])
([3, 2, 1], [2, 2])
"""
===== Graded homework =====
Solve homework [[courses:be5b33prg:homework:regular:02_quadratic_equation|02 - Quadratic equation solver]] and submit it via Upload system in time! Check the deadline in Upload system.
* The homework from last week was to read chapters 6,7,8,9 from [[http://openbookproject.net/thinkcs/python/english3e/|Wentworth2012]]. Go through them once more and concentrate on things you do not understand.
* Read chapter 11 (Lists) from [[http://openbookproject.net/thinkcs/python/english3e/|Wentworth2012]].