SlideShare a Scribd company logo
Python & Perl
Lecture 04
Department of Computer Science
Utah State University
Outline
●
●
●
●
●

Types in Python
Built-in Sequences
List: Python's Workhorse
Function parameters
Scoping
Types in Python
Strong and Dynamic Typing
●

●

●

●

●

Python is both strongly and dynamically typed
Strong typing means that every object has a specific
type
Variables contain references to objects
Dynamic typing means that types of objects pointed to
by variables are inferred at run time
There is no contradiction b/w strong and dynamic typing:
they describe two different features of programming languages
Strong and Dynamic Typing
var = 10
var = 'hello'
●

So hasn't var just changed type ?

●

The answer is NO. var isn't an object at all - it's a name.
Duck Typing
●

●

●

●

Sometimes Python is called a duck typing language
The gist of duck typing: real types of objects do not
matter so long as specific operations can be performed on them
Objects themselves “know” whether they can be added,
multiplied, concatenated, etc
If and when objects cannot perform a requested operation,
a run-time error occurs
Checking Object Types
●

There are two methods in Python for checking object types:
type(<object>) and isinstance(<object>, <type>)
>>> type([1, 2])
<type 'list'>
>>> isinstance([1, 2], list)
True
>>> type((1, 2))
<type 'tuple'>
>>> isinstance((1, 2), tuple)
True
>>> isinstance((1, 2), type((1, 2)))
True
Built-In Sequences
Python Built-In Sequences
●

There are six sequences in Python:


list



tuple



string (str)



unicode string (unicode)



buffer



xrange
Lists
General Facts
●

Lists are mutable type sequences

●

Lists contain 0 or more references to objects

●

Lists can contain references to different types of objects
Construction
# lst1 is an empty list
>>> lst1 = []
>>> lst1
[]
# lst2 is a list of integers.
>>> lst2 = [1, 2, 3, -4, 0, -5, 7]
>>> lst2
[1, 2, 3, -4, 0, -5, 7]
Construction
# lst3 is a mixed list.
>>> lst3 = [1, 'a', 'rock violin', 3.14, "math"]
>>> lst3
[1, 'a', 'rock violin', 3.14, 'math']
# lst4 is a nested list.
>>> lst4 = [1, [2, 3], 4, 5]
# lst5 is another nested list.
>>> lst5 = [1, lst3]
>>> lst5
[1, [1, 'a', 'rock violin', 3.14, 'math']]
Construction
●

list() function can be used as a constructor to
make empty lists or convert other sequences to lists
>>> x = list()
>>> x
[ ]
>>> y = list(“abc”)
>>> y
['a', 'b', 'c']
Sequence Operations
●

All sequences support the following operations:


Indexing



Membership



Concatenation



Multiplication



Slicing



Length



Minimum/Maximum element
Indexing
●

●

Use the square brackets [ and ] to index into lists
Since lists are sequences, left-to-right indexing starts
at 0
>>> lst = [1, 2, 3]
>>> lst[0]
1
>>> lst[1]
2
>>> lst[2]
3
Side Note on Sequence Indexing
●

●

●

In Python sequences, elements can be indexed left to
right and right to left
If s is a sequence, then the leftmost element is s[0] while
the rightmost element is s[-1]
In general, if s is a sequence of n elements, then
s[0] == s[-n], s[1] == s[-n+1], …, s[n-1] == s[-1]
Sequence Indexing Example
>>> s = ['a', 'b', 'c', 'd', 'e']

### s is a list of 5 characters

>>> s[0] == s[-5]

### s[0] == s[-5] == 'a'

True
>>> s[1] == s[-5+1] == s[-4]

### s[1] == s[-4] == 'b'

True
>>> s[2] == s[-5+2] == s[-3]

### s[2] == s[-3] == 'c'

True
>>> s[3] == s[-5+3] == s[-2]

### s[3] == s[-2] == 'd'

True
>>> s[4] == s[-5+4] == s[-1]
True

### s[4] == s[-1] == 'e'
Indexing
●

Right-to-left indexing starts with -1 and ends with -n,
where n is the number of elements in the list
>>> lst = [1, 2, 3]
>>> lst[-1] # 1st element from right
3
>>> lst[-2] # 2nd element from right
2
>>> lst[-3] # 3rd element from right
1
Out of Range Indexing
●

Both positive and negative indices result in errors if they go off on either side of the list
>>> lst = [1, 2, 3]
>>> lst[3]
out of range error
>>> lst[-4]
out of range error
Membership
●

If x is an object and lst is a list, then x in lst returns
True if x is an element of lst, else False is returned
>>> lst = [10, 'eggs', 3]
>>> 10 in lst
True
>>> 'eggs' in lst
True
>>> 'buzz' in lst
False
Membership
●

