Introduction to Python: Day Three

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Python
Group practice in problem design and problem solving
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Files Tutor: You will need ….
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Let’s get started!.
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])
Programming Constructs Notes Software Design & Development: Computational Constructs, Data Types & Structures, Algorithm Specification.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Lakshit Dhanda. Output Formatting Python has ways to convert any value to a string. 2 Methods repr() – meant to generate representations of values read.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Introduction to Computing Science and Programming I
Topic: File Input/Output (I/O)
Algorithmic complexity: Speed of algorithms
Python Let’s get started!.
Introduction to Python
Introduction to Python
Computer Programming Fundamentals
File Writing Upsorn Praphamontripong CS 1110
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python: Control Structures
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Python’s input and output
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
Functions CIS 40 – Introduction to Programming in Python
Engineering Innovation Center
Exceptions and files Taken from notes by Dr. Neil Moore
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Spring 2018
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
File IO and Strings CIS 40 – Introduction to Programming in Python
Loop Structures and Booleans Zelle - Chapter 8
Exceptions and files Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithmic complexity: Speed of algorithms
Text / Serial / Sequential Files
Introduction to Python: Day Two
G. Pullaiah College of Engineering and Technology
Introduction to Python: Day one
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Introduction to File Input and Output
Algorithmic complexity: Speed of algorithms
CSE 231 Lab 5.
Topics Introduction to File Input and Output
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Class code for pythonroom.com cchsp2cs
Python Simple file reading
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
def-ining a function A function as an execution control structure
Functions, Procedures, and Abstraction
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

Introduction to Python: Day Three Stephanie J. Spielman Big data in biology summer school, 2018 Center for computational biology and bioinformatics University of Texas at austin

Recap: we have learned… Working with several variable types Integer, float, boolean, string, list, dictionary Control flow with if and for Today: Writing and testing functions File input/output

exercise break

functions in python We’ve used several built-in functions: len(), sum(), etc. We can *write our own* functions too Reusability Modular design and organization Readability Debugging!!

writing custom functions Reference example: my_list = [1, 2, 3, 4, 5, 6] a = len(my_list) # here, a = 6 a: the returned value len: the function name my_list: the argument to the function

writing custom functions # Anatomy of a function definition def function_name(...arguments...): ... ... Python code return returned_value

so how can we re-write the len() function? # Example function construction def my_len(item): # Loop over item to count its size j = 0 for entry in item: j += 1 # Return the size return j # Now we can use the function! my_list = [1,2,3,4,5,6] b = my_len(my_list) print(b) 6 print(j) NameError: name 'j' is not defined Function names should be meaningful Arguments are arbitrary variable names Variables defined/used in the function exist only in the function

functions are generic formulas def triangle_area(l, w): area = (l*w) / 2.0 return area # Usage 1 area = triangle_area(7, 6) # Usage 2 length = 7 width = 6 area = triangle_area(length, width) # Usage 3 l = 7 w = 6 area = triangle_area(l, w)

use test cases to ensure your function works After writing a function, *always* test it with input that you know should work def triangle_area(l, w): area = l*w/ 2.0 return area # Before using the function all over the place, make sure that l=7, w=6 prints 21 print(triangle_area(7,6)) 21

Testing, but fancier def triangle_area(l, w): area = l*w/ 2.0 return area # Unit test code truth = 21 l = 7 w = 6 assert( triangle_area(l, w) == truth), "Triangle fail." ## If assertion FAILS, prints statement and script immediately exits

a note on scope Scope: the portion of your code where a certain variable/function exists In Python, scope is basically top-to- bottom Punch-line: define functions at the *top* of your script!

functions don't need to return anything! def square_cube(x): square = x**2 cube = x**3 print(x, "squared is", square, "and", x, "cubed is", cube) # Simply call the function square_cube(3) 3 squared is 9 and 3 cubed is 27 # What if you try to save a returned value when none exists? a = square_cube(3) print(a) None

returning multiple values def square_cube(x): square = x**2 cube = x**3 return square, cube # separate values with a comma s, c = square_cube(5) print(s) 25 print(c) 125 answer = square_cube(5) print(answer[0]) print(answer[1])

exercise break

reading and writing files in python Python does not deal with files directly We interact with files via special variables, called handles Interact with files in 3 main modes: Read-only ("r") Write-only ("w") Append ("a")

opening files for reading # Name of file to open filename = "my_file_with_important_stuff.txt" # Open the file into a handle with the .open() function file_handle = open(filename, "r") # two arguments # Read the file contents with the .read() method file_contents = file_handle.read() # Close the file when done with the .close() method (!!!) file_handle.close() print(file_contents) Line 1 of file. Line 2 of file. Line 3 of file. ... The entire body of the file, as a single string!

looping over lines in a file filename = "my_file_with_important_stuff.txt" file_handle = open(filename, "r") file_contents = file_handle.read() file_handle.close() # We can convert file_contents to a list using .split() file_contents_list = file_contents.split("\n") # or \r print(file_contents_list) ["Line 1 of file.", "Line 2 of file.", "Line 3 of file.", ...] # "Better" option: use the .readlines() method file_lines = file_handle.readlines() print(file_lines) ["Line 1 of file.\n", "Line 2 of file.\n", "Line 3 of file.\n", ...]

For large files, avoid saving content to memory filename = "my_file_with_important_stuff.txt" file_handle = open(filename, "r") ## Loop over handle while the file is still open for line in file_handle: print(line) Line 1 of file. Line 2 of file. Line 3 of file. ... file_handle.close()

Looping more than once file_handle = open(filename, "r") ## Loop over handle while the file is still open for line in file_handle: print(line) Line 1 of file. Line 2 of file. Line 3 of file. ... ## Looping again yields NO OUTPUT ## To go again, use .seek(<line number>) file_handle.seek(0) file_handle.close()

opening files for writing # Name of file to open (or to create!) filename = "my_file_to_write_to.txt" # Open handle for writing with the .open() function file_handle = open(filename, "w") # note the mode! # Write to the file with the .write() method file_handle.write("Line 1 of the file.\n") file_handle.write("Line 2 of the file.\n") # Close the file when done with the .close() method (!!!) file_handle.close() CAUTION: writing to file overwrites the file, if it exists already.

Add to an existing file with append-mode filename = "my_file_to_append_to.txt" # Open handle for writing with the .open() function file_handle = open(filename, "a") # note the mode! # Write to the file with the .write() method # Append mode does NOT add a new line for you. file_handle.write("\nAdding this line to the file.") # Close the file when done with the .close() method (!!!) file_handle.close()

but stephanie, I'm really lazy! # Use open and close file_handle = open(filename, "r") #### do stuff with file_handle #### file_handle.close() # Use the with statement (no need for close!) with open(filename, "r") as file_handle: # do stuff with file_handle ## File automatically closes outside the with block

remember file paths!! filename = "my_file.txt" file_handle = open(filename, "r") IOError: [Errno 2] No such file or directory: 'my_file.txt' # Solution: include the full (or relative) path path = "/path/to/files/" file_handle = open(path + filename, "r") ## No error now

exercise break