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

lab7

CHm lab

Uploaded by

gadita5390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lab7

CHm lab

Uploaded by

gadita5390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ITI 1120

Lab # 7
More programming with lists

Arguably, this is the most important lab of this semester

1
Starting Lab 7
• Open a browser and log into Brightspace

• On the left hand side under Labs tab, find lab6 material
contained in lab7-students.pdf file

• Download that file to the Desktop.

2
Before starting, always make sure you
are running Python 3
This slide is applicable to all labs, exercises, assignments … etc

ALWAYS MAKE SURE FIRST that you are running Python 3

That is, when you click on IDLE (or start python any other way)
look at the first line that the Python shell displays. It should say
Python 3 (and then some extra digits)

If you do not know how to do this, read the material provided


with Lab 1. It explains it step by step

3
Programming Exercises (the most important lab)
The following exercises are easily the most important exercises in this
whole semester. Solving these problems (by yourself preferably) should
greatly increase your understanding of computational problem solving
and programming.

Do as many as possible (preferably all) of the following 13 programming


exercises from your textbook by Perkovic. 11 out of 13 are mandatory
for this lab (your choice which 11) -- see the slides to come.

Introduction to Computing Using Python: An Application Development


Focus, 2nd Edition, Ljubomir Perkovic

Sometimes the author uses a word “outputs”. By that he means “prints”


First recall from the next 4 slides, list (and few string) functions and
methods that you will need.
Introduction to Computing Using Python by Lj. Perkovic

List operators and functions >>> lst = [1, 2, 3]


>>> lstB = [0, 4]
>>> 4 in lst
Like strings, lists can be manipulated with False
>>> 4 not in lst
operators and functions True
>>> lst + lstB
[1, 2, 3, 0, 4]
>>> 2*lst
Usage Explanation [1, 2, 3, 1, 2, 3]
>>> lst[0]
x in lst x is an item of lst 1
x not in lst x is not an item of lst >>> lst[1]
2
lst + lstB Concatenation of lst and lstB >>> lst[-1]
3
lst*n, n*lst Concatenation of n copies of lst >>> len(lst)
3
lst[i] Item at index i of lst >>> min(lst)
len(lst) Number of items in lst 1
>>> max(lst)
min(lst) Minimum item in lst 3
>>> sum(lst)
max(lst) Maximum item in lst 6
>>> help(list
sum(lst) Sum of items in lst ...
Introduction to Computing Using Python by Lj. Perkovic

Lists methods
>>> lst = [1, 2, 3]
Usage Explanation >>> lst.append(7)
lst.append(item) adds item to the end of lst >>> lst.append(3)
>>> lst
lst.count(item) returns the number of times item [1, 2, 3, 7, 3]
occurs in lst >>> lst.count(3)
2
lst.index(item) Returns index of (first occurrence of) >>> lst.remove(2)
item in lst >>> lst
[1, 3, 7, 3]
lst.pop() Removes and returns the last item in lst >>> lst.reverse()
>>> lst
lst.remove(item) Removes (the first occurrence of) item [3, 7, 3, 1]
from lst >>> lst.index(3)
0
lst.reverse(item) Reverses the order of items in lst >>> lst.sort()
lst.sort(item) Sorts the items of lst in increasing >>> lst
[1, 3, 3, 7]
order >>> lst.remove(3)
>>> lst
Methods append(), remove(), reverse(), [1, 3, 7]
and sort() do not return any value; they, along >>> lst.pop()
7
with method pop(), modify list lst >>> lst
[1, 3]
Introduction to Computing Using Python by Lj Perkovic

String operators >>> 'Hello, World!'


'Hello, World!'
>>> s = 'rock'
>>> t = 'climbing'
>>> s == 'rock'
True
Usage Explanation >>> s != t
x in s x is a substring of s True
>>> s < t
x not in s x is not a substring of s False
>>> s > t
s + t Concatenation of s and t True
>>> s + t
s * n, n * s Concatenation of n copies of s 'rockclimbing'
s[i] Character at index i of s >>> s + ' ' + t
'rock climbing'
len(s) (function) Length of string s >>> 5 * s
'rockrockrockrockrock'
>>> 30 * '_'
'______________________________'
To view all operators, use the help() tool >>> 'o' in s
True
>> help(str) >>> 'o' in t
Help on class str in module builtins: False
>>> 'bi' in t
class str(object) True
| str(string[, encoding[, errors]]) -> str >>> len(t)
... 8
Introduction to Computing Using Python by Lj. Perkovic

String methods
Usage Explanation
s.capitalize() returns a copy of s with first character
capitalized
s.count(target) returns the number of occurences of
target in s
Strings are s.find(target) returns the index of the first
immutable; occurrence of target in s
none of the s.lower() returns lowercase copy of s
string methods s.replace(old, new) returns copy of s with every
modify string occurrence of old replaced with new
link
s.split(sep) returns list of substrings of s,
delimited by sep
s.strip() returns copy of s without leading and
trailing whitespace
s.upper() returns lowercase copy of s
Programming Exercises
rpmaximum
×

You might also like