Python’s input and output

Slides:



Advertisements
Similar presentations
Python’s input and output Chenghao Wang. Fancier Output Formatting – Output method ▪Print() ▪Str() ▪Repr() Example S=“Hello World” Print(s) OR Print(“Hello.
Advertisements

Computer Science 111 Fundamentals of Programming I Files.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
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.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
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.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
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.
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.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
 Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
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])
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.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Lakshit Dhanda. Output Formatting Python has ways to convert any value to a string. 2 Methods repr() – meant to generate representations of values read.
Kanel Nang.  Two methods of formatting output ◦ Standard string slicing and concatenation operations ◦ str.format() method ie. >>> a = “The sum of 1.
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.
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()
CSc 120 Introduction to Computer Programing II Adapted from slides by
COMPSCI 107 Computer Science Fundamentals
SQL – Python and Databases
Topic: File Input/Output (I/O)
Fundamentals of Python: First Programs
Strings CSCI 112: Programming in C.
Lecture 4 Python Basics Part 3.
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 120 Introduction to Computer Programing II
File Access (7.5) CSE 2031 Fall July 2018.
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
Strings in Python Creating a string.
Introduction to Python
Class 9 Reading and writing to files chr, ord and Unicode
Lecture 13 Input/Output Files.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
Introduction To Files In Python
Fundamentals of Programming I Files
Lecture 4 Python Basics Part 3.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Lists in Python Outputting lists.
Introduction to Python: Day Three
Using Text Files in Python
Files Handling In today’s lesson we will look at:
Fundamentals of Python: First Programs
CS190/295 Programming in Python for Life Sciences: Lecture 3
functions: argument, return value
15-110: Principles of Computing
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Python Review
Topics Introduction to File Input and Output
Presentation transcript:

Python’s input and output Neev Wanvari Drexel University nsw26@drexel.edu

Output Python can either print data or output to a file for future use Handle String manually using string slicing and concatenation operations Use str.format() method

How to convert to string Use str() or repr() functions Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function Strings and floating point numbers, in particular, have two distinct representations

Example

Using string.format()

Old string formatting The % operator interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation

Reading and Writing Files open() returns a file object, and is most commonly used with two arguments: open(filename, mode). The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used

Different modes 'r‘- read 'w‘- write (an existing file with the same name will be erased) 'a‘- append; any data written to the file is automatically added to the end 'r+' read and write The mode argument is optional; 'r' will be assumed if it’s omitted

Methods of File Objects f.read(size) reads some quantity of data and returns it as a string size is an optional numeric argument When size is omitted or negative, the entire contents of the file will be read and returned If the end of the file has been reached, f.read() will return an empty string (" ")

Methods of File Objects f.readline() reads a single line from the file A newline character (\n) is left at the end of the string It is only omitted on the last line of the file if the file doesn’t end in a newline

Methods of File Objects f.readlines() returns a list containing all the lines of data in the file Only complete lines will be returned

For loop to read a file An alternative approach to reading lines is to loop over the file object. This is memory efficient, fast, and leads to simpler code

Methods of File Objects f.write(string) writes the contents of string to the file, returning None To write something other than a string, it needs to be converted to a string first

Thank you