0% found this document useful (0 votes)
11 views

Basic Strings List Tuples

Uploaded by

shashwat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Basic Strings List Tuples

Uploaded by

shashwat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

DSN3002: Python for DS

Basics of Python
Python Identifiers:
• A Python identifier is a name used to identify a variable, function, class, module, or
other object. An identifier starts with a letter A to Z or a to z or an underscore (_)
followed by zero or more letters, underscores, and digits (0 to 9).

• Python does not allow punctuation characters such as @, $, and % within


identifiers. Python is a case sensitive programming language. Thus Manpower and
manpower are two different identifiers in Python.
Python Identifiers (cont’d)
• Here are following identifier naming convention for Python:
• Class names start with an uppercase letter and all other identifiers with a lowercase
letter.
• Starting an identifier with a single leading underscore indicates by convention that the
identifier is meant to be private.
• Starting an identifier with two leading underscores indicates a strongly private
identifier.
• If the identifier also ends with two trailing underscores, the identifier is a language-
defined special name.
Reserved Words:
Keywords contain lowercase letters only.

and exec not


assert finally or
break for pass
class Reserved Words: from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
Lines and Indentation:
• One of the first caveats programmers encounter when learning Python is the fact that
there are no braces to indicate blocks of code for class and function definitions or flow
control. Blocks of code are denoted by line indentation, which is rigidly enforced.
• The number of spaces in the indentation is variable, but all statements within the block
must be indented the same amount. Both blocks in this example are fine:
if True:
print "Answer“;
print "True" ;
else:
print "Answer“;
print "False"
Multi-Line Statements:
• Statements in Python typically end with a new line. Python does, however, allow the use
of the line continuation character (\) to denote that the line should continue. For
example:
total = item_one + \
item_two + \
item_three
• Statements contained within the [], {}, or () brackets do not need to use the line
continuation character. For example:
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
Quotation in Python:

