Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()

Similar presentations


Presentation on theme: "ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()"— Presentation transcript:

1 ENGINEERING 1D04 Tutorial 2

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

3 SPECIAL NOTE In the past, students have had the most trouble with string manipulation and file processing. Practice, practice, practice this for your labs!

4 Strings R epresent 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

5 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

6 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

7 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 [expr], where the index position of the character we would like to access is enclosed in []

8 String Indexing HIRACHEL 012345678 >>> 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

9 STRING FUNCTIONS Learning how to manipulate strings

10 Slicing Use this technique to access a continuous sequence of characters Format [ : ] 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

11 Concatenation and Repetition Combining two strings Use the + to add two words together Use the * to multiply it by itself

12 len() function This function will return the number of characters in the word Ex. len(“hello”) = 5

13 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

14 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)

15 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

16 FILE PROCESSING How to Read and Write from Files

17 Opening To read a file into python we use this format: = open(, ) So we create a variable to hold the file: filevar Then we “open” the file putting the name of the file in the 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()

18 Reading There are three methods to reading a file in Python: 1..read() : gives you the entire file as a string 2..readline() : gives you the next line of the file, this includes the next newline character 3..readlines(): gives you a list of the remaining lines in the file, each list item is a single line including the newline characters

19 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

20 DECISION MAKING If Statements

21 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

22 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

23 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


Download ppt "ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()"

Similar presentations


Ads by Google