Docsity

Python Programming: Understanding Loops and Conditional Statements in COMP1D04, Exercises of Mathematics

Toggle sidebar
bg1
COMP1D04: From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving
2.2
Loops
Conditionals
Reading Files
Grace Ngai
Much of this course and lots of the lecture notes were inspired by
or derived from Brown University’s CS931. Our thanks go to Prof
Shriram Krishnamurthi and Hammurabi Mendes for their kind
permission in allowing us to use their materials.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Python Programming: Understanding Loops and Conditional Statements in COMP1D04 and more Exercises Mathematics in PDF only on Docsity!

Loops

Conditionals

Reading Files

Grace Ngai

Much of this course and lots of the lecture notes were inspired by

or derived from Brown University’s CS931. Our thanks go to Prof

Shriram Krishnamurthi and Hammurabi Mendes for their kind

permission in allowing us to use their materials.

Textual Analysis

Define the problem Find the Data Write the Instructions Solution Computer Build a concordance of a text

  • Locations of words
  • Frequencies of words Word frequencies over time Author of texts Bias of authors (e.g. liberal media bias) Worldwide trends (e.g. oil vs. gold) …

Python

Reminder from last week

  • (^) myString.split()
  • (^) split() returns a list of words in a string
    • (^) Separated by whitespace
    • (^) Can also use a specified delimiter
  • (^) In programming: we call split() on the string variable myString
  • (^) split() is what we call a function (more on that later)

myString.split()

myString.split(“\n”)

string to “work on” Empty, or a substring that will act as a delimiter

Do Act 2-2, Task 1

Reading in a File

  • (^) Before we read in a file, we need to open it.
  • (^) The variable infile is called a filehandle. It represents

the file inside the Python program.

  • (^) The string "r" after the name of the file tells Python that

we are going to read the file.

  • (^) If the file does not exist, Python will give us an error. filename = “myFile.txt” infile = open(filename, “r") fileString = infile.read() infile.close()

Reading in a file

  • (^) The read() command tells Python to read in all the contents of infile at once. - (^) The file is read in as one big string of characters and stored in the variable (fileString in this example) - (^) Alphabetical symbols, numbers, spaces, newlines, ….
  • (^) The close() command does some “cleanup” after we’re done reading in the file. filename = “myFile.txt” infile = open(filename, “r") fileString = infile.read() infile.close()
  • Do Activity 2-2, Task

Review: Sequential Operation

  • (^) Last week, we learned how

Python runs programming

statements sequentially

  • (^) Programming statements

are executed one after the

other, in the order that

they appear in the

program.

  • (^) Each statement waits for

the previous one to finish

before starting.

x = 5 # assign x y = 6 # assign y z = x+y # assign z print(“x is”, x) print(“z is”, z) x = 13 # update x print(“x is now”, x) print(“z is still”, z) x is 5 z is 11 x is now 13 z is still 11

Repeating Things

  • (^) Python gives us an easy way of iterating over all the elements in a list: x = [“Hong”, “Kong”, “Polytechnic”, “University”] for item in x: print(“The word is”, item) The word is Hong The word is Kong The word is Polytechnic The word is University

Repeating Things

  • (^) Try the following in IDLE:
  • (^) What does the for … in … do? for i in [1, 2, 3, 4, 5]: print(i) for myVariable in [3, 7, 1, 2, 8, 2]: print(myVariable) for j in [2, 4, 6, 8, 10, 12]: print(j)

Example of Loop

x = [“Hong”, “Kong”, “Polytechnic”, “University”] num = 1 for word in x: print(“Word”, num, “is”, word) num = num + 1

Note that the loop body is indented

(preceded by a tab)

This indicates that these two program

statements are “inside” the loop.

Word 1 is Hong Word 2 is Kong Word 3 is Polytechnic Word 4 is University

x = [“Hong”, “Kong”, “Polytechnic”, “University”] num = 1 for word in x: print(“Word”, num, “is”, word) num = num + 1

Variable Value

x = [“Hong”, “Kong”, “Polytechnic”, “University”] num = 1 for word in x: print(“Word”, num, “is”, word) num = num + 1

Variable Value
x [“Hong”,^ “Kong”,^ “Polytechnic”,^ “University”]
num 1

x = [“Hong”, “Kong”, “Polytechnic”, “University”] num = 1 for word in x: print(“Word”, num, “is”, word) num = num + 1

Variable Value
x [“Hong”,^ “Kong”,^ “Polytechnic”,^ “University”]
num 1
word “Hong”