• Python accepts single ('), double (") and triple (''' or """) quotes to
denote string literals, as long as the same type of quote starts and
ends the string.
• The triple quotes can be used to span the string across multiple
lines. For example, all the following are legal:
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made
up of multiple lines and sentences."""
Comments in Python:
A hash sign (#) that is not inside a string literal begins a comment. All characters after the #
and up to the physical line end are part of the comment, and the Python interpreter
ignores them.
Using Blank Lines:

• A line containing only whitespace, possibly with a comment, is known as a blank line,
and Python totally ignores it.
• In an interactive interpreter session, you must enter an empty physical line to terminate
a multiline statement.
Variables
• Are not declared, just assigned
• The variable is created the first time you assign it a value
• Are references to objects
• Type information is with the object, not the reference
• Everything in Python is an object
• Everything means everything, including functions and classes (more on
this later!)

• Data type is a property of the object and not of the variable


Assignment

assignment statement: Stores a value into a variable.


• Syntax:
name = value

• Examples: x = 5
gpa = 3.14

x 5 gpa 3.14
• A variable that has been given a value can be used in expressions.
x + 4 is 9
Comments
• Start comments with # – the rest of line is ignored.

• Can include a “documentation string” as the first line of any new function or class
that you define.

• The development environment, debugger, and other tools use it: it’s good style to
include one.
def my_function(x, y):
“““This is the docstring. This
function does blah blah blah.”””
# The code would go here...
Type and Help
type() returns the type of variable passed
>>> type(2)
<class 'int'>

help() function returns helps related to argument passed


Printing
Python command print – Displays data, values, and expressions

The single and double quotation mark string objects, which are collections of
texts surrounded by quotes.
Printing: Examples
>>> print ("Hello, World! ")
Hello, World!

>>> "hello"
'hello'

>>> "world"
'world'

>>> "hello"+"world"
'helloworld'
Some integer operations

Operation Result
x+y sum of x and y
x-y difference of x and y
x*y product of x and y
x/y
x // y Some integer operations
quotient of x and y
floored quotient of x and y
x%y remainder of x / y
-x x negated
+x x unchanged
abs(x) absolute value or magnitude of x
int(x) x converted to integer
float(x) x converted to floating point
a complex number with real part re, imaginary
complex(re, im)
part im. im defaults to zero.
pow(x, y) x to the power y
x ** y x to the power y
Strings
Strings are immutable – you cannot update the value of an existing string
object.
Operation Result

str.capitalize() stringwith its first character capitalized and the rest


lowercased
str.count(sub[, st number of non-overlapping occurrences of substring sub in
Some String Operations
art[, end]])
str.find(sub[,
the range [start, end]
start lowest index in the string where substring sub is found within
[, end]]) the slice s[start:end], else -1
str.isdigit() true if all characters in the string are digits and there is at
least one character, false otherwise
str.lower() Return lowercased string

str.upper Return uppercased string

Str.title() titlecased version of the string where words start with an


uppercase character and the remaining characters are
lowercase.
>>> str1 = "Hello"
String Data Type >>> str2 = 'there'
>>> bob = str1 + str2
>>> print(bob)
• A string is a sequence of characters Hellothere
>>> str3 = '123'
• A string literal uses quotes >>> str3 = str3 + 1
'Hello' or "Hello" Traceback (most recent call
last): File "<stdin>", line 1,
• For strings, + means “concatenate” in <module>
TypeError: cannot concatenate
• When a string contains numbers, it is 'str' and 'int' objects
still a string >>> x = int(str3) + 1
>>> print(x)
124
• We can convert numbers in a string
>>>
into a number using int()
Reading and >>> name = input('Enter:')
Enter:Chuck
Converting >>> print(name)
Chuck
>>> apple = input('Enter:')
• We prefer to read data in using
Enter:100
strings and then parse and
>>> x = apple – 10
convert the data as we need
Traceback (most recent call
last): File "<stdin>", line 1,
• This gives us more control over
in <module>
error situations and/or bad user
TypeError: unsupported operand
input
type(s) for -: 'str' and 'int'
>>> x = int(apple) – 10
• Input numbers must be
>>> print(x)
converted from strings
90
Looking Inside Strings
• We can get at any single character in a b a n a n a
string using an index specified in 0 1 2 3 4 5
square brackets
>>> fruit = 'banana'
• The index value must be an integer
>>>
>>>
letter = fruit[1]
print(letter)
and starts at zero a
>>> x = 3
• The index value can be an expression >>> w = fruit[x - 1]
that is computed >>> print(w)
n
A Character Too Far
• You will get a python error >>> zot = 'abc'
>>> print(zot[5])
if you attempt to index Traceback (most recent call
beyond the end of a string last): File "<stdin>", line
1, in <module>
• So be careful when IndexError: string index out
constructing index values of range
and slices >>>
Strings Have Length

b a n a n a
The built-in function len gives 0 1 2 3 4 5
us the length of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.

'banana' len() 6
(a string) function (a number)
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
def len(inp):
blah
'banana' blah 6
for x in y:
(a string) blah
(a number)
blah
Looping Through Strings

Using a while statement, fruit = 'banana' 0b


an iteration variable, and index = 0 1a
the len function, we can while index < len(fruit): 2n
letter = fruit[index] 3a
construct a loop to look at
print(index, letter)
each of the letters in a 4n
index = index + 1
string individually 5a
Looping Through Strings

• A definite loop using a b


for statement is much a
fruit = 'banana'
more elegant n
for letter in fruit:
• The iteration variable is print(letter) a
n
completely taken care of a
by the for loop
Looping Through Strings

• A definite loop using a fruit = 'banana'


for letter in fruit :
b
for statement is much a
print(letter)
more elegant n
• The iteration variable is index = 0
a
n
completely taken care of while index < len(fruit) :
letter = fruit[index] a
by the for loop
print(letter)
index = index + 1
Looping and Counting
word = 'banana'
This is a simple loop that count = 0
loops through each letter in a for letter in word :
string and counts the number if letter == 'a' :
of times the loop encounters count = count + 1
the 'a' character print(count)
Looking Deeper into in
• The iteration variable “iterates”
through the sequence Iteration Six-character
(ordered set) variable string
• The block (body) of code is
executed once for each value for letter in 'banana' :
in the sequence print(letter)
• The iteration variable moves
through all of the values in the
sequence
Yes No b a n a n a
Done? Advance letter

print(letter)

for letter in 'banana' :


print(letter)

The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequence
More String Operations
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
continuous section of a string
using a colon operator >>> s = 'Monty Python'
>>> print(s[0:4])
• The second number is one Mont
beyond the end of the slice - >>> print(s[6:7])
“up to but not including” P
>>> print(s[6:20])
• If the second number is
Python
beyond the end of the string,
it stops at the end
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11

>>> s = 'Monty Python'


If we leave off the first number >>> print(s[:2])
or the last number of the slice, Mo
it is assumed to be the >>> print(s[8:])
beginning or end of the string thon
respectively >>> print(s[:])
Monty Python
String Concatenation
>>> a = 'Hello'
>>> b = a + 'There'
When the + operator is >>> print(b)
applied to strings, it means HelloThere
“concatenation” >>> c = a + ' ' + 'There'
>>> print(c)
Hello There
>>>
Using in as a Logical Operator
>>> fruit = 'banana'
• The in keyword can also be >>> 'n' in fruit
used to check to see if one True
string is “in” another string >>> 'm' in fruit
False
• The in expression is a
>>> 'nan' in fruit
True
logical expression that >>> if 'a' in fruit :
returns True or False and ... print('Found it!')
can be used in an if ...
statement Found it!
>>>
String Comparison
if word == 'banana':
print('All right, bananas.')

if word < 'banana':


print('Your word,' + word + ', comes before banana.')
elif word > 'banana':
print('Your word,' + word + ', comes after banana.')
else:
print('All right, bananas.')
• Python has a number of string String Library
functions which are in the
string library
>>> greet = 'Hello Bob'
• These functions are already >>> zap = greet.lower()
built into every string - we >>> print(zap)
invoke them by appending the hello bob
function to the string variable >>> print(greet)
Hello Bob
• These functions do not modify >>> print('Hi There'.lower())
the original string, instead they hi there
return a new string that has >>>
been altered
>>> stuff = 'Hello world'
>>> type(stuff)
<class 'str'>
>>> dir(stuff)
['capitalize', 'casefold', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map',
'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']

https://docs.python.org/3/library/stdtypes.html#string-methods
String Library
str.capitalize() str.replace(old, new[, count])
str.center(width[, fillchar]) str.lower()
str.endswith(suffix[, start[, end]]) str.rstrip([chars])
str.find(sub[, start[, end]]) str.strip([chars])
str.lstrip([chars]) str.upper()
Searching a String
b a n a n a
• We use the find() function to search
for a substring within another string
0 1 2 3 4 5

• find() finds the first occurrence of the >>> fruit = 'banana'


substring >>> pos = fruit.find('na')
>>> print(pos)
• If the substring is not found, find() 2
returns -1 >>> aa = fruit.find('z')
>>> print(aa)
• Remember that string position starts -1
at zero
Making everything UPPER CASE
• You can make a copy of a >>> greet = 'Hello Bob'
string in lower case or upper >>> nnn = greet.upper()
case >>> print(nnn)
• Often when we are searching
HELLO BOB
>>> www = greet.lower()
for a string using find() we first
>>> print(www)
convert the string to lower case
hello bob
so we can search a string
>>>
regardless of case
Search and Replace
• The replace() function
is like a “search and >>> greet = 'Hello Bob'
replace” operation in a >>> nstr = greet.replace('Bob','Jane')
>>> print(nstr)
word processor Hello Jane

• It replaces all
>>> nstr = greet.replace('o','X')
>>> print(nstr)
occurrences of the HellX BXb
search string with the >>>
replacement string
Stripping Whitespace
• Sometimes we want to take
a string and remove
whitespace at the beginning >>> greet = ' Hello Bob '
>>> greet.lstrip()
and/or end
'Hello Bob '
• lstrip() and rstrip() remove
>>> greet.rstrip()
' Hello Bob'
whitespace at the left or right >>> greet.strip()
'Hello Bob'
• strip() removes both >>>
beginning and ending
whitespace
Prefixes
>>> line = 'Please have a nice day'
>>> line.startswith('Please')
True
>>> line.startswith('p')
False
Parsing and
21 31 Extracting
From [email protected] Sat Jan 5 09:14:16 2008

>>> data = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> atpos = data.find('@')
>>> print(atpos)
21
>>> sppos = data.find(' ',atpos)
>>> print(sppos)
31
>>> host = data[atpos+1 : sppos]
>>> print(host)
uct.ac.za
Two Kinds of Strings
Python 2.7.10 Python 3.5.1
>>> x = '이광춘' >>> x = '이광춘'
>>> type(x) >>> type(x)
<type 'str'> <class 'str'>
>>> x = u'이광춘' >>> x = u'이광춘'
>>> type(x) >>> type(x)
<type 'unicode'> <class 'str'>
>>> >>>

In Python 3, all strings are Unicode


Summary
• String type • String operations
• Read/Convert • String library
• Indexing strings [] • String comparisons
• Slicing strings [2:4] • Searching in strings
• Looping through strings • Replacing text
with for and while • Stripping white space
• Concatenating strings with +
Programming
Algorithm
- A set of rules or steps used to solve a problem

Data Structure
- A particular way of organizing data in a computer

https://en.wikipedia.org/wiki/Algorithm
https://en.wikipedia.org/wiki/Data_structure
What is Not a “Collection”?
Most of our variables have one value in them - when we put a new
value in the variable, the old value is overwritten

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of
Collection
• A collection allows us to put many values in a single “variable”

• A collection is nice because we can carry all many values


around in one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


List Constants
List constants are surrounded by >>> print([1, 24, 76])
square brackets and the elements [1, 24, 76]
>>> print(['red', 'yellow',
in the list are separated by 'blue'])
commas ['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
A list element can be any Python ['red', 24, 98.6]
>>> print([ 1, [5, 6], 7])
object - even another list [1, [5, 6], 7]
>>> print([])
A list can be empty []
We Already Use Lists!
5
for i in [5, 4, 3, 2, 1] :
print(i) 4
print('Blastoff!') 3
2
1
Blastoff!
Lists and Definite Loops - Best Pals

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends : Happy New Year: Joseph
print('Happy New Year:', friend)
print('Done!') Happy New Year: Glenn
Happy New Year: Sally
Done!
z = ['Joseph', 'Glenn', 'Sally']
for x in z:
print('Happy New Year:', x)
print('Done!')
Looking Inside Lists
Just like strings, we can get at any single element in a list using an
index specified in square brackets

>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]


Joseph Glenn Sally >>> print(friends[1])
Glenn
0 1 2 >>>
Lists are Mutable
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
Strings are “immutable” - we cannot Traceback
TypeError: 'str' object does not
change the contents of a string - support item assignment
we must make a new string to >>> x = fruit.lower()
make any change >>> print(x)
banana
Lists are “mutable” - we can change >>> lotto = [2, 14, 26, 41, 63]
>>> print(lotto)
an element of a list using the index [2, 14, 26, 41, 63]
operator >>> lotto[2] = 28
>>> print(lotto)
[2, 14, 28, 41, 63]
How Long is a List?

The len() function takes a list as a >>> greet = 'Hello Bob'


parameter and returns the number >>> print(len(greet))
of elements in the list 9
>>> x = [ 1, 2, 'joe', 99]
>>> print(len(x))
Actually len() tells us the number of 4
elements of any set or sequence >>>
(such as a string...)
Using the range Function

The range function returns a


>>> print(range(4))
list of numbers that range [0, 1, 2, 3]
from zero to one less than >>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print(len(friends))
the parameter 3
>>> print(range(len(friends)))
[0, 1, 2]
We can construct an index >>>
loop using for and an
integer iterator
A Tale of Two Loops...
>>> friends = ['Joseph', 'Glenn', 'Sally']
friends = ['Joseph', 'Glenn', 'Sally'] >>> print(len(friends))
3
for friend in friends : >>> print(range(len(friends)))
print('Happy New Year:', friend) [0, 1, 2]
>>>
for i in range(len(friends)) :
friend = friends[i]
print('Happy New Year:', friend) Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Concatenating Lists Using +
>>> a = [1, 2, 3]
We can create a new list >>> b = [4, 5, 6]
by adding two existing >>> c = a + b
>>> print(c)
lists together
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
Lists Can Be Sliced Using :
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41,12] Remember: Just like in
>>> t[:4] strings, the second
[9, 41, 12, 3] number is “up to but not
>>> t[3:]
including”
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods
>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']
>>>

http://docs.python.org/tutorial/datastructures.html
Building a List from Scratch >>> stuff = list()
We can create an empty list and >>> stuff.append('book')
then add elements using the >>> stuff.append(99)
append method >>> print(stuff)
['book', 99]
The list stays in order and new >>> stuff.append('cookie')
>>> print(stuff)
elements are added at the
['book', 99, 'cookie']
end of the list
Is Something in a List?

Python provides two operators >>> some = [1, 9, 21, 10, 16]
that let you check if an item is >>> 9 in some
True
in a list
>>> 15 in some
False
These are logical operators that >>> 20 not in some
return True or False True
>>>
They do not modify the list
Lists are in Order
• A list can hold many
items and keeps
those items in the
order until we do >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
>>> friends.sort()
something to change >>> print(friends)
the order ['Glenn', 'Joseph', 'Sally']
>>> print(friends[1])
• A list can be sorted Joseph
(i.e., change its order) >>>

• The sort method


(unlike in strings)
means “sort yourself”
Built-in Functions and Lists
>>> nums = [3, 41, 12, 9, 74, 15]
There are a number of >>> print(len(nums))
functions built into Python 6
that take lists as >>> print(max(nums))
parameters 74
>>> print(min(nums))
3
Remember the loops we >>> print(sum(nums))
built? These are much 154
simpler. >>> print(sum(nums)/len(nums))
25.6
total = 0 Enter a number: 3
count = 0
while True :
Enter a number: 9
inp = input('Enter a number: ') Enter a number: 5
if inp == 'done' : break
value = float(inp) Enter a number: done
total = total + value Average: 5.66666666667
count = count + 1

average = total / count


numlist = list()
print('Average:', average)
while True :
inp = input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)

