Skip to document

Practical-sheet 3-v2 - Practical work

Practical work
Course

Engineering

804 Documents
Students shared 804 documents in this course
Academic year: 2013/2014
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
SRM Valliammai Engineering College

Comments

Please sign in or register to post comments.

Preview text

CPDठ⃚forठ⃚Aठ⃚Levelठ⃚Computerठ⃚Science:ठ⃚Practicalठ⃚Sheetठ⃚3ठ⃚ठ⃚ Octoberठ⃚14ठ⃚

Page 1 of 3ठ⃚

Practicalठ⃚Sheetठ⃚3ठ⃚ Moreठ⃚Advancedठ⃚Python ठ⃚

1 Multipleठ⃚Dimensionalठ⃚Arraysठ⃚

ठ⃚

Exerciseठ⃚1:ठ⃚Sumठ⃚Columnsठ⃚ofठ⃚Tableठ⃚ Writeठ⃚aठ⃚functionठ⃚toठ⃚sumठ⃚theठ⃚columnsठ⃚ofठ⃚aठ⃚2ठ⃚dimensionalठ⃚arrayठ⃚(i.ठ⃚aठ⃚listठ⃚ofठ⃚lists).ठ⃚Theठ⃚ functionठ⃚shouldठ⃚workठ⃚forठ⃚anyठ⃚sizeठ⃚tableठ⃚(providingठ⃚itठ⃚isठ⃚regularठ⃚inठ⃚shape).ठ⃚ Note:ठ⃚thisठ⃚ exerciseठ⃚assumesठ⃚youठ⃚areठ⃚familiarठ⃚withठ⃚functions.ठ⃚Weठ⃚reviseठ⃚functionsठ⃚inठ⃚theठ⃚nextठ⃚session.ठ⃚ Forठ⃚example,ठ⃚with:ठ⃚ table1 = [
[1, 2, 3, 4],
[4, 5, 6, 7],
[8, 9,10,11] ] weठ⃚get:ठ⃚ >>> sumCol(table1,0) 13 >>> sumCol(table1,1) 16 Exerciseठ⃚1:ठ⃚Swappingठ⃚Rowsठ⃚andठ⃚Columnsठ⃚ Writeठ⃚aठ⃚functionठ⃚toठ⃚swapठ⃚rowठ⃚andठ⃚columns,ठ⃚forठ⃚anyठ⃚sizeठ⃚orठ⃚shapeठ⃚ofठ⃚table.ठ⃚Thisठ⃚functionठ⃚isठ⃚ usuallyठ⃚calledठ⃚8transpose9.ठ⃚Forठ⃚example,ठ⃚withठ⃚table:ठ⃚ table2 = [
[1,2,3,4],
[5,6,7,8] ] weठ⃚get:ठ⃚ >>> transpose(table2) [[1, 5], [2, 6], [3, 7], [4, 8]] Note:ठ⃚thisठ⃚exerciseठ⃚isठ⃚quiteठ⃚hard.ठ⃚

2 Python'sठ⃚Builtૐ퀐inठ⃚Typesठ⃚

2 Rangesठ⃚

ठ⃚

Exerciseठ⃚2:ठ⃚Tryingठ⃚Rangesठ⃚ Theठ⃚list()ठ⃚functionठ⃚createsठ⃚aठ⃚listठ⃚fromठ⃚aठ⃚range.ठ⃚Useठ⃚thisठ⃚toठ⃚experimentठ⃚withठ⃚ranges,ठ⃚ checkingठ⃚thatठ⃚youठ⃚understandठ⃚howठ⃚theyठ⃚work.ठ⃚Forठ⃚example:ठ⃚ range(10) range(10, 20) range(10, 40, 2) Hereठ⃚isठ⃚anठ⃚example:ठ⃚ >>> r = range(10) >>> list(r) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

CPDठ⃚forठ⃚Aठ⃚Levelठ⃚Computerठ⃚Science:ठ⃚Practicalठ⃚Sheetठ⃚3ठ⃚ठ⃚ Octoberठ⃚14ठ⃚

Page 2 of 3ठ⃚

Exerciseठ⃚2:ठ⃚Usingठ⃚Forठ⃚Loopsठ⃚ Practiceठ⃚usingठ⃚forठ⃚loops.ठ⃚Forठ⃚example,ठ⃚rewriteठ⃚theठ⃚followingठ⃚whileठ⃚loopठ⃚usingठ⃚aठ⃚forठ⃚loopठ⃚ andठ⃚aठ⃚range:ठ⃚ठ⃚ठ⃚ x= name="william" while x < len(name): print(x,":",name[x]) x = x + 1 Createठ⃚andठ⃚solveठ⃚someठ⃚similarठ⃚problems.ठ⃚ Exerciseठ⃚2:ठ⃚Teachingठ⃚Forठ⃚Loopsठ⃚ Discussठ⃚withठ⃚anotherठ⃚courseठ⃚memberठ⃚whenठ⃚/ठ⃚whetherठ⃚toठ⃚teachठ⃚forठ⃚loopsठ⃚(andठ⃚ranges)ठ⃚inठ⃚ Python.ठ⃚ठ⃚