If x is an object and lst is a list, then x not in lst
returns True if x is not an element of lst, else False
>>> lst = [10, 'eggs', 3]
>>> 11 not in lst
True
>>> 'eggs' not in lst
False
>>> 'buzz' not in lst
True
Membership
●

Membership can be tested on nested lists
>>> lst = [['one', 1], ['two', 2], ['three', 3]]
>>> ['one', 1] in lst
True
>>> ['two', 2] in lst
True
>>> ['three', 3] in lst
True
Side Note On NOT
●

If you want to test if the negation of a boolean expression is true or false, you can use not in front that
expression
>>> not 1 == 2
True
>>> not 1 == 1
False
>>> lst=[1,2,3]
>>> 4 not in lst
True
>>> 1 != 2 ## this works as well
Concatenation
●

If x and y are lists, then x + y is their concatenation,
i.e. the list that consists of x's elements followed by
y's elements
>>> x = [10, 'eggs', 3]
>>> y = [12, 'buzz', 5]
>>> z = x + y
>>> z
[10, 'eggs', 3, 12, 'buzz', 5]
Multiplication
●

If x is a list and n is an integer, then x * n or n * x
is the list that consists of n copies (copies of references
to x) of x
>>> x = [1, 2]
>>> y = ['a', 'b']
>>> z = [x, y]
>>> z
[[1, 2], ['a', 'b']]
>>> z2 = z * 2
>>> z2
[[1, 2], ['a', 'b'], [1, 2], ['a', 'b']]
Slicing
●

●

●

●

Slicing is an operation that accesses a range of elements in a sequence
When the length of the range is 1, slicing is equivalent to indexing
A Slice is defined by two indexes: the start index is
the index of the first element; the end index is the index of the first element that immediately follows the
last element of the slice
The start index is inclusive and the end index is exclusive
Slicing Example
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> lst[0:1]
['a']
>>> lst[0:2]
['a', 'b']
>>> lst[1:2]
['b']
>>> lst[3:7]
['d', 'e', 'f', 'g']
●

Omission of both indexes slices the entire list

Slicing

>>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> lst[:] ## same as lst[0:7]
['a', 'b', 'c', 'd', 'e', 'f', 'g']
●

Omitted start index defaults to 0
>>> lst[:3] ## same as lst[0:3]
['a', 'b', 'c']

●

Omitted end index defaults to the one right after the
last index in the sequence
>>> lst[3:] ## same as lst[3:7]
['d', 'e', 'f', 'g']
Length, Minimum, Maximum
●

These are self explanatory
>>>
>>>
3
>>>
'a'
>>>
'c'

lst = ['a', 'b', 'c']
len(lst)
min(lst)
max(lst)
Multi-Dimensional Lists
●

It is possible to construct multi-dimensional lists

●

Here is an example of a 2D list (aka matrix)
>>> matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> matrix[0]
[0, 1, 2]
>>> matrix[1]
[3, 4, 5]
>>> matrix[0][0]
0
>>> matrix[0][2]
2
Deleting Elements
●

Delete individual element
>>> lst = [1, 2, 3, 4, 5]
>>> del lst[1] ## deleting 2
>>> lst
[1, 3, 4, 5]

●

Delete a slice
>>> del lst[1:3]
>>> lst
[1, 5]

●

Assign an empty list to a slice to delete it
>>> lst[0:2] = []
List Manipulation with Built-In Methods
append(), extend(), reverse(), remove(),
index(), count(), sort()
list.append()
●

The method append() adds an object at the end of
the list
>>> lst1 = [1, 2, 3]
>>> lst1.append('a')
>>> lst1
[1, 2, 3, 'a']
>>> lst1.append(['b', 'c'])
>>> lst1
[1, 2, 3, 'a', ['b', 'c']]
list.extend()
●

The method extend() also destructively adds to the
end of the list, but, unlike append(), does not work
with non-iterable objects
>>> lst1 = [1, 2, 3]
>>> lst1.extend(4) # error
>>> lst1.extend(“abc”)
>>> lst1
[1, 2, 3, 'a', 'b', 'c']
list.append() vs. list.extend()
●

Here is another difference b/w extend() and append():
>>> lst1 = [1, 2, 3]
>>> lst1.append(['a', 'b'])
>>> lst1
[1, 2, 3, ['a', 'b']] ### the last element is a list
>>> lst1 = [1, 2, 3]
>>> lst1.extend(['a', 'b'])
>>> lst1
[1, 2, 3, 'a', 'b'] ### ['a', 'b'] is added at the end of
### lst1 element by element
len(), list.count(), list.index()
●

Let lst be a list

●

lst.len() returns the length of lst

●

lst.count(x) returns number of i's such that
s[i] == x

●

●

