Lecture 4: Basic I/O

Similar documents
Lecture 10: Strings. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Lecture 4: Simple Input-Calculate-Output Programs

Not-So-Mini-Lecture 6. Modules & Scripts

Getting Started Values, Expressions, and Statements CS GMU

Fundamentals of Programming (Python) Getting Started with Programming

Getting Started with Python

Introduction to Python

Lecture 2: Python Arithmetic

Table of Contents Date(s) Title/Topic Page #s. Abstraction

ENGR 101 Engineering Design Workshop

CS 115 Lecture 4. More Python; testing software. Neil Moore

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

When you open IDLE (Version 3.2.2) for the first time, you're presented with the following window:

Python Class-Lesson1 Instructor: Yao

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Lecture 4. Defining Functions

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

Lecture 10: Boolean Expressions

Chapter 2 Writing Simple Programs

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Full file at

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Lecture 3. Input, Output and Data Types

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

CS112 Lecture: Primitive Types, Operators, Strings

Chapter 2 Getting Started with Python

PYTHON. Values and Variables

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Lecture 2: SQL Basics

Algorithms and Programming I. Lecture#12 Spring 2015

Programming for Engineers in Python. Recitation 1

Variables, Constants, and Data Types

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Python Input, output and variables

Chapter 2: Data and Expressions

Python Unit

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

Programming in Python 3

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Introduction to Bioinformatics

Variable and Data Type I

Simple Lexical Analyzer

MEIN 50010: Python Introduction

Programming for Engineers in Python. Autumn

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Intro to Python & Programming. C-START Python PD Workshop

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Lecture 2: SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases. Brief Note on Naming Conventions. Our Running Example.

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Chapter 2: Data and Expressions

Programming to Python

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Variables, Expressions, and Statements

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Logical Thinking through Computer Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Chapter 2 C++ Fundamentals

Variable and Data Type I

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Scheme: Strings Scheme: I/O

Chapter 2: Data and Expressions

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

3 The Building Blocks: Data Types, Literals, and Variables

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Python Day 3 11/28/16

Lecture 7: Functions. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Visual C# Instructor s Manual Table of Contents

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

Objectives. In this chapter, you will:

A variable is a name for a location in memory A variable must be declared

The current topic: Python. Announcements. Python. Python

Lexical Analysis and jflex

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

My First Python Program

Getting started with Java

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lecture 1. basic Python programs, defining functions

COMP1730/COMP6730 Programming for Scientists. Strings

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Impera've Programming

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

CS1110 Lab 1 (Jan 27-28, 2015)

Transcription:

Lecture 4: Basic I/O CS1068+ Introductory Programming in Python Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 1 / 20

Summary Data type string. Basic I/O using print and input. Simple input-calculate-output programs. KH (21/09/17) Lecture 4: Basic I/O 2017-2018 2 / 20

Data type str String basics Strings are sequences of characters (letters, digits, symbols and so on) and are used to represent textual snippets (among other things) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 3 / 20

Data type str String basics Strings are sequences of characters (letters, digits, symbols and so on) and are used to represent textual snippets (among other things) Examples: "" "President" "Barack Obama" "1600 Pensylvania Avenue, Washington DC, USA" "+1 202-456-1111" "cs1068 is $#?%!" Double quotes delimit string (but are not part of it.) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 3 / 20

Data type str Printing strings Use print function: >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 4 / 20

Data type str Printing strings Use print function: >>>print("barack Obama") KH (21/09/17) Lecture 4: Basic I/O 2017-2018 4 / 20

Data type str Printing strings Use print function: >>>print("barack Obama") Barack Obama Note: Enclosing quotes not printed (not part of string) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 4 / 20

Data type str More on string literals We can also use single quotes to delimit strings Only 97 days to Christmas! We prefer doubles, but both are legal KH (21/09/17) Lecture 4: Basic I/O 2017-2018 5 / 20

