October 10, 2017
Week 3’s lab was intense! We started with list methods, where we left off last week and went through the following:
list.append(x): Add an item to the end of the list.
list.extend(L): Extend the list by appending all the items in the given list.
list.insert(i, x): Insert an item at a given position.
list.remove(x): Remove the first item from the list whose value is x (it will come up as an error if there is no such item).
list.pop([i]): Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
list.index(x): Return the index in the list of the first item whose value is x (it will come up as an error if there is no such item).
list.count(x): Return the number of times x appears in the list.
list.sort(cmp=None, key=None, reverse=False): Sort the items of the list in place.
list.reverse(): Reverse the elements of the list, in place.
Here is an example that uses most of the list methods
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count(‘x’))
2 1 0
>>> a.insert(2, –1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>> a.pop()
1234.5
>>> a
[-1, 1, 66.25, 333, 333]
We then introduced the Python Dictionary:
Python Dictionary
In the Python dictionary, each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
An empty dictionary without any items is written with just two curly braces, like this: {}. Keys within a dictionary must be a data type such as strings, numbers, or tuples.
Example:
dict = {‘Name’: ‘Michael’, ‘Age’: 7, ‘Class’: ‘First’}
print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]
When we run this code, it produces the following result:
dict[‘Name’]: Michael
dict[‘Age’]: 7
And finally, we very briefly touched upon string methods and boolean methods – by no means did we cover all the material we intended (as you see below) – but will pick up in Week 4 with string methods first!
String Methods
The string data type has multiple methods. Here are all of the methods of list objects:
str.upper() – Making Strings uppercase
str.lower() – Making Strings lowercase
str.join() method will concatenate two strings, but in a way that passes one string through another.
str.split() method returns a list of strings that are separated by whitespace if no other parameter is given.
str.replace() method can take an original string and return an updated string with some replacement.
Boolean methods:
Method | True if |
str.isalnum() | If string consists of only alpha-numeric values |
str.isalpha() | If string consists of only alphabets |
str.islower() | If string consists of only lower-case values |
str.isnumeric() | String consists of only numeric characters |
str.isspace() | String consists of only whitespace characters |
str.istitle() | String is in title case |
str.isupper() | String’s alphabetic characters are all upper case |
Example:
>>> string = “Hello”
>>> string.upper()
‘HELLO’
>>> string.lower()
‘hello’
>>> string = “Hello,world”
>>> string.split(“,”)
[‘Hello’, ‘world’]
>>> string[::-1]
‘dlrow,olleH’
>>> len(string)
11
>>> string1 = “Hello”
>>> string2 = “World”
>>> string1+string2
‘HelloWorld’
Hope this material is helpful and inspiring. Please don’t hesitate to contact us for any clarifications. And as always, if you have any requests for material to cover in our labs, please email or contact us!