slides - McMaster Computing and Software

Download Report

Transcript slides - McMaster Computing and Software

ENGINEERING 1D04
Tutorial 2
What we’re doing today
•
More on Strings
•
String input
•
Strings as lists
•
String indexing
•
Slice
•
Concatenation and Repetition
•
len()
•
split()
•
Lists
•
ASCII
•
Opening a file
•
Reading a file
•
Writing to a file
•
If Statement
•
Practice
SPECIAL NOTE
In the past, students have had the most trouble with string manipulation
and file processing. Practice, practice, practice this for your labs!
Strings
•
Represent series of characters
• Must be enclosed by quotation marks, either “” or ‘’
• Doesn’t matter which kind but the same kind must be used for
start and end of the word
• Ie. “hello” and ‘hello’ not “hello’ nor ‘hello”
• In Python they will turn green in colour
String Input
•
How do we get input in the form of strings from user?
Input() function will not
work!! That is reserved
for number input
String Input
•
The correct form is raw_input()
•
This function does not immediately evaluate the expression that the user
enters
• This means that the user does not need to enter their input enclosed in
quotation marks
• For example: >>>favFruit = raw_input(“Please enter the name of your
favourite fruit: “)
Please enter the name of your favourite fruit: Pineapple
>>>Print “My favourite fruit is”, favFruit
My favourite fruit is Pineapple
Strings as Lists
•
In our earlier definition of strings, we said that they represent a series of
characters
•
In Python we can actually access different characters in the string!
•
How do we do this?
•
Through a process called indexing
•
Strings always begin with index position ZERO and are numbered in increasing
order from the left
•
The form is <string>[expr], where the index position of the character we would like
to access is enclosed in []
String Indexing
H
I
0
1
2
R
A
C
H
E
L
3
4
5
6
7
8
>>> welcome = HI RACHEL
>>> welcome[3]
‘R’
>>>print welcome[0]
‘H’
>>>print welcome[2-1]
‘I’
NOTE:Also possible to access strings from the right side using negative indexes
STRING FUNCTIONS
Learning how to manipulate strings
Slicing
•
Use this technique to access a continuous sequence of characters
•
Format <string>[<start>:<end>]
•
The substring will contain the letters beginning at index start and up to index
end…similar to the range function it DOES NOT include the character at the end
index
•
If you leave either start or end blank, it will take all of the characters up to the one
before end or all of the characters from the start to the rest of the word
•
Leaving it completely blank will show the entire string
Concatenation and Repetition
• Combining two strings
• Use the + to add two words together
• Use the * to multiply it by itself
len() function
• This function will return the number of characters
in the word
• Ex. len(“hello”) = 5
split()
•
Splits a string into substrings and puts them into a list
•
Leaving the () blank will split the string at spaces
•
Putting in ‘,’ will split at the commas
•
Putting in ‘\n’ will split at newlines
•
Putting in ‘\t’ will split at tabs
Lists
• Lists are similar to strings but do not need to contain only
characters
• They can be made up of numbers, strings or both
• One other difference is that lists are mutable (they can be
changed)
ASCII
Letters have corresponding numbers
based off of the ASCII table
The ord() function gives you the number
The chr() function converts the number to
the character
FILE PROCESSING
How to Read and Write from Files
Opening
•
To read a file into python we use this format: <filevar> = open(<name>,<mode>)
•
So we create a variable to hold the file: filevar
•
Then we “open” the file putting the name of the file in the <name> section
•
To read a file, we put ‘r’ in mode
•
Ie. infile = open(“test.txt”, “r”)
•
NOTE: After reading a file, you should close it using the close() function.
•
Ie. infile.close()
Reading
•
There are three methods to reading a file in Python:
1.
<filevar>.read() : gives you the entire file as a string
2.
<filevar>.readline() : gives you the next line of the file , this includes the next
newline character
3.
<filevar>.readlines(): gives you a list of the remaining lines in the file, each list
item is a single line including the newline characters
Writing
•
When you open a file for writing, it will erase all of the current files contents
•
If the file is yet to exist, it will create a new file
•
The format is the same as reading except you use ‘w’ instead of ‘r’
•
You can simply use write(“string”) to write items to the file
DECISION MAKING
If Statements
If Statement
•
One of the crucial decision making statements
•
Basically, if this happens do this
•
Takes the format: if a==b:
print “Equal”
else:
print “Not equal”
Or, for a additional comparisons you can use elif
Practice with String Methods and
Reading/Writing
Requirements:
•
Program asks the user to enter the name of a text file, F
•
Program computes: x – the number of lines in F, y – the number of words in F and z
– the number of characters in F(not counting newline characters)
•
Program writes x,y and z into a new file called count.txt
Practice for File Processing
•
Write a Python program that reads a sequence of numbers stored in a file,
represents the sequence of numbers as a list of floats, performs a calculation on
the members of the list, and then outputs the result.
Requirements:
1.
The program asks the user to enter a text file name, F, which stores a finite
sequence of numbers. You can assume the file contains one number per line.
2.
The program stores the numbers in the list and calculates the average of the
numbers.
3.
The program then writes the average to a file called average