Data type str More on string literals We can also use single quotes to delimit strings Only 97 days to Christmas! We prefer doubles, but both are legal We can also use treble quotes (single or double) for multi-line strings """ There was a young student from Cork You tried Python for a lark But as hard as be tried His brain it did fry And he struggled to come up to the mark! """ KH (21/09/17) Lecture 4: Basic I/O 2017-2018 5 / 20

Data type str Including tricky characters strings What if we need to represent a string that itself contains a double quote character? Escape the internal quote symbol by preceding it with backslash : >>>print("barack says, \"Python rocks!\"") Barack says, "Python rocks!" The two-character combination \ produces the single symbol Other awkward characters: \\ backslash \ single quote \ double quote \n newline \t tab KH (21/09/17) Lecture 4: Basic I/O 2017-2018 6 / 20

Data type str Basic string manipulation Concatenation Can join(concatenate) strings together using +: "Santa"+"Claus"! $\rightarrow$ \verb!"santaclaus" Repetition Can repeat strings together using so "Wibble! "*3@ $\rightarrow$ \verb@"wibble! Wibble! Wibble! "Santa says"+3*" ho"! $\rightarrow$ \verb!"santa says ho ho ho NB Operators + and valid with both integers and strings but with different meaning KH (21/09/17) Lecture 4: Basic I/O 2017-2018 7 / 20

Data type str Basic string conversions Need to be careful when working with different types >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 8 / 20

Data type str Basic string conversions Need to be careful when working with different types >>>x = "123" KH (21/09/17) Lecture 4: Basic I/O 2017-2018 8 / 20

Data type str Basic string conversions Need to be careful when working with different types >>>x = "123" >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 8 / 20

Data type str Basic string conversions Need to be careful when working with different types >>>x = "123" >>>y = x + 1 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 8 / 20

Data type str Basic string conversions Need to be careful when working with different types >>>x = "123" >>>y = x + 1 Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> x + 1 TypeError: Can t convert int object to str implicitly Use int function to explicitly convert string 123 into corresponding integer (123); use float for string to float conversions; string must be numerical error otherwise KH (21/09/17) Lecture 4: Basic I/O 2017-2018 8 / 20

Data type str String conversions con t >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Data type str String conversions con t >>>x = "123" KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Data type str String conversions con t >>>x = "123" >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Data type str String conversions con t >>>x = "123" >>>y = int(x) + 1 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Data type str String conversions con t >>>x = "123" >>>y = int(x) + 1 >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Data type str String conversions con t >>>x = "123" >>>y = int(x) + 1 >>>print(y) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Data type str String conversions con t >>>x = "123" >>>y = int(x) + 1 >>>print(y) 124 For int-to-string use str function i.e. str(123) is 123 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 9 / 20

Using the print statement Basic print capabilities >>>print(17) 17 >>>x = 23 >>>print(x) 23 >>>print("hello") Hello >>>print("x is ", x) x is 17 Single print can handle multiple items Each print generates newline once it s finished (by default) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 10 / 20

Using input statement Operation of input function Use input function to gather data from user >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 11 / 20

Using input statement Operation of input function Use input function to gather data from user >>>x = input("please type something: ") KH (21/09/17) Lecture 4: Basic I/O 2017-2018 11 / 20

Using input statement Operation of input function Use input function to gather data from user >>>x = input("please type something: ") Please type something: KH (21/09/17) Lecture 4: Basic I/O 2017-2018 11 / 20

Using input statement Operation of input function Use input function to gather data from user >>>x = input("please type something: ") Please type something: Wibble! >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 11 / 20

Using input statement Operation of input function Use input function to gather data from user >>>x = input("please type something: ") Please type something: Wibble! >>>print(x) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 11 / 20

Using input statement Operation of input function Use input function to gather data from user >>>x = input("please type something: ") Please type something: Wibble! >>>print(x) Wibble! The prompt ( Please... ) displayed to nudge user to type something (System pauses until user types something and hits Enter) entire line typed in by user is read in entire line (minus trailing newline) interpreted as a string name x is attached to this string KH (21/09/17) Lecture 4: Basic I/O 2017-2018 11 / 20