lst.index(x, [i, [,j]]) returns smallest k for which
s[k] == x and i <= k < j
Python documentation note: the notation [i, [, j]] means
that parameters i and j are optional: they can both be absent,
one of them can be absent, or they can both be present
len(), list.count(), list.index()
>>> lst = ['a', 'a', 1, 1, 1, 'b', 2, 2, 3]
>>> len(lst)
9
>>> lst.count('a')
2
>>> lst.count(1)
3
>>> lst.index('a', 0, 2)
0
>>> lst.index('a', 1, 2)
1
>>> lst.index('a', 2)
ValueError: list.index(x): x not in list
list.remove(), list.reverse()
●

Let lst be a list

●

lst.remove(x) is the same as
del lst[lst.index(x)]

●

lst.reverse() reverses lst in place

●

lst.reverse() does not return a value
list.remove(), list.reverse()
>>> lst = [1, 2, 3, 4, 5]
>>> lst.remove(1)
>>> lst
[2, 3, 4, 5]
>>> lst = [1, 2, 3, 4, 5]
>>> del lst[lst.index(1)]
>>> lst
[2, 3, 4, 5]
>>> lst.reverse()
>>> lst
[5, 4, 3, 2]
Function Parameters
Types of Parameters
●

●

●

There are two types of parameters in Python:
positional and keyword
Positional parameters are regular parameters in the
function signature: the values they receive are
determined by their position in the signature
Keyword parameters are parameters that can take on
default values and whose position can vary in the
functional signature
Positional Parameters: Example
def hw_avail_str(crsn, hwn, hw_loc, due_date, submit_loc):
print "%s Assignment %s is available at %s. It is due by %s in %s." % 
(crsn, hwn, hw_loc, due_date, submit_loc)
>>> hw_avail_str('CS3430', '2', 'www.canvas.org', '11:59pm, Jan 25, 2014',
'your canvas')
CS3430 Assignment 2 is available at www.canvas.org. It is due 11:59pm, Jan
25, 2014 in your canvas .
●

Note: You have to remember the position of each parameter in
the signature, i.e., that crsn (course number) comes first, hwn
(homework number) comes second, hw_loc (homework web
location) comes third, etc.
Keyword Parameters: Example
def hw_avail_str2(crsn='CS3430', hwn='0', hw_loc='www.myblog.org',
due_date='', submit_loc='your canvas'):
print "%s Assignment %s is available at %s. It is due by %s in %s." % 
(crsn, hwn, hw_loc, due_date, submit_loc)
>>> hw_avail_str2(hwn='2', due_date='11:59pm, Jan 25, 2014')
CS3430 Assignment 2 is available at www.myblog.org. It is due by 11:59pm,
Jan 25, 2014 in your canvas.
●

Note: You do not have to remember the position of each
parameter in the signature but you do have to remember the
name of each keyword parameter
Combining Positional & Keyword Parameters
●

Positional & keyword parameters can be combined in one
signature: positional parameters must precede keyword
parameters
def combine(x, y=10, z='buzz'):
print 'x=%d y=%d z=%s' % (x, y, z)
>>> combine(5)
x=5 y=10 z=buzz
>>> combine(5, y=50, z='foo')
x=5 y=50 z=foo
Positional Parameter Collection
●

●

●

●

What happens if you do not know how many parameter values
you receive on the input?
There are two choices: use a container, e.g., a list or a tuple, or
use *operator
Suppose that we want to write a function that applies three types
of operations: sum, min, and max to sequences
We can use *operator to solve this problem
Applying Operations to Parameter Collections
def apply_oper(oper_name, *params):
if oper_name == 'sum':
return sum(params)
elif oper_name == 'min':
if len(params) > 0:
return min(params)
else:
return None
elif oper_name == 'max':
if len(params) > 0:
return max(params)
else:
return None
else:
return 'Unknown operation ' + oper_name
Applying Operations to Parameter Collections
>>> apply_oper('sum', 1, 2)
3
>>> apply_oper('sum', 1, 2, 3)
6
>>> apply_oper('sum')
0
>>> apply_oper('min', 1, 2, 3)
1
>>> apply_oper('max', 1, 2, 3, 4, 5)
5
>>> apply_oper('max')
Scoping
Scope
●

●

●

Scopes (aka namespaces) are dictionaries that map
variables to their values
Builtin function vars() returns the current scope
Python documentation recommends against
modifying the returned value of vars() because the
results are undefined
Scope
●

def always introduces a new scope

●

Here is a quick def scoping example:
>>> x = 10
>>> def f(y):
x = y ## changes x to y only inside f
print 'x=%d'%x
>>> f(20)
>>> x ## we are outside the scope of f so x is 10
10
Scope
●

It is possible to change the value of a global variable
inside a function: all you have to do is to tell Python
that the variable is global
def change_x_val(z):
global x
x=z
>>> x = 1
>>> change_x_val(10)
>>> x
10
Scope Sensitivity of vars()
●

