Kanel Nang.  Two methods of formatting output ◦ Standard string slicing and concatenation operations ◦ str.format() method ie. >>> a = “The sum of 1.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Python’s input and output Chenghao Wang. Fancier Output Formatting – Output method ▪Print() ▪Str() ▪Repr() Example S=“Hello World” Print(s) OR Print(“Hello.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Built-in Data Structures in Python An Introduction.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
CIT 590 Intro to Programming Files etc. Announcements From HW5 onwards (HW5, HW6,…) You can work alone. You can pick your own partner. You can also stick.
Python: Input and Output Yuen Kwan Lo. Output Format str( ) and repr( ) same representation but String and Floating point number a=0.24 str(a)‘0.24’ repr.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Python Crash Course File I/O Bachelors V1.0 dd Hour 2.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
 Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python.
Files Tutor: You will need ….
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
PC204 Lecture 5 Conrad Huang Genentech Hall, N453A
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
ASC, National Centre for Physics Programming Python – Lecture#2 Mr. Adeel-ur-Rehman.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Input and Output in python Josh DiCristo. How to output numbers str()  You can put any variable holding a number inside the parentheses and it will display.
Lecture 4 Python Basics Part 3.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
Lakshit Dhanda. Output Formatting Python has ways to convert any value to a string. 2 Methods repr() – meant to generate representations of values read.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
IIITD File Input / Output In Python. File and operations  File is a named location on disk to store related information  When we want to read from or.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Topic: File Input/Output (I/O)
Python Variable Types.
Using the Console.
06 File Objects and Directory Handling
Python’s input and output
Variables, Expressions, and IO
Binary Files.
Binary Files.
Lecture 13 Input/Output Files.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Using files Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Input and Output with FILES
Strings and Serialization
Files Handling In today’s lesson we will look at:
CS190/295 Programming in Python for Life Sciences: Lecture 3
Introduction to Value-Returning Functions: Generating Random Numbers
15-110: Principles of Computing
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Introduction to Computer Science
Topics Introduction to File Input and Output
Presentation transcript:

Kanel Nang

 Two methods of formatting output ◦ Standard string slicing and concatenation operations ◦ str.format() method ie. >>> a = “The sum of is {0}”.format(1+2) >>> print a The sum of is 3

 How do you convert value to string in Python? ◦ Functions  Repr()  Str() ◦ Differences  Str() function is meant to return representation of values that is human-readable  Repr() generate representations can be read by interpreter  If str() does have a proper human representation, then it will return the same value as repr()

 >>> s = ‘hello, world\n’  >>> hello = repr(s)  >>> print hello  ‘hello, world\n’  >>> hellos = str(s)  >>> print hellos  hello, world  >>> x = 10 * 3.25  >>> y = 200 * 200  >>> s = ‘The value of x is ‘ + repr(x) + ‘, and y is ‘ + repr(y) + ‘…’  >>> print s  The value of x is 32.5, and y is 40000…

 Useful methods of string objects ◦ rjust()  Right-justifies a string in a field of a given width by padding spaces on the left ◦ ljust()  Left-justifies a string in a field ◦ center()  Center-justifies a string in a field ◦ zfill() Pads a numeric string on the left with zeros

 >>> for x in range(1, 11): ...print repr(x).rjust(2), repr(x*x).rjust(3), ...# Note trailing comma on previous line ...print repr(x*x*x).rjust(4)          

 >>> ‘12’.zfill(5)  ‘00012’  >>> ‘-3.14’.zfill(7)  ‘ ’  >>> ’ ’.zfill(5)  ‘ ’

 Old string formatting ◦ The % operator is another method for string formatting. ◦ Similar to sprintf() in that it interprets the left argument to be applied to the right argument, then return the result of the string.  ie. >>> import math >>> print ‘The value of PI is approximately %5.3f.’ % math.pi The value of PI is approximately 3.142

 Open() ◦ Returns a file object and is most commonly used with two arguments  ie.  >>> open(filename, mode)  Modes ◦ ‘r’ – read only ◦ ‘w’ - writing only (overwrite existing files with same name) ◦ ‘a’ – opens file for appending (new data added to the end) ◦ ‘r+’ - opens the file in both reading and writing ◦ The mode argument is optional, ‘r’ will be assumed if it’s omitted

 Other modes ◦ On Windows  ‘b’ appended to the mode opens file in binary mode  ie.  ‘rb’, ‘wb’, and ‘r+b’ ◦ Python on windows will make a distinction between text and binary files, therefore the end-of-line characters in text files will be automatically altered slighted when data is read or written  Safe for ASCII text files, currupt binary data such as JPEG or EXE files

 Methods ◦.read(size) - reads some quantity of data and returns it as a string, if size is omitted, the entire content will be read and returned ◦.readline() - reads a single line from a file (\n should be left at the end of a string and should only be omitted on the last line ◦.readlines() - returns a list containing all the lines of a data file (size bytes can be included to avoid overloading memory) ◦.write() – writes the content of a string to the file ◦.tell() – returns an integer giving the file object’s current position in the file measured in bytes ◦.seek() - change the files object’s position ◦.close() – closes the file

 >>> F.read()  ‘This is the entre file.\n’  >>> F.readline()  ‘This is the firstline of the file.\n’  >>> F.readline()  ‘Second line of the file\n’  >>> F.readlines()  [‘This is the first line of the file.\n’, ‘Second line of the file\n’]  >>> F = open(‘/tmp/workfile’, ‘r+’)  >>> F.write(‘ abcdef’)  >>> F.seek(5) #Go to the 6 th byte in the file  >>> F.read(1)  ‘5’

 pickling ◦ A module that can take almost any Python object and convert it to a string representation.  ie.  You have object x and a file object f that’s open for writing  Pickle.dump(x, f)  unpickling ◦ Reconstructing the object from the string representation ◦ ie. ◦ f is a file object opened for reading ◦ X = pickle.load(f) The standard way to make Python objects which can be stored and reused by other programs

 html