2 Tuplesठ⃚

Exerciseठ⃚2:ठ⃚Tryingठ⃚Tupleठ⃚ Enterठ⃚aठ⃚tupleठ⃚suchठ⃚as:ठ⃚t = (3,1,5,8)ठ⃚Thenठ⃚tryठ⃚eachठ⃚ofठ⃚theठ⃚followठ⃚expressionsठ⃚toठ⃚seeठ⃚ifठ⃚ a)ठ⃚itठ⃚isठ⃚legalठ⃚b)ठ⃚whatठ⃚theठ⃚resultठ⃚is:ठ⃚ठ⃚ t[1] t[1:2] t[1:3] len(t) t[2]= ठ⃚ Exerciseठ⃚2:ठ⃚Twoठ⃚Listsठ⃚toठ⃚aठ⃚Listठ⃚ofठ⃚Pairsठ⃚ Writeठ⃚aठ⃚functionठ⃚thatठ⃚takesठ⃚twoठ⃚listsठ⃚andठ⃚returnsठ⃚aठ⃚listठ⃚ofठ⃚pairs.ठ⃚Thisठ⃚functionठ⃚isठ⃚oftenठ⃚ calledठ⃚8zip9.ठ⃚Forठ⃚example:ठ⃚ >>> zip([1,2,3],["a","b","c"]) [(1, 'a'), (2, 'b'), (3, 'c')] ठ⃚

2 Setsठ⃚

Exerciseठ⃚2:ठ⃚Multiplesठ⃚ofठ⃚7ठ⃚andठ⃚5ठ⃚ Supposeठ⃚weठ⃚wantठ⃚toठ⃚getठ⃚theठ⃚multiplesठ⃚ofठ⃚someठ⃚numberठ⃚N,ठ⃚belowठ⃚aठ⃚limitठ⃚L.ठ⃚Weठ⃚canठ⃚ expressठ⃚thisठ⃚asठ⃚aठ⃚range:ठ⃚range(N,ठ⃚L,ठ⃚N).ठ⃚Forठ⃚example:ठ⃚ Useठ⃚thisठ⃚ideaठ⃚andठ⃚theठ⃚setठ⃚intersectionठ⃚operationठ⃚toठ⃚writeठ⃚aठ⃚functionठ⃚toठ⃚giveठ⃚multiplesठ⃚ofठ⃚ twoठ⃚numbersठ⃚belowठ⃚someठ⃚limit.ठ⃚Hereठ⃚areठ⃚someठ⃚examples:ठ⃚ठ⃚ >>> multiplesNandM(5, 7, 100) [35, 70] >>> multiplesNandM(13, 17, 1000) [442, 884, 221, 663] ठ⃚

Was this document helpful?

Practical-sheet 3-v2 - Practical work

Course: Engineering

804 Documents
Students shared 804 documents in this course
Was this document helpful?
CPD$for$A$Level$Computer$Science:$Practical$Sheet$3$$ October$14$
Page 1 of 3$
Practical(Sheet(3(
More(Advanced(Python!
1 Multiple)Dimensional)Arrays)
(
Exercise(1.1:(Sum(Columns(of(Table(
Write!a!function!to!sum!the!columns!of!a!2!dimensional!array!(i.e.!a!list!of!lists).!The!
function!should!work!for!any!size!table!(providing!it!is!regular!in!shape).!Note:$this$
exercise$assumes$you$are$familiar$with$functions.$We$revise$functions$in$the$next$session.$
For!example,!with:!
table1 = [ \
[1, 2, 3, 4], \
[4, 5, 6, 7], \
[8, 9,10,11] ]
we!get:!
>>> sumCol(table1,0)
13
>>> sumCol(table1,1)
16
Exercise(1.2:(Swapping(Rows(and(Columns(
Write!a!function!to!swap!row!and!columns,!for!any!size!or!shape!of!table.!This!function!is!
usually!called!8transpose9.!For!example,!with!table:!
table2 = [ \
[1,2,3,4], \
[5,6,7,8] ]
we!get:!
>>> transpose(table2)
[[1, 5], [2, 6], [3, 7], [4, 8]]
Note:$this$exercise$is$quite$hard.$
2 Python's)Builtૐ퀐in)Types)
2.1 Ranges)
(
Exercise(2.1:(Trying(Ranges(
The!list()!function!creates!a!list!from!a!range.!Use!this!to!experiment!with!ranges,!
checking!that!you!understand!how!they!work.!For!example:!
range(10)
range(10, 20)
range(10, 40, 2)
Here!is!an!example:!
>>> r = range(10)
>>> list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]