Calls to vars() are scope-sensitive and return the currently
active scopes
def local_vars2(x, y):
def local_vars3(n, m):
n=x+1
m=y+2
print 'vars() inside local_vars3'
print vars()
z = x + 10
w = y + 20
print 'vars() inside local_vars2'
print vars()
local_vars3(3, 4)
Scope Sensitivity of vars()
>>> local_vars2(1, 2)
vars() inside local_vars2
{'y': 2, 'x': 1, 'local_vars3': <function
local_vars3 at 0x020B10B0>, 'z': 11, 'w': 22}
vars() inside local_vars3
{'y': 2, 'x': 1, 'm': 4, 'n': 2}
Global Scope
●

●

Builtin function globals() returns the global scope
regardless of what the current scope is
For example, the following function prints the global
scope
def global_vars(x, y):
z=x+1
w=y+2
print globals()
Global Scope

●

This function prints the global scope several times
despite the scope nestings
def global_vars2(x, y):
def global_vars3(n, m):
n=x+1
m=y+2
print 'globals() inside global_vars3', globals()
z = x + 10
w = y + 20
print 'globals() inside global_vars2', globals()
global_vars3(3, 4)
Nested Scopes

●

Nested scopes are very useful in functions that return other
functions
def func_factory(oper, lst):
def list_extender(inner_lst):
inner_lst.extend(lst)
def list_remover(inner_lst):
for i in lst: inner_lst.remove(i)
if oper == 'extend':
return list_extender
elif oper == 'remove':
return list_remover
else:
return 'ERROR: Unknown operation'
Nested Scopes
●

Nested scopes are very useful in functions that return other
functions
>>> fex = func_factory('extend', ['I', 'am', 'extended'])
>>> frm = func_factory('remove', ['I', 'am', 'extended'])
>>> lst = [1, 2, 3]
>>> fex(lst) ## lst is extended by ['I', 'am', 'extended']
>>> lst
[1, 2, 3, 'I', 'am', 'extended']
>>> frm(lst) ## all elements of ['I', 'am', 'extended'] are removed
from lst
>>> lst
[1, 2, 3]
Reading & References
●

www.python.org

●

http://docs.python.org/library/stdtypes.html#typesseq

●

doc.python.org/howto/unicode.html

●

Ch 02, M. L. Hetland. Beginning Python From Novice to
nd
Professional, 2 Ed., APRESS

More Related Content

What's hot

6. R data structures
6. R data structures6. R data structures
6. R data structures
ExternalEvents
 
Python data type
Python data typePython data type
Python data type
Jaya Kumari
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
kalyanineve
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
1. python
1. python1. python
1. python
PRASHANT OJHA
 
Sets in python
Sets in pythonSets in python
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
TeresaJencyBala
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
H K
 
Day 1b R structures objects.pptx
Day 1b   R structures   objects.pptxDay 1b   R structures   objects.pptx
Day 1b R structures objects.pptx
Adrien Melquiond
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
vibrantuser
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
Praveen M Jigajinni
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in R
naroranisha
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
Venugopalavarma Raja
 
Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3
Hariz Mustafa
 
Class 9: Consistent Hashing
Class 9: Consistent HashingClass 9: Consistent Hashing
Class 9: Consistent Hashing
David Evans
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
Josh Doyle
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
timourian
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 

What's hot (20)

6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Python data type
Python data typePython data type
Python data type
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
1. python
1. python1. python
1. python
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
 
Day 1b R structures objects.pptx
Day 1b   R structures   objects.pptxDay 1b   R structures   objects.pptx
Day 1b R structures objects.pptx
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in R
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
 
Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3Lecture07 the linked-list_as_a_data_structure_v3
Lecture07 the linked-list_as_a_data_structure_v3
 
Class 9: Consistent Hashing
Class 9: Consistent HashingClass 9: Consistent Hashing
Class 9: Consistent Hashing
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 

Viewers also liked

Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19
jordplan
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
Tanwir Zaman
 
Jonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_finalJonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_final
jordplan
 
Jordplan faktaark a4
Jordplan faktaark a4Jordplan faktaark a4
Jordplan faktaark a4
jordplan
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
Tanwir Zaman
 
Cs3430 lecture 13
Cs3430 lecture 13Cs3430 lecture 13
Cs3430 lecture 13
Tanwir Zaman
 
Jordplan faktaark
Jordplan faktaarkJordplan faktaark
Jordplan faktaarkjordplan
 
Tov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapTov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapjordplan
 
Python lecture 8
Python lecture 8Python lecture 8
Python lecture 8
Tanwir Zaman
 

Viewers also liked (9)

Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
 
Jonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_finalJonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_final
 
Jordplan faktaark a4
Jordplan faktaark a4Jordplan faktaark a4
Jordplan faktaark a4
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
 
Cs3430 lecture 13
Cs3430 lecture 13Cs3430 lecture 13
Cs3430 lecture 13
 
Jordplan faktaark
Jordplan faktaarkJordplan faktaark
Jordplan faktaark
 
Tov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapTov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskap
 
Python lecture 8
Python lecture 8Python lecture 8
Python lecture 8
 

Similar to Python lecture 04

Module III.pdf
Module III.pdfModule III.pdf
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
GaneshRaghu4
 
Python lists
Python listsPython lists
Python lists
nuripatidar
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
AnitaDevi158873
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
AUNGHTET61
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
TanTran598844
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
RamiHarrathi1
 
Python list concept
Python list conceptPython list concept
Python list concept
DrJSaiGeetha
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
Asst.prof M.Gokilavani
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir1
 
Pythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of ProgrammingPythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
GaganRaj28
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
Panimalar Engineering College
 
Pytho lists
Pytho listsPytho lists
Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
Inzamam Baig
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
Koteswari Kasireddy
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
NehaSpillai1
 
Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
NehaSpillai1
 

Similar to Python lecture 04 (20)

Module III.pdf
Module III.pdfModule III.pdf
Module III.pdf
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
 
Python lists
Python listsPython lists
Python lists
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
Python list concept
Python list conceptPython list concept
Python list concept
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Pythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of ProgrammingPythonlearn-08-Lists for fundatmentals of Programming
Pythonlearn-08-Lists for fundatmentals of Programming
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Pytho lists
Pytho listsPytho lists
Pytho lists
 
Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
 
Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
 

More from Tanwir Zaman

Cs3430 lecture 17
Cs3430 lecture 17Cs3430 lecture 17
Cs3430 lecture 17
Tanwir Zaman
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
Tanwir Zaman
 
Cs3430 lecture 14
Cs3430 lecture 14Cs3430 lecture 14
Cs3430 lecture 14
Tanwir Zaman
 
Cs3430 lecture 16
Cs3430 lecture 16Cs3430 lecture 16
Cs3430 lecture 16
Tanwir Zaman
 
Python lecture 12
Python lecture 12Python lecture 12
Python lecture 12
Tanwir Zaman
 
Python lecture 09
Python lecture 09Python lecture 09
Python lecture 09
Tanwir Zaman
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
Tanwir Zaman
 
Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
Tanwir Zaman
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
Tanwir Zaman
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
Tanwir Zaman
 
Python lecture 01
Python lecture 01Python lecture 01
Python lecture 01
Tanwir Zaman
 
Python lecture 11
Python lecture 11Python lecture 11
Python lecture 11
Tanwir Zaman
 

More from Tanwir Zaman (12)

Cs3430 lecture 17
Cs3430 lecture 17Cs3430 lecture 17
Cs3430 lecture 17
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 
Cs3430 lecture 14
Cs3430 lecture 14Cs3430 lecture 14
Cs3430 lecture 14
 
Cs3430 lecture 16
Cs3430 lecture 16Cs3430 lecture 16
Cs3430 lecture 16
 
Python lecture 12
Python lecture 12Python lecture 12
Python lecture 12
 
Python lecture 09
Python lecture 09Python lecture 09
Python lecture 09
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
 
Python lecture 01
Python lecture 01Python lecture 01
Python lecture 01
 
Python lecture 11
Python lecture 11Python lecture 11
Python lecture 11
 

Recently uploaded

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 

Recently uploaded (20)

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 

Python lecture 04

  • 1. Python & Perl Lecture 04 Department of Computer Science Utah State University
  • 2. Outline ● ● ● ● ● Types in Python Built-in Sequences List: Python's Workhorse Function parameters Scoping
  • 4. Strong and Dynamic Typing ● ● ● ● ● Python is both strongly and dynamically typed Strong typing means that every object has a specific type Variables contain references to objects Dynamic typing means that types of objects pointed to by variables are inferred at run time There is no contradiction b/w strong and dynamic typing: they describe two different features of programming languages
  • 5. Strong and Dynamic Typing var = 10 var = 'hello' ● So hasn't var just changed type ? ● The answer is NO. var isn't an object at all - it's a name.
  • 6. Duck Typing ● ● ● ● Sometimes Python is called a duck typing language The gist of duck typing: real types of objects do not matter so long as specific operations can be performed on them Objects themselves “know” whether they can be added, multiplied, concatenated, etc If and when objects cannot perform a requested operation, a run-time error occurs
  • 7. Checking Object Types ● There are two methods in Python for checking object types: type(<object>) and isinstance(<object>, <type>) >>> type([1, 2]) <type 'list'> >>> isinstance([1, 2], list) True >>> type((1, 2)) <type 'tuple'> >>> isinstance((1, 2), tuple) True >>> isinstance((1, 2), type((1, 2))) True
  • 9. Python Built-In Sequences ● There are six sequences in Python:  list  tuple  string (str)  unicode string (unicode)  buffer  xrange
  • 10. Lists
  • 11. General Facts ● Lists are mutable type sequences ● Lists contain 0 or more references to objects ● Lists can contain references to different types of objects
  • 12. Construction # lst1 is an empty list >>> lst1 = [] >>> lst1 [] # lst2 is a list of integers. >>> lst2 = [1, 2, 3, -4, 0, -5, 7] >>> lst2 [1, 2, 3, -4, 0, -5, 7]
  • 13. Construction # lst3 is a mixed list. >>> lst3 = [1, 'a', 'rock violin', 3.14, "math"] >>> lst3 [1, 'a', 'rock violin', 3.14, 'math'] # lst4 is a nested list. >>> lst4 = [1, [2, 3], 4, 5] # lst5 is another nested list. >>> lst5 = [1, lst3] >>> lst5 [1, [1, 'a', 'rock violin', 3.14, 'math']]
  • 14. Construction ● list() function can be used as a constructor to make empty lists or convert other sequences to lists >>> x = list() >>> x [ ] >>> y = list(“abc”) >>> y ['a', 'b', 'c']
  • 15. Sequence Operations ● All sequences support the following operations:  Indexing  Membership  Concatenation  Multiplication  Slicing  Length  Minimum/Maximum element
  • 16. Indexing ● ● Use the square brackets [ and ] to index into lists Since lists are sequences, left-to-right indexing starts at 0 >>> lst = [1, 2, 3] >>> lst[0] 1 >>> lst[1] 2 >>> lst[2] 3
  • 17. Side Note on Sequence Indexing ● ● ● In Python sequences, elements can be indexed left to right and right to left If s is a sequence, then the leftmost element is s[0] while the rightmost element is s[-1] In general, if s is a sequence of n elements, then s[0] == s[-n], s[1] == s[-n+1], …, s[n-1] == s[-1]
  • 18. Sequence Indexing Example >>> s = ['a', 'b', 'c', 'd', 'e'] ### s is a list of 5 characters >>> s[0] == s[-5] ### s[0] == s[-5] == 'a' True >>> s[1] == s[-5+1] == s[-4] ### s[1] == s[-4] == 'b' True >>> s[2] == s[-5+2] == s[-3] ### s[2] == s[-3] == 'c' True >>> s[3] == s[-5+3] == s[-2] ### s[3] == s[-2] == 'd' True >>> s[4] == s[-5+4] == s[-1] True ### s[4] == s[-1] == 'e'
  • 19. Indexing ● Right-to-left indexing starts with -1 and ends with -n, where n is the number of elements in the list >>> lst = [1, 2, 3] >>> lst[-1] # 1st element from right 3 >>> lst[-2] # 2nd element from right 2 >>> lst[-3] # 3rd element from right 1
  • 20. Out of Range Indexing ● Both positive and negative indices result in errors if they go off on either side of the list >>> lst = [1, 2, 3] >>> lst[3] out of range error >>> lst[-4] out of range error
  • 21. Membership ● If x is an object and lst is a list, then x in lst returns True if x is an element of lst, else False is returned >>> lst = [10, 'eggs', 3] >>> 10 in lst True >>> 'eggs' in lst True >>> 'buzz' in lst False
  • 22. Membership ● If x is an object and lst is a list, then x not in lst returns True if x is not an element of lst, else False >>> lst = [10, 'eggs', 3] >>> 11 not in lst True >>> 'eggs' not in lst False >>> 'buzz' not in lst True
  • 23. Membership ● Membership can be tested on nested lists >>> lst = [['one', 1], ['two', 2], ['three', 3]] >>> ['one', 1] in lst True >>> ['two', 2] in lst True >>> ['three', 3] in lst True
  • 24. Side Note On NOT ● If you want to test if the negation of a boolean expression is true or false, you can use not in front that expression >>> not 1 == 2 True >>> not 1 == 1 False >>> lst=[1,2,3] >>> 4 not in lst True >>> 1 != 2 ## this works as well
  • 25. Concatenation ● If x and y are lists, then x + y is their concatenation, i.e. the list that consists of x's elements followed by y's elements >>> x = [10, 'eggs', 3] >>> y = [12, 'buzz', 5] >>> z = x + y >>> z [10, 'eggs', 3, 12, 'buzz', 5]
  • 26. Multiplication ● If x is a list and n is an integer, then x * n or n * x is the list that consists of n copies (copies of references to x) of x >>> x = [1, 2] >>> y = ['a', 'b'] >>> z = [x, y] >>> z [[1, 2], ['a', 'b']] >>> z2 = z * 2 >>> z2 [[1, 2], ['a', 'b'], [1, 2], ['a', 'b']]
  • 27. Slicing ● ● ● ● Slicing is an operation that accesses a range of elements in a sequence When the length of the range is 1, slicing is equivalent to indexing A Slice is defined by two indexes: the start index is the index of the first element; the end index is the index of the first element that immediately follows the last element of the slice The start index is inclusive and the end index is exclusive
  • 28. Slicing Example >>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> lst[0:1] ['a'] >>> lst[0:2] ['a', 'b'] >>> lst[1:2] ['b'] >>> lst[3:7] ['d', 'e', 'f', 'g']
  • 29. ● Omission of both indexes slices the entire list Slicing >>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> lst[:] ## same as lst[0:7] ['a', 'b', 'c', 'd', 'e', 'f', 'g'] ● Omitted start index defaults to 0 >>> lst[:3] ## same as lst[0:3] ['a', 'b', 'c'] ● Omitted end index defaults to the one right after the last index in the sequence >>> lst[3:] ## same as lst[3:7] ['d', 'e', 'f', 'g']
  • 30. Length, Minimum, Maximum ● These are self explanatory >>> >>> 3 >>> 'a' >>> 'c' lst = ['a', 'b', 'c'] len(lst) min(lst) max(lst)
  • 31. Multi-Dimensional Lists ● It is possible to construct multi-dimensional lists ● Here is an example of a 2D list (aka matrix) >>> matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> matrix[0] [0, 1, 2] >>> matrix[1] [3, 4, 5] >>> matrix[0][0] 0 >>> matrix[0][2] 2
  • 32. Deleting Elements ● Delete individual element >>> lst = [1, 2, 3, 4, 5] >>> del lst[1] ## deleting 2 >>> lst [1, 3, 4, 5] ● Delete a slice >>> del lst[1:3] >>> lst [1, 5] ● Assign an empty list to a slice to delete it >>> lst[0:2] = []
  • 33. List Manipulation with Built-In Methods append(), extend(), reverse(), remove(), index(), count(), sort()
  • 34. list.append() ● The method append() adds an object at the end of the list >>> lst1 = [1, 2, 3] >>> lst1.append('a') >>> lst1 [1, 2, 3, 'a'] >>> lst1.append(['b', 'c']) >>> lst1 [1, 2, 3, 'a', ['b', 'c']]
  • 35. list.extend() ● The method extend() also destructively adds to the end of the list, but, unlike append(), does not work with non-iterable objects >>> lst1 = [1, 2, 3] >>> lst1.extend(4) # error >>> lst1.extend(“abc”) >>> lst1 [1, 2, 3, 'a', 'b', 'c']
  • 36. list.append() vs. list.extend() ● Here is another difference b/w extend() and append(): >>> lst1 = [1, 2, 3] >>> lst1.append(['a', 'b']) >>> lst1 [1, 2, 3, ['a', 'b']] ### the last element is a list >>> lst1 = [1, 2, 3] >>> lst1.extend(['a', 'b']) >>> lst1 [1, 2, 3, 'a', 'b'] ### ['a', 'b'] is added at the end of ### lst1 element by element
  • 37. len(), list.count(), list.index() ● Let lst be a list ● lst.len() returns the length of lst ● lst.count(x) returns number of i's such that s[i] == x ● ● lst.index(x, [i, [,j]]) returns smallest k for which s[k] == x and i <= k < j Python documentation note: the notation [i, [, j]] means that parameters i and j are optional: they can both be absent, one of them can be absent, or they can both be present
  • 38. len(), list.count(), list.index() >>> lst = ['a', 'a', 1, 1, 1, 'b', 2, 2, 3] >>> len(lst) 9 >>> lst.count('a') 2 >>> lst.count(1) 3 >>> lst.index('a', 0, 2) 0 >>> lst.index('a', 1, 2) 1 >>> lst.index('a', 2) ValueError: list.index(x): x not in list
  • 39. list.remove(), list.reverse() ● Let lst be a list ● lst.remove(x) is the same as del lst[lst.index(x)] ● lst.reverse() reverses lst in place ● lst.reverse() does not return a value
  • 40. list.remove(), list.reverse() >>> lst = [1, 2, 3, 4, 5] >>> lst.remove(1) >>> lst [2, 3, 4, 5] >>> lst = [1, 2, 3, 4, 5] >>> del lst[lst.index(1)] >>> lst [2, 3, 4, 5] >>> lst.reverse() >>> lst [5, 4, 3, 2]
  • 42. Types of Parameters ● ● ● There are two types of parameters in Python: positional and keyword Positional parameters are regular parameters in the function signature: the values they receive are determined by their position in the signature Keyword parameters are parameters that can take on default values and whose position can vary in the functional signature
  • 43. Positional Parameters: Example def hw_avail_str(crsn, hwn, hw_loc, due_date, submit_loc): print "%s Assignment %s is available at %s. It is due by %s in %s." % (crsn, hwn, hw_loc, due_date, submit_loc) >>> hw_avail_str('CS3430', '2', 'www.canvas.org', '11:59pm, Jan 25, 2014', 'your canvas') CS3430 Assignment 2 is available at www.canvas.org. It is due 11:59pm, Jan 25, 2014 in your canvas . ● Note: You have to remember the position of each parameter in the signature, i.e., that crsn (course number) comes first, hwn (homework number) comes second, hw_loc (homework web location) comes third, etc.
  • 44. Keyword Parameters: Example def hw_avail_str2(crsn='CS3430', hwn='0', hw_loc='www.myblog.org', due_date='', submit_loc='your canvas'): print "%s Assignment %s is available at %s. It is due by %s in %s." % (crsn, hwn, hw_loc, due_date, submit_loc) >>> hw_avail_str2(hwn='2', due_date='11:59pm, Jan 25, 2014') CS3430 Assignment 2 is available at www.myblog.org. It is due by 11:59pm, Jan 25, 2014 in your canvas. ● Note: You do not have to remember the position of each parameter in the signature but you do have to remember the name of each keyword parameter
  • 45. Combining Positional & Keyword Parameters ● Positional & keyword parameters can be combined in one signature: positional parameters must precede keyword parameters def combine(x, y=10, z='buzz'): print 'x=%d y=%d z=%s' % (x, y, z) >>> combine(5) x=5 y=10 z=buzz >>> combine(5, y=50, z='foo') x=5 y=50 z=foo
  • 46. Positional Parameter Collection ● ● ● ● What happens if you do not know how many parameter values you receive on the input? There are two choices: use a container, e.g., a list or a tuple, or use *operator Suppose that we want to write a function that applies three types of operations: sum, min, and max to sequences We can use *operator to solve this problem
  • 47. Applying Operations to Parameter Collections def apply_oper(oper_name, *params): if oper_name == 'sum': return sum(params) elif oper_name == 'min': if len(params) > 0: return min(params) else: return None elif oper_name == 'max': if len(params) > 0: return max(params) else: return None else: return 'Unknown operation ' + oper_name
  • 48. Applying Operations to Parameter Collections >>> apply_oper('sum', 1, 2) 3 >>> apply_oper('sum', 1, 2, 3) 6 >>> apply_oper('sum') 0 >>> apply_oper('min', 1, 2, 3) 1 >>> apply_oper('max', 1, 2, 3, 4, 5) 5 >>> apply_oper('max')
  • 50. Scope ● ● ● Scopes (aka namespaces) are dictionaries that map variables to their values Builtin function vars() returns the current scope Python documentation recommends against modifying the returned value of vars() because the results are undefined
  • 51. Scope ● def always introduces a new scope ● Here is a quick def scoping example: >>> x = 10 >>> def f(y): x = y ## changes x to y only inside f print 'x=%d'%x >>> f(20) >>> x ## we are outside the scope of f so x is 10 10
  • 52. Scope ● It is possible to change the value of a global variable inside a function: all you have to do is to tell Python that the variable is global def change_x_val(z): global x x=z >>> x = 1 >>> change_x_val(10) >>> x 10
  • 53. Scope Sensitivity of vars() ● Calls to vars() are scope-sensitive and return the currently active scopes def local_vars2(x, y): def local_vars3(n, m): n=x+1 m=y+2 print 'vars() inside local_vars3' print vars() z = x + 10 w = y + 20 print 'vars() inside local_vars2' print vars() local_vars3(3, 4)
  • 54. Scope Sensitivity of vars() >>> local_vars2(1, 2) vars() inside local_vars2 {'y': 2, 'x': 1, 'local_vars3': <function local_vars3 at 0x020B10B0>, 'z': 11, 'w': 22} vars() inside local_vars3 {'y': 2, 'x': 1, 'm': 4, 'n': 2}
  • 55. Global Scope ● ● Builtin function globals() returns the global scope regardless of what the current scope is For example, the following function prints the global scope def global_vars(x, y): z=x+1 w=y+2 print globals()
  • 56. Global Scope ● This function prints the global scope several times despite the scope nestings def global_vars2(x, y): def global_vars3(n, m): n=x+1 m=y+2 print 'globals() inside global_vars3', globals() z = x + 10 w = y + 20 print 'globals() inside global_vars2', globals() global_vars3(3, 4)
  • 57. Nested Scopes ● Nested scopes are very useful in functions that return other functions def func_factory(oper, lst): def list_extender(inner_lst): inner_lst.extend(lst) def list_remover(inner_lst): for i in lst: inner_lst.remove(i) if oper == 'extend': return list_extender elif oper == 'remove': return list_remover else: return 'ERROR: Unknown operation'
  • 58. Nested Scopes ● Nested scopes are very useful in functions that return other functions >>> fex = func_factory('extend', ['I', 'am', 'extended']) >>> frm = func_factory('remove', ['I', 'am', 'extended']) >>> lst = [1, 2, 3] >>> fex(lst) ## lst is extended by ['I', 'am', 'extended'] >>> lst [1, 2, 3, 'I', 'am', 'extended'] >>> frm(lst) ## all elements of ['I', 'am', 'extended'] are removed from lst >>> lst [1, 2, 3]