- Information
- AI Chat
Was this document helpful?
Practical-sheet 3-v2 - Practical work
Course: Engineering
804 Documents
Students shared 804 documents in this course
University: National Institute of Technology Raipur
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]