ACT2-2
. Download stringSplit.py
into the ACT2-2
folder.IDLE
, and then open stringSplit.py
.ACT2-2
window, press F5 (or select Run > Run Module
). What happens?"\n"
stands for? Add a print
statement into your code with the answer.split()
statements to try the following delimiters:split(",")
split("a")
split("out")
print
statement to explain what split
does, and what a delimiter is, in your own words.readFile.py
and tiger.txt
into your ACT2-2
folder.readFile.py
, we want to write a program that will read in William Blake's poem "The Tiger", the words of which are in tiger.txt.
fileName
.fileName
for reading and assigns the return value to inFile
. The value of inFile
now a filehandle that has been opened and can be read.fileString
the contents of the file, which are returned by calling the read
function on the file. fileString
is now a big string that contains the poem.myFile
; you don't need it any longer, and this will let Python clean up some of the memory space that you used.split
function to create a list of words from the string, separated by whitespace. Print that list to see what you get. Can you use this list to find the number of words in the file? Print out the number of words in the file. (Hint: Remember Activity 2-1, Task 1, Step 3f?)split
function again, but this time with the appropriate delimiter to get a list of sentences. Print the list to make sure that you are getting the correct thing. Print out the number of sentences in the file.File... New Window
and start a new program called testLoops.py
print(i*2)
, in the loop body, write a loop that will print out the numbers 2
, 6
, 10
, 14
, 18
, 20
(one number on each line).print(i)
, in the loop body, write a loop that will print out the numbers 3
, 8
, -1
, 35
, 22
, 0
(one number on each line).print(i)
, in the loop body, write a loop that will print out the numbers 8
, 8
, 8
, 8
, 8
(one number on each line).The factorial of 5 is 120
print("The factorial of 5 is", factorial)
factorial = 0
factorial = 1
for i in [1, 3, 5, 7, 9]
for i in [1, 2, 3, 4, 5]
for i in [2, 4, 3, 8, -1]
factorial = factorial + i
factorial = factorial * i
n
is calculated as n * (n-1) * (n-2) * (n-3) * ... *2 * 1
. Therefore, the factorial of 5 is calculated as 5*4*3*2*1 = 120readFile.py
program for this task.
len()
function (try doing a len("word")
in the IDLE interpreter if you forget how to use it for strings.)readFile.py
program for this task.
len()
function (try doing a len("word")
in the IDLE interpreter if you forget how to use it for strings.)