More Strings, File Processing and Methods and Functions - Notes | CSSE 120, Study notes of Software Engineering

Material Type: Notes; Class: Intro to Software Development; Subject: Computer Sci & Software Engr; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-del
koofers-user-del 🇺🇸

9 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MORE STRINGS,
FILE PROCESSING, AND
METHODS VS. FUNCTIONS
CSSE 120 Rose-Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download More Strings, File Processing and Methods and Functions - Notes | CSSE 120 and more Study notes Software Engineering in PDF only on Docsity!

MORE STRINGS,

FILE PROCESSING, AND

METHODS VS. FUNCTIONS

CSSE 120 – Rose-Hulman Institute of Technology

Outline

 Class exercise: processing with lists

 String representation

 More on string input and output

 File processing

 Coming attraction: Objects

 In-class practice time

The Software Development Process

Analyze the Problem

Determine Specifications

Create a Design

Implement the Design

Test/Debug the Program

Maintain the Program

Phases of Software Development

 Analyze: figure out exactly what the problem to be

solved is

 Specify: WHAT will program do? NOT HOW.

 Design: SKETCH how your program will do its work,

design the algorithm

 Implement : translate design to computer language

 Test/debug : See if it works as expected.

bug == error, debug == find and fix errors

 Maintain : continue developing in response to needs

of users

Consistent String Encodings

 Needed to share data between computers

 Examples:

 ASCII—American Standard Code for Info. Interchange  ―Ask-ee‖  Standard US keyboard characters plus ―control codes‖  Extended ASCII encodings  Add various international characters  Unicode  Tens of thousands of characters  Nearly every written language known

Class Exercise: Encoding

 Download encode.py from Angel:

 Lessons  Modules to Download in Class  Session 5  encode module

 Verify that the module works on your computer:

 Run the module  Enter the message: Spam  Enter the key: 1  Expected result: [84, 113, 98, 110]

 Answer In-class Quiz questions 4 and 5

String Formatting

 The % operator is overloaded

 Multiple meanings depending on context

 What does it mean for numbers?

 Other meaning for %

 Plug values from tuple into ―slots‖ in string  Slots given by format specifiers

Format Specifiers

 Syntax:

 %.

 Width gives total spaces to use

 0 means as many as needed  0 n means pad with leading 0s to n total spaces  - n means ―left justify‖ in the n spaces

 Precision gives digits after decimal point

 TypeChar is:

 f for float, s for string, or d for decimal (i.e., int)

File Writing in Python

 Open file:

 Syntax: = open(, )  Example: file_to_write = open('average.txt', ' w ')  Replaces contents!

 Write file:

 Syntax: .write()

 Close file:

 Syntax: .close()  Example: file_to_write.close()

File Reading in Python

 Open file: file_to_read = open('grades.txt', ' r ')

 Read file:

.read() Returns one BIG string  .readline() Returns next line, including \n  .readlines() Returns BIG list of strings, 1 per line  for in Iterates over lines efficiently

 Close file: file_to_read.close()

Up Next: Objects

 Why do we apply some operations like this:

 infile = open('file.txt','r')  abs(-1.2)

 and others like this:

 infile.read()  circle.draw(win)

 Files and circles are objects —data plus operations

.() is a method call

 Tells object to do something

Practice

 Hand in quiz

 Start working on HW

 Practice with string formatting  Practice with file processing

 On Angel

 Lessons  Homework  Homework 5  Homework 5 Instructions