average = sum(numlist) / len(numlist)


print('Average:', average)
Best Friends: Strings and Lists
>>> abc = 'With three words' >>> print(stuff)
>>> stuff = abc.split() ['With', 'three', 'words']
>>> print(stuff) >>> for w in stuff :
['With', 'three', 'words'] ... print(w)
>>> print(len(stuff)) ...
3 With
>>> print(stuff[0]) Three
With Words
>>>

Split breaks a string into parts and produces a list of strings. We think of these
as words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces'] ● When you do not specify a
>>>
>>> line = 'first;second;third' delimiter, multiple spaces are
>>> thing = line.split()
>>> print(thing) treated like one delimiter
['first;second;third']
>>> print(len(thing))
1 ● You can specify what delimiter
>>> thing = line.split(';')
>>> print(thing) character to use in the splitting
['first', 'second', 'third']
>>> print(len(thing))
3
>>>
From [email protected] Sat Jan 5 09:14:16 2008

fhand = open('mbox-short.txt') Sat


for line in fhand:
line = line.rstrip() Fri
if not line.startswith('From ') : continue Fri
words = line.split()
print(words[2])
Fri
...

>>> line = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> words = line.split()
>>> print(words)
['From', '[email protected]', 'Sat', 'Jan', '5', '09:14:16', '2008']
>>>
Sometimes we split a line one way, and then grab one of the pieces
of the line and split that piece again

