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