SEQUENCE TYPES: lists, ranges and strings
LISTS
1. A list can be defined with square brackets. The list can be empty, or you can initialize the list with any number of items separated by commas
new_list_object = [element_1, element_2, element_3]
empty_list = []
2. the append method adds a new item to the end of the list
empty_list.append(new_item)
3. the extend method adds multiple elements to the end of the list
empty_list.extend([list_of_items])
A list of characters: ['a', 'b', 'c', 'd', 'e'] A list of characters: ['a', 'b', 'c', 'd', 'e'] |
'a' 'a' |
['b', 'c', 'd'] ['b', 'c', 'd'] |
'c' 'c' |
['c', 'd', 'e'] ['c', 'd', 'e'] |
An empty list: [] After appending items: ['f', 'g'] An empty list: [] After appending items: ['f', 'g'] |
Extend adds each item to the list, while append adds the entire list as a single item!!!
List 3 after extending: ['a', 'b', 'c', 'd', 'e'] List 3 after extending: ['a', 'b', 'c', 'd', 'e'] |
List 3 after appending a list:['a', 'b', 'c', 'd', 'e', ['f', 'g']] List 3 after appending a list:['a', 'b', 'c', 'd', 'e', ['f', 'g']] |
RANGE - returns a list of integers that starts at zero and increments by one until it reaches (but does not include) the specified value.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
[5, 6, 7, 8, 9] [5, 6, 7, 8, 9] |
[3, 5, 7, 9] [3, 5, 7, 9] |
A list of integers: [1, 3, 5, 7, 9] A list of integers: [1, 3, 5, 7, 9] |
Another shortcut for creating a list of integers that is similar to the range function:
[start_value..endpoint, step=1]
Another list of integers: [1, 3, 5, 7, 9, 11] Another list of integers: [1, 3, 5, 7, 9, 11] |
3 [1, 3, 5, 7] 11 [5, 7, 9] [5, 7, 9, 11] 3 [1, 3, 5, 7] 11 [5, 7, 9] [5, 7, 9, 11] |
[3.73240000000000, 3, 5, 7, 9, 11] [3.73240000000000, 3, 5, 7, 9, 11] |
[0, 2, 4, 7, 9, 11] [0, 2, 4, 7, 9, 11] |
[0, 2, 4, 6] [0, 2, 4, 6] |
STRINGS - they have all the features of sequence types
John Smith 10 John Smith JOHN SMITH John Smith 10 John Smith JOHN SMITH |
FOR loop - A Python for loop iterates over the items in a list.
for loop_variable in list_name:
statement 1
statement 2
Let's say you have some data stored in a list, and you want to print the data in a particular format. We will use three variations of the for loop to display the data.
|
|
Iterating over a single list: 0.000000000000000 V -0.101340000000000 V -0.270000000000000 V -0.390000000000000 V Iterating over a single list: 0.000000000000000 V -0.101340000000000 V -0.270000000000000 V -0.390000000000000 V |
zip accepts one or more sequence types (with the same number of elements) as arguments and returns a list of tuples, where each tuple is composed of the one element from each sequence.
The syntax time,value was used to unpack each tuple, so that we could access the values through the variables time and value.
Iterating over multiple lists: 0.000000000000000 sec 0.000000000000000 V 1.50000000000000 sec -0.101340000000000 V 2.60000000000000 sec -0.270000000000000 V 3.10000000000000 sec -0.390000000000000 V Iterating over multiple lists: 0.000000000000000 sec 0.000000000000000 V 1.50000000000000 sec -0.101340000000000 V 2.60000000000000 sec -0.270000000000000 V 3.10000000000000 sec -0.390000000000000 V |
for loop_counter in range(len(list_name)):
statement 1
statement 2
Iterating with an index variable: 0.000000000000000 sec 0.000000000000000 V 1.50000000000000 sec -0.101340000000000 V 2.60000000000000 sec -0.270000000000000 V 3.10000000000000 sec -0.390000000000000 V Iterating with an index variable: 0.000000000000000 sec 0.000000000000000 V 1.50000000000000 sec -0.101340000000000 V 2.60000000000000 sec -0.270000000000000 V 3.10000000000000 sec -0.390000000000000 V |
Two simple examples of for loops:
45 45 |
How many lines will be printed when the following loop runs?
line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed line printed |
['a', 'b', 'c'] ['a', 'b', 'c'] |
We defined a list of strings, each of which contained whitespace before and/or after the character.
The string method strip was used to remove the whitespace from each string, and we used the list method append to create a new list of stripped strings.
LIST COMPREHENSION - general sintax
new_list = [ operation_on_value for value in existing_list ]
['a', 'b', 'c'] ['a', 'b', 'c'] |
If you are creating a new list, then the existing list can be generated by a function like range or srange.
[0.000, 4.00, 16.0, 36.0, 64.0] [0.000, 4.00, 16.0, 36.0, 64.0] |
WHILE loop - is used when we don't know how many iterations will be required and in that case we can't use FOR loop!
WHILE conditional_expresion:
statement 1
statement 2
The loop iterates as long as the conditional expression evaluates to the value True.
The loop terminates as soon as the expression evaluates to False.
6464 3232 1616 808 404 202 101 6464 3232 1616 808 404 202 101 |
IF statement - allow a program to make decisions while it is running.
if conditional_expression:
statements
else:
statements
|
|
3 3 3607 3803 3 3 3607 3803 |
2 2 2 2 2 2 |
|
|
[3, 3, 3607, 3803] [3, 3, 3607, 3803] |
123456789 123456789 |
ELIF instead of SWITCH
numerical numerical |
|
|