5. SEQUENCES: STRINGS, LISTS AND FILES (PART II) Rocky K. C. Chang September 16, 2015 (Adapted from John Zelle’s slides)

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
Introduction to C Programming
COSC 120 Computer Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to Python
Introduction to C Programming
String Escape Sequences
Topics This week: File input and output Python Programming, 2/e 1.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Introducing Java.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Files Tutor: You will need ….
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
ECE Application Programming
Containers and Lists CIS 40 – Introduction to Programming in Python
Python: Sequences: Strings, Lists and files (Part II)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 13 Input/Output Files.
Topics Introduction to File Input and Output
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to Programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
CS190/295 Programming in Python for Life Sciences: Lecture 3
Topics Designing a Program Input, Processing, and Output
Topics Introduction to File Input and Output
Topics Designing a Program Input, Processing, and Output
Capitolo 1 – Introduction C++ Programming
Unit 3: Variables in Java
Introduction to Programming
Topics Introduction to File Input and Output
Introduction to C Programming
Presentation transcript:

5. SEQUENCES: STRINGS, LISTS AND FILES (PART II) Rocky K. C. Chang September 16, 2015 (Adapted from John Zelle’s slides)

Objectives To understand the concept of objects, an active data type. To understand how string objects and list objects works in Python. To understand basic file processing concepts and techniques for reading and writing text files in Python. To be able to understand and write programs that process textual information.

Native data types So far, we have learned some native data types in Python. These data types are “passive” in the sense that they are just data and they cannot compute. Some problems: We cannot easily use these data types to model real-life objects in our problem solving. E.g., a circle (object) needs three float data, and a student record (object) needs numbers and strings.

The concept of objects Languages that support object-oriented features provide object data types, such as strings and lists. Example: myName = “Rocky Chang” myGrades = ["A+", "A", "B+", "B"] The difference with the object data types? Each object contains data (which are generally more complex). Each object also has methods operated in the data. In a program, we could request an object to perform operation for us.

EXERCISE 5.1 o Create a string object, such as student = “your_first_name, your_last_name, your student ID”. o Invoke some methods on the object, such as student.split(), student.lower(), student.upper(), student.capitalize().

Rocky Chang Methods split() lower() upper() capitalize() Data … A string object student Your program

String methods For Python 3.0: str.capitalize() str.casefold() str.center(width[, fillchar]) str.count(sub[, start[, end]])... str.title() str.translate(map) str.upper() str.zfill(width)

EXERCISE 5.2 Implement your pseudo-code for exercise You may consider using the split() method.

Consider a solution to Exercise 5.2.

Lists are also objects. One of the methods is append(). What do these codes give us? squares = [] for x in range(1,10): squares.append(x*x) print(squares ) Other methods in sequence-operations. sequence-operations

Turning a list into a string s.join(list) : concatenate list into a string, using s as a separator. o aList = ["Rocky", "Chang"] o "Memory Dennis".join(aList) o "".join(aList) o s.join(str) : concatenate str into a string, using s as a separator. o "Memory Dennis".join("Rocky Chang") o "".join("Rocky Chang")

EXERCISE 5.3 Create a list of “A”, “B”, “C”, “D”. How do you use the join() method for lists to return “ABCD”?

EXERCISE 5.4 Modify the solution to Exercise 5.2 by using a list of strings and the methods append() and join().

Input/Output as String Manipulation Program study: Download dateconvert.py from the course website for converting a date in mm/dd/yyyy to month, day, and year strings. E.g., 05/24/2014 -> May 24, 2014 Pseudo-code: Input the date in mm/dd/yyyy format ( dateStr ). Split dateStr into month, day, and year strings. Convert the month string into a month number. Use the month number to lookup the month name. Create a new date string in the form “ Month Day, Year ”. Output the new date string.

Several things to note Use “/” in split(). Use int(), instead of eval(). int() can remove leading zeroes but not eval().

Using str() Converting a number to a string, e.g., str(100)  “100”. We now have a complete set of type conversion operations: FunctionMeaning float( )Convert expr to a floating point value int( )Convert expr to an integer value long( )Convert expr to a long integer value str( )Return a string representation of expr eval( )Evaluate string as an expression

String Formatting We have so far used some simple string operations to format the output. But it is difficult to format finer control with them. String object has a format method to perform more sophisticated formatting.

EXERCISE 5.5 Try o Assign rocky = 100, dennis = 200, memory = 300. o print("Rocky has ${0:0.2f} and Dennis has ${1:0.2f} and Memory has ${2:0.2f}".format(rocky, dennis, memory)) o print("Rocky has ${2:0.2f} and Dennis has ${1:0.2f} and Memory has ${0:0.2f}".format(rocky, dennis, memory)) o print("Rocky has ${3:0.2f} and Dennis has ${2:0.2f} and Memory has ${1:0.2f}".format(rocky, dennis, memory))

