>>> index = 0 >>> text = 'Hello, C88C!' >>> text 'Hello, C88C!' >>> text[0] 'H' >>> text[1] 'e' >>> text[7] 'C' >>> while index < len(text): ... print('Letter is: ' + text[index]) File "", line 2 print('Letter is: ' + text[index]) ^ IndentationError: expected an indented block after 'while' statement on line 1 >>> while index < len(text): ... print('Letter is: ' + text[index]) ... index = index + 1 ... Letter is: H Letter is: e Letter is: l Letter is: l Letter is: o Letter is: , Letter is: Letter is: C Letter is: 8 Letter is: 8 Letter is: C Letter is: ! >>> index 12 >>> len(text) 12 >>> >>> >>> for letter in text: ... print('Letter is: ' + letter) ... Letter is: H Letter is: e Letter is: l Letter is: l Letter is: o Letter is: , Letter is: Letter is: C Letter is: 8 Letter is: 8 Letter is: C Letter is: ! >>> text 'Hello, C88C!' >>> text[0] 'H' >>> for thing in text: ... print(thing) ... H e l l o , C 8 8 C ! >>> for number in range(10): ... print(number) ... 0 1 2 3 4 5 6 7 8 9 >>> num = 0 >>> while num <= 9 File "", line 1 while num <= 9 ^ SyntaxError: expected ':' >>> while num <= 9: ... print(num) ... num += 1 ... 0 1 2 3 4 5 6 7 8 9 >>> for number in range(1, 11): ... print(number) ... 1 2 3 4 5 6 7 8 9 10 >>> range(10) range(0, 10) >>> range(100000000000000000000) range(0, 100000000000000000000) >>> range(10) range(0, 10) >>> list( range(10) ) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> nums = list( range(10) ) >>> nums [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> len(nums) 10 >>> text 'Hello, C88C!' >>> len(text) 12 >>> text[11] '!' >>> list( range(1, 11) ) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list( range(1, 11, 2) ) [1, 3, 5, 7, 9] >>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return an object that produces a sequence of integers from start (inclusive) | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. | These are exactly the valid indices for a list of 4 elements. | When step is given, it specifies the increment (or decrement). | | Methods defined here: | | __bool__(self, /) | True if self else False | | __contains__(self, key, /) | Return key in self. >>> >>> >>> courses = ['CS88', 'DATA8', 'POLSCI2', 'PHILR1B'] >>> courses ['CS88', 'DATA8', 'POLSCI2', 'PHILR1B'] >>> courses[0] 'CS88' >>> courses[4] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range >>> courses[3] 'PHILR1B' >>> for course in courses: ... print('I am taking ' + course) ... I am taking CS88 I am taking DATA8 I am taking POLSCI2 I am taking PHILR1B >>> courses ['CS88', 'DATA8', 'POLSCI2', 'PHILR1B'] >>> courses + ['MATH1A'] ['CS88', 'DATA8', 'POLSCI2', 'PHILR1B', 'MATH1A'] >>> courses + 'MATH1A' Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "str") to list >>> courses - ['MATH1A'] Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for -: 'list' and 'list' >>> >>> >>> >>> text = 'I am taking C88C' >>> text 'I am taking C88C' >>> text.split(' ') ['I', 'am', 'taking', 'C88C'] >>> text.split() ['I', 'am', 'taking', 'C88C'] >>> text.split('') Traceback (most recent call last): File "", line 1, in ValueError: empty separator >>> text.split('88') ['I am taking C', 'C'] >>> words = text.split(' ') >>> words ['I', 'am', 'taking', 'C88C'] >>> text 'I am taking C88C' >>> for word in words: ... print(word[0]) ... I a t C >>> courses ['CS88', 'DATA8', 'POLSCI2', 'PHILR1B'] >>> courses.pop() 'PHILR1B' >>> course 'PHILR1B' >>> courses.pop(0) 'CS88' >>> help(courses.insert) Help on built-in function insert: insert(index, object, /) method of builtins.list instance Insert object before index. >>> courses.insert(0, 'C88C') >>> courses ['C88C', 'DATA8', 'POLSCI2'] >>>