The Double Split Pattern


From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1]
print pieces[1]
The Double Split Pattern

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] [email protected]
print pieces[1]
The Double Split Pattern

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] [email protected]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
The Double Split Pattern

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] [email protected]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print(pieces[1]) 'uct.ac.za'
List Summary
• Concept of a collection • Slicing lists

• Lists and definite loops • List methods: append, remove

• Indexing and lookup • Sorting lists

• List mutability • Splitting strings into lists of words

• Functions: len, min, max, sum • Using split to parse strings


Tuples are another kind of sequence that functions much like a list
- they have elements which are indexed starting at 0

Tuples Are Like Lists


>>> x = ('Glenn', 'Sally', 'Joseph') >>> for iter in y:
>>> print(x[2]) ... print(iter)
Joseph ...
>>> y = ( 1, 9, 2 ) 1
>>> print(y) 9
(1, 9, 2) 2
>>> print(max(y)) >>>
9
but... Tuples are “immutable”
Unlike a list, once you create a tuple, you cannot alter its
contents - similar to a string

>>> x = [9, 8, 7] >>> y = 'ABC' >>> z = (5, 4, 3)


>>> x[2] = 6 >>> y[2] = 'D' >>> z[2] = 0
>>> print(x) Traceback:'str' Traceback:'tuple'
>>>[9, 8, 6] object does object does
>>> not support item not support item
Assignment Assignment
>>> >>>
Things not to do With Tuples
>>> x = (3, 2, 1)
>>> x.sort()
Traceback:
AttributeError: 'tuple' object has no attribute 'sort'
>>> x.append(5)
Traceback:
AttributeError: 'tuple' object has no attribute 'append'
>>> x.reverse()
Traceback:
AttributeError: 'tuple' object has no attribute 'reverse'
>>>
A Tale of Two Sequences
>>> l = list()
>>> dir(l)
['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']