Using input statement Simple example 1 >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 1 >>>fav = input("what is your favourite colour? ") KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 1 >>>fav = input("what is your favourite colour? ") What is your favourite colour? KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 1 >>>fav = input("what is your favourite colour? ") What is your favourite colour? Grey >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 1 >>>fav = input("what is your favourite colour? ") What is your favourite colour? Grey >>>print("you typed ", fav) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 1 >>>fav = input("what is your favourite colour? ") What is your favourite colour? Grey >>>print("you typed ", fav) You typed Grey KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 1 >>>fav = input("what is your favourite colour? ") What is your favourite colour? Grey >>>print("you typed ", fav) You typed Grey Note: No check that user entered anything sensible KH (21/09/17) Lecture 4: Basic I/O 2017-2018 12 / 20

Using input statement Simple example 2 Enhanced version of temperature conversion code that asks users for Celsius temp First attempt: >>>temp c = input("please enter... ") KH (21/09/17) Lecture 4: Basic I/O 2017-2018 13 / 20

Using input statement Simple example 2 Enhanced version of temperature conversion code that asks users for Celsius temp First attempt: >>>temp c = input("please enter... ") Please enter temp. (Celsius) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 13 / 20

Using input statement Simple example 2 Enhanced version of temperature conversion code that asks users for Celsius temp First attempt: >>>temp c = input("please enter... ") Please enter temp. (Celsius) 17 >>>temp f = temp c * 9 / 5 + 32 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 13 / 20

Using input statement Diagnosis: temp c is a string KH (21/09/17) Lecture 4: Basic I/O 2017-2018 13 / 20 Simple example 2 Enhanced version of temperature conversion code that asks users for Celsius temp First attempt: >>>temp c = input("please enter... ") Please enter temp. (Celsius) 17 >>>temp f = temp c * 9 / 5 + 32 Traceback (most recent call last File "/home/kieran/work/teaching/python/code/examples/tempconv line 5, in <modul temp f = temp c * 9 /5 + 32 TypeError: unsupported operand type(s) for /: str and int

Using input statement Simple example 2 Corrected version >>>temp c = float(input("please enter...")) Please enter temp. (Celsius) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 14 / 20

Using input statement Simple example 2 Corrected version >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 >>>temp f = temp c * 9 / 5 + 32 >>>print("fahrenheit equivalent is ", temp f) Fahrenheit equivalent is 62.6 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 14 / 20

Using input statement Simple example 2 Corrected version slow motion >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 >>>temp f = temp c * 9 / 5 + 32 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 >>>temp f = temp c * 9 / 5 + 32 >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 >>>temp f = temp c * 9 / 5 + 32 >>>print("fahrenheit equivalent is ", temp f) KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using input statement Simple example 2 Corrected version slow motion >>>temp c = float(input("please enter...")) Please enter temp. (Celsius)17 >>>temp f = temp c * 9 / 5 + 32 >>>print("fahrenheit equivalent is ", temp f) Fahrenheit equivalent is 62.6 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 15 / 20

Using the IDLE program editor The program editor So far we have entered programs one line at at time Far more common (and convenient) to compose complete program in IDLE s program editor Can run programs ( Run menu option) from here Output appears in command shell KH (21/09/17) Lecture 4: Basic I/O 2017-2018 16 / 20

Using the IDLE program editor Executing programs from text editor Left: IDLE shell; Right: editor with temperature converter program KH (21/09/17) Lecture 4: Basic I/O 2017-2018 17 / 20

Using the IDLE program editor Once Run is clicked, program is translated and executed; any I/O appears in shell KH (21/09/17) Lecture 4: Basic I/O 2017-2018 18 / 20 Executing programs from text editor cont d

Using the IDLE program editor What the output looks like Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "copyright", "credits".... ================================================== >>> Enter temp (Celsius): 23 Equivalent in Fahrenheit is 73.4 >>> KH (21/09/17) Lecture 4: Basic I/O 2017-2018 19 / 20

Back Material Notes and Acknowledgements Reading Code Acknowledgements KH (21/09/17) Lecture 4: Basic I/O 2017-2018 20 / 20