EXERCISE 5.6 Try o rocky = 100. o print("Rocky has ${0:0.2f}".format(rocky)) o print("Rocky has ${0:1.2f}".format(rocky)) o print("Rocky has ${0:5.2f}".format(rocky)) o print("Rocky has ${0:10.2f}".format(rocky))

EXERCISE 5.7 Try o import math o print("The value of pi is {0:0.4f}".format(math.pi)) o print("The value of pi is {0:0.20f}".format(math.pi)) o print("The value of pi is {0:0.20}".format(math.pi))

EXERCISE 5.8 Going back to our rounding mystery. Try o print("The value of {0} is {0:0.40f}".format(0.45)) o print("The value of {0} is {0:0.40f}".format(1.45)) o print("The value of {0} is {0:0.40f}".format(2.45)) o print("The value of {0} is {0:0.40f}".format(3.45))

The string format() method String formatting syntax:.format( ) E.g., "The value of pi is {0:0.4f}".format(math.pi) Curly braces( {} ) inside the template-string marks “slots” into which the provided values are inserted. { : }, e.g., {0:0.4f} index : indicate which parameter to insert here One form of specifiers is.. width : the number of spaces for printing the parameter precision : the number of decimal places f : will print out 0 to fill up the number of decimal places.

File (object) A file is a sequence of data that is stored in secondary memory (disk drive). Two types of files: text file and binary file A text file contains characters, structured as lines of text. A binary file a file formatted in a way that only a computer program can read. A text file usually contains more than one line of text. Lines of text are separated with a special character, the newline character.

EXERCISE 5.9 o Open notepad++ (you may need to install it yourself in Y drive) o Find a.py file and use notepad++ to open it. o Under “View/Show Symbols”, choose “Show All Characters” to see the newline and tab characters.

How to end a line? ( LF: Multics, Unix and Unix-like systems (GNU/Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS and others.MulticsUnixUnix-likeGNULinuxOS XFreeBSDAIXXenixBeOSAmigaRISC OS CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9 and OS-9CommodoreAcorn BBCZX SpectrumTRS-80Apple II familyMac OSversion 9OS-9 CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPCMicrosoft WindowsDECTOPS-10RT-11CP/M MP/MDOSMS-DOSPC DOSAtari TOSOS/2 Symbian OSPalm OSAmstrad CPC

EXERCISE 5.10 o Install and open binaryviewer in Y drive. o Open the same.py file used in the last exercise. o Compare the binary representation on the left and the text on the right.

File Processing Open a file in a secondary storage. Read / write the file. Save the file if write. Close the file.

Opening a file in a secondary storage If successful, a “file handler” will be returned. infile = open(“mbox.txt”, “r”) infile = open(“mbox.txt”, “w”) Source:

Reading/writing and saving a file Load (part of) the file into the main memory. Read the file from the memory. Write the file to the memory. Saving will write the file in the memory to a secondary storage.

EXERCISE 5.11 Try infile = open(“a_text_file_of_your_choice","r") data = infile.read() print(data) (You may need os.getcwd() and os.chdir() to find your files.)

EXERCISE 5.12 Try infile = open(“a_text_file_of_your_choice","r") for i in range(10): line = infile.readline() print(line[:-1]) o How do you print the lines in a single line?

EXERCISE 5.13 Try infile = open(“a_text_file_of_your_choice","r") for line in infile.readlines(): print(line) o Does the output look the same as the file’s?

Three file read methods.read() – returns the entire remaining contents of the file as a single (possibly large, multi-line) string..readline() – returns the next line of the file. This is all text up to and including the next newline character..readlines() – returns a list of the remaining lines in the file. Each list item is a single line including the newline characters.

EXERCISE 5.14 Try input_file = open(“an_existing_py_file", "r") output_file = open(“clone.py", "w") content = input_file.read() output_file.write(content) output_file.close() Find the new clone.py file.

EXERCISE 5.15 Try input_file = open("an_existing_py_file", "r") output_file = open("another_existing_py_file", "a") content = input_file.read() output_file.write(content) output_file.close() Open the output file.

Write and append methods Opening a file for writing prepares the file to receive data. If you open an existing file for writing, you wipe out the file ’ s contents. If the named file does not exist, a new one is created. It is important to close a file that is written to, otherwise the tail end of the file may not be written to the file.

EXERCISE 5.16 o Download userfile.py from the course website. o Prepare an input file and use userfile.py to generate batch usernames.

EXERCISE 5.17 o Modify userfile.py by replacing print(uname, file=outfile) with write(). Do you observe any difference in the output file? o How do you slightly modify the codes to generate the same outputs?

END