>>> t = tuple()
>>> dir(t)
['count', 'index']
Tuples are More Efficient
• Since Python does not have to build tuple structures to be
modifiable, they are simpler and more efficient in terms of
memory use and performance than lists
• So in our program when we are making “temporary variables”
we prefer tuples over lists
Tuples and Assignment
• We can also put a tuple on the left-hand side of an assignment
statement
• We can even omit the parentheses
>>> (x, y) = (4, 'fred')
>>> print(y)
fred
>>> (a, b) = (99, 98)
>>> print(a)
99
Tuples and Dictionaries
>>> d = dict()
>>> d['csev'] = 2
>>> d['cwen'] = 4
The items() method >>> for (k,v) in d.items():
in dictionaries ... print(k, v)
returns a list of (key, ...
csev 2
value) tuples
cwen 4
>>> tups = d.items()
>>> print(tups)
dict_items([('csev', 2), ('cwen', 4)])
The comparison operators work with tuples and other
sequences. If the first item is equal, Python goes on to the next
element, and so on, until it finds elements that differ.

Tuples are Comparable


>>> (0, 1, 2) < (5, 1, 2)
True
>>> (0, 1, 2000000) < (0, 3, 4)
True
>>> ( 'Jones', 'Sally' ) < ('Jones', 'Sam')
True
>>> ( 'Jones', 'Sally') > ('Adams', 'Sam')
True
Sorting Lists of Tuples
• We can take advantage of the ability to sort a list of tuples to
get a sorted version of a dictionary
• First we sort the dictionary by the key using the items() method
and sorted() function

>>> d = {'a':10, 'b':1, 'c':22}


>>> d.items()
dict_items([('a', 10), ('c', 22), ('b', 1)])
>>> sorted(d.items())
[('a', 10), ('b', 1), ('c', 22)]
Using sorted()
>>> d = {'a':10, 'b':1, 'c':22}
We can do this even >>> t = sorted(d.items())
more directly using the >>> t
built-in function sorted [('a', 10), ('b', 1), ('c', 22)]
that takes a sequence >>> for k, v in sorted(d.items()):
as a parameter and ... print(k, v)
returns a sorted ...
a 10
sequence
b 1
c 22
Sort by Values Instead of Key
• If we could construct a >>> c = {'a':10, 'b':1, 'c':22}
>>> tmp = list()
list of tuples of the
>>> for k, v in c.items() :
form (value, key) we ... tmp.append( (v, k) )
could sort by value ...
>>> print(tmp)
• We do this with a for [(10, 'a'), (22, 'c'), (1, 'b')]
loop that creates a list >>> tmp = sorted(tmp, reverse=True)
of tuples >>> print(tmp)
[(22, 'c'), (10, 'a'), (1, 'b')]
fhand = open('romeo.txt') The top 10 most
counts = {}
for line in fhand:
common words
words = line.split()
for word in words:
counts[word] = counts.get(word, 0 ) + 1

lst = []
for key, val in counts.items():
newtup = (val, key)
lst.append(newtup)

lst = sorted(lst, reverse=True)

for val, key in lst[:10] :


print(key, val)
Even Shorter Version
>>> c = {'a':10, 'b':1, 'c':22}

>>> print( sorted( [ (v,k) for k,v in c.items() ] ) )

[(1, 'b'), (10, 'a'), (22, 'c')]

List comprehension creates a dynamic list. In this case, we


make a list of reversed tuples and then sort it.
http://wiki.python.org/moin/HowTo/Sorting
Summary
• Tuple syntax • Tuples in assignment
statements
• Immutability
• Comparability • Sorting dictionaries by
either key or value
• Sorting

You might also like