Lists and loops - About - eilslabs · PDF fileConditions if : elif : else: 5/29

29
Lists and loops Lecture 3 Johannes Werner November 2016

Transcript of Lists and loops - About - eilslabs · PDF fileConditions if : elif : else: 5/29

Page 1: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Lists and loopsLecture 3

Johannes Werner

November 2016

Page 2: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Table of contents

Summary of last lecture

Lists

Loops

Take home messages

·

·

·

·

2/29

Page 3: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Summary of last lecture

Page 4: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Datatypes

dynamic type-checking

types

datatype functions

conversion of datatypes

·

·

numbers: integer, float, complex

strings

bool

-

-

-

·

depending on the datatype, e.g. +-

numbers: addition

strings: concatenation

-

-

·

4/29

Page 5: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Conditions

if <condition1>: <code block1> elif <condition2>: <code block2> else: <code block3>

5/29

Page 6: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Lists

Page 7: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

What are lists?

ordered item of values

>>> [1, 2, 3, 4, 5] >>> ['a', 'b', 'c', 'd'] >>> [item1, item2, item3] >>> [] >>> [100, 200, 'hello world', [5, 3] ]

7/29

Page 8: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Basic list operations

Length: len

Concatenation: +

Repetition: *

Membership: in

Iteration: for

·

·

·

·

·

8/29

Page 9: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

List methods

adding items

>>> list1 = [] >>> list1.append('hello') >>> print(list1) ['hello'] >>> list1.append('world') >>> print(list1) ['hello', 'world']

9/29

Page 10: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

List methods

adding items

>>> list2 = [] >>> list2.append(100) >>> list2.append(200) >>> list2.append('hello world') >>> list2 [100, 200, 'hello world'] >>> list2.append([5, 3]) >>> list2.extend([6, 4]) >>> list2 [100, 200, 'hello world', [5, 3], 6, 4]

10/29

Page 11: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

List methods

removing items

>>> list3 = [1, 2, 3, 4, 5, 6] >>> del list3[3] >>> print(list3) [1, 2, 3, 5, 6] >>> list3.pop() 6 >>> print(list3) [1, 2, 3, 5]

11/29

Page 12: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

List methods

assigning values and length of lists

>>> list4 = ['a', 'b', 'c', 'x', 'e'] >>> list4[3] = 'd' >>> print(list4) ['a', 'b', 'c', 'd', 'e'] >>> len(list4) 5

12/29

Page 13: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

List methods

Further methodsinsert

remove

clear

index

count

sort

reverse

copy

·

·

·

·

·

·

·

·

13/29

Page 14: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Slicing

"Hello World"

14/29

Page 15: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Slicing

>>> list5 = [1, 2, 3, 4, 5, 6, 7] >>> list5[1:4] [2, 3, 4] >>> list5[5:] [6, 7] >>> list5[:-4] [1, 2, 3] >>> list5[0:6:2] [1, 3, 5] >>> list5[::2] [1, 3, 5, 7]

15/29

Page 16: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

List comprehensions

>>> squares = [] >>> for x in range(10): ... squares.append(x**2) >>> squares = [x**2 for x in range(10)]

>>> [x for x in [1, 2, 3, 4, 5] if x > 3] [4, 5] >>> [bin(x) for x in [1, 2, 3, 4, 5]] ['0b1', '0b10', '0b11', '0b100', '0b101']

16/29

Page 17: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Nested lists

>>> list6 = [1, 2, 3, [4, 5, 6]] >>> list6[2] 3 >>> list6[3] [4, 5, 6] >>> list6[3][1] 5 >>> array = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

17/29

Page 18: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Tuples

immutable sequence of Python objects

>>> t1 = (1, 2, 3) >>> del t1[2] TypeError: 'tuple' object does not support item deletion >>> t2 = () >>> t2[0] = 'hello' TypeError: 'tuple' object does not support item assignment

18/29

Page 19: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Loops

Page 20: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Functionality

20/29

Page 21: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

for loop

Iterating over numbers

>>> for item in range(1, 4): ... print(item) 1 2 3

21/29

Page 22: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

for loop

Iterating over lists

>>> for item in [1, 2, 3]: ... print(item) 1 2 3

>>> seq = 'ATGTAGCGTAC' >>> for letter in seq: ... print('Base: ', letter) Base: A Base: T Base: G ...

22/29

Page 23: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

for loop

Iterating over two or more lists

>>> first_name = ['Sheldon', 'Leonard'] >>> last_name = ['Cooper', 'Hofstadter'] >>> for fn, ln in zip(first_name, last_name): ... print(fn, ln) Sheldon Cooper Leonard Hofstadter

23/29

Page 24: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

while loop

>>> while x < 4: ... print(x) ... x += 1 1 2 3

>>> sequence = 'ACTGTACGTACGTACGTATGCATGCA' >>> i = 0 >>> while i < len(sequence): ... print(sequence[i:i+3]) ... i += 3 ACT GTA CGT ...

24/29

Page 25: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Nested loops

>>> A = C = G = T = 0 >>> seqs = ['ACTGGATCGTAC', 'TGACTGACGA', 'GTCAGAGCA'] >>> for seq in seqs: ... for letter in seq: ... if letter == 'A': ... A += 1 ... elif letter == 'C': ... C += 1 ... elif letter == 'G': ... G += 1 ... elif letter == 'T': ... T += 1 ... else: ... print('Wrong letter, ', letter) ... break >>> print('A: ', A) A: 9

25/29

Page 26: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

for or while loops

list = [1, 2, 3, 4] for i in list: print(i)

list = [1, 2, 3, 4] index = 0 while index < len(list): print(list[index]) index += 1

26/29

Page 27: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

infinite loops

>>> x = True >>> while x is True: ... print(x, 'is True')

27/29

Page 28: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Take home messages

Page 29: Lists and loops - About - eilslabs · PDF fileConditions if :  elif :  else:  5/29

Take home messages

lists

loops

·

creating lists

adding and removing items

assigning values

slicing

tuples

nested lists

-

-

-

-

-

-

·

for loop

while loop

interrupting loops

-

-

-

29/29