Python I/O.

Slides:



Advertisements
Similar presentations
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Advertisements

Pasewark & Pasewark 1 Access Lesson 6 Integrating Access Microsoft Office 2007: Introductory.
1 Access Lesson 6 Integrating Access Microsoft Office 2010 Introductory Pasewark & Pasewark.
The Computer Workstation DIGITAL LITERACY UNIT *Credit: Mr. Spinelli *
An Introduction to Textual Programming
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Lists in Python.
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Files Tutor: You will need ….
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Database (Microsoft Access). Database A database is an organized collection of related data about a specific topic or purpose. Examples of databases include:
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.
GCSE COMPUTER SCIENCE Practical Programming using Python
Introduction to Programming
Fundamentals of Python: First Programs
Introduction to Programming
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Chapter 1 Introduction to Java
CMPT 120 Topic:  Case Study.
Variables, Expressions, and IO
Basic operations in Matlab
Functions CIS 40 – Introduction to Programming in Python
Engineering Innovation Center
User Defined Functions
File Handling Programming Guides.
Java for Teachers Intermediate
File IO and Strings CIS 40 – Introduction to Programming in Python
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
File Handling.
Learning to Program in Python
Python Lessons 13 & 14 Mr. Kalmes.
Data Structures – 1D Lists
Fundamentals of Data Structures
ARRAYS 1 GCSE COMPUTER SCIENCE.
Lists in Python Outputting lists.
Class Examples.
Python programming exercise
Files Handling In today’s lesson we will look at:
Fundamentals of Python: First Programs
ARRAYS 2 GCSE COMPUTER SCIENCE.
Python Lists.
INC 161 , CPE 100 Computer Programming
Lets Play with arrays Singh Tripty
Functions continued.
Python Basics with Jupyter Notebook
To understand what arrays are and how to use them
Input and Output Python3 Beginner #3.
Introduction to Python
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Python 10 Mr. Husch.
Starter Activities GCSE Python.
Arrays & Loops.
Starter Which of these inventions is: Used most by people in Britain
Arrays & Loops.
Introduction to Computer Science
Python Creating a calculator.
Presentation transcript:

Python I/O

Array recap

Task 1 Create a new folder called Create a new text file Add the following details to your file, then save to the File IO folder – Superhero name, city of origin, real name, rating out of ten

Why? Python is able to access the information you have just stored in your text file Take a look at Task 2 in your A453 assessment. Why do you think we need to learn how to access information from a separate file for this task?

In Python… Type these two lines at the top of your code. What do you think these two lines will do? What do you think ‘csv’ stands for?

In Python… Type these two lines at the top of your code. Line 1 – Opens your ‘Superheroes’ text file inside Python Line 2 – Converts your text file into a format that Python can understand. Comma Separated Values What separates the values in our text file Name of original text file variable New variable name Csv.reader function

In Python… Type these lines at the top of your code. Line 1 – Creates an empty Names array Line 2 – Instructs python to iterate over every row inside the CSV file Line 3 – Adds the first row of the text file to the Names array 1 2 3

Tasks Add separate arrays for City of Origin, Real Name and Rating Edit your For loop to append these arrays with the relevant information from your CSV file Change the data type of your Rating array from strings to integers (look at your A453 Task 1 code for a hint…) 1 2 3

Add this to the bottom of your code

In Python Ask the user to type in which superhero they would like to search for If the name we type in (q) exists in our Name array, do this: Sets the index of Name array of what we typed in Prints out the name we searched for Prints out the relevant information from each individual array

Tasks Edit your code so that the user can search for a superheroes real name and have Python tell them the relevant information (Easy) Add to your code so that the user can choose whether to search by either superhero name, city of origin or real name, and print the relevant information (Medium) Tell the user what the average rating is of all the superheroes (Medium) Hint: use the ‘sum’ and ‘len’ commands

Input and Output We have learned to input data into Python from an external source (a text file) and sort through it to find the data we need The next thing we need to do is output this data in a way that is easy for the user to read and understand You will need to know how to do this for Task 2 and 3 in your A453 project, so listen and learn!

In Python… The first thing we need to do is create a new text file from within Python. This is where our information will be output This creates a new text file, Superhero Information, and stores it in Python under the variable name new_text_file The w+ operator at the end of the line means we can read and write to the file

Add this to the bottom of your code

1) Asks the user to input which superhero they want information about 2) If what we type in (writing) exists in the Name array, do this: 3) Set our array index to the location of the superhero we typed in 4) Writes the column headings ‘Name’ and ‘Real Name’ to the new text file 5) Creates a new line in our text file (like pressing Enter on the keyboard) 6) Writes a new line in our text file with the relevant variables from our array – Name and Real Name

Run Your Code Open the text file in your folder

Tasks Edit your code so that the program outputs “Superhero not found” if the user types in a superhero that doesn’t exist (Easy) Edit your code so that the user can search for a superhero. The program should output the Name, City, Real Name and Rating to a new text file (Medium) Edit your code so that the user can search for a superhero by their City of Origin. The program should output the Name, City, Real Name and Rating to a new text file (Medium) Edit your code so that the user can decide what the name of the outputted text file is (Medium)

Summary We can import data into Python from text files We do this by converting the text file into a CSV file so Python can understand it This is then stored into separate arrays which we can search and manipulate We can output data into separate text files We can select which data we output using user input and iteration