With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Lists Introduction to Computing Science and Programming I.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Python November 14, Unit 7. Python Hello world, in class.
Computer Science 1620 Programming & Problem Solving.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Introduction to Python
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
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.
Input, Output, and Processing
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python Functions.
Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
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,
Decision Structures, String Comparison, Nested Structures
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
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.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
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.
Python Let’s get started!.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introduction to Python Lesson 2a Print and Types.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
C++ First Steps.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Formatting Output.
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Topics Introduction to File Input and Output
CHAPTER 3: String And Numeric Data In Python
Introduction to Computer Science
COMPUTER PROGRAMMING SKILLS
Primitive Types and Expressions
Unit 3: Variables in Java
Topics Introduction to File Input and Output
Class code for pythonroom.com cchsp2cs
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

With Python

 One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively simple.

 Opening and Closing: ◦ Files must always be opened before accessing, and closed when done, or else file errors will turn up. f = open(“data.txt”, “w+”) f.close() The First parameter is the filename. The second parameter is the ‘mode’

 r -> read only  w -> write only  a -> append  r+ -> reading and writing  w+ -> will create a file if it doesn’t exist  Append b to the end of any mode for binary reading/writing  Mode isn’t necessary, r will be assumed if it is not specified

 File functions: ◦ str = f.read()  Will read the entire file and return a string ◦ str = f.readline()  Will read the next line of the file and return a string ◦ f.write(str)  Will write the string str to the file ◦ f.seek(4)  Will seek 4 bytes further in the file (only works in binary modes) ◦ f.read(4)  Will read 4 bytes from the file (only works in binary modes)

 Reading from a file:  This will print out the entire file

 Reading from a file:  This will print out one line at a time.

 Writing to a file:  “\n” will write a new in the file.

 Escape characters are used to write certain characters that are illegal because they are used in the language. \tTab \nNew Line \rCarriage Return \”Quotes \bBackspace \\Backslash \’Apostrophe

With Python

 Functions are a quick way to make multiple use of lines of code without rewriting it. This function adds four to any passed value, and then returns the new value

 Functions have 4 main parts: Declaration Label Parameters Code

 Declaration: ◦ In Python, every function is declared using the “def” statement. ◦ This says that everything indented after this line is a part of this function

 Label ◦ The label is how you reference a function outside of the function. ◦ Functions can have the same names as variables but it is really bad practice to do so.

 Parameters ◦ Parameters are values that you pass into a function ◦ For Example:  I could call both addFour(3) and addFour(4) and I would get back 7 and 8 respectively ◦ You can have as many parameters as you want for any given function ◦ Variable numbers of parameters are also allowed

 Multiple parameters ◦ You can pass as many defined parameters as you like into a function. ◦ Multiple parameters are separated by a comma

 Variable Parameters ◦ When using variable parameters you use the * to signify an array. The remaining parameters are then stored into the specified array ◦ This function adds the passed numbers together

 Specified and Variable parameters ◦ You can combine the specified parameters and arrays to make a useful function ◦ This one finds averages of a certain amount of numbers

 Code ◦ The code in a function is what gets executed when you call the function. ◦ The return value in a function is the value that the function is set to after it has completed. ◦ The previous function x = addFour(x) will set x to x+4 on its return

 Recursion occurs when a function calls itself.  This is useful when you plan to scale down a parameter  Recursive functions must have a break condition that returns a set value Break values

 Python uses a bunch of built in functions such as: ◦ print  prints the parameters with a space in between ◦ len  returns the length of a string or array ◦ range  returns a range of numbers ◦ int  returns integer value of a number ◦ atoi  returns integer value of a string ◦ float  returns decimal value of a number ◦ Many File operations ◦ All the python included functions can be found at

In Python

 Global vs. Local variables ◦ Local variables are variables used inside of functions  They cannot be referenced outside of the function. ◦ Global variables can be used while the program is still running  They can be referenced across functions local global

 All parameters that are passed into a function are automatically local variables  Local variables are useful because it allows you to reuse variable names in different functions

 Global variables are any variables defined outside of a function  In Python, any variables in the main function are automatically global variables  Variables are defined as global or local when they are first called. if x is first called inside a function, it will give you an error when you try to call it in a global context.

ABCDEXYZABCDEXYZ  Local  Global  Local  Global

 Write a program that will generate a file containing the numbers 0 – 99 (“\n” creates a new line)  Write a program that uses a defined function to multiply an arbitrary amount of numbers together.  Write a program that will print the numbers 1 through 5 recursively  Write a program that will print the first alphabetical name from a file containing a list of names

 Write a program that will count the number of lines in a file and print the result to the screen.  Write a program that will convert an entire file to lowercase letters and print it to a new file.  Write a program that uses a function to convert mass and velocity to kinetic energy. (Hint: KE = ½ mv 2 )  Write a recursive function that will give you the number at the nth position of the Fibonacci sequence: ◦ (Hint: Fib(n) = Fib(n-1) + Fib(n-2))