Download python io.operations and more Study notes Computer Science in PDF only on Docsity!
Lectures 11: IO Operations
Course Leader: Jishmi Jos Choondal Department of Computer Science and Engineering Faculty of Engineering and Technology M. S. Ramaiah University of Applied Sciences
Objectives
- (^) At the end of this lecture, student will be able to
- (^) Explain library functions and format used to perform input/output operations
- (^) Use strings for the terminal input and output of text
- (^) Construct a simple Python program that performs inputs, calculations, and outputs
Input From The User
- (^) Python has the input() built in function to display a prompt on screen, and then accept keyboard input, returning what was entered as a string to the code Note: Python takes all the input as a string input by default. To convert it to any other data type we have to convert the input explicitly.
res = input('What is your favorite programming language: ') What is your favorite programming language: Python res Python res = input('What is your favorite programming language: ') print(res) What is your favorite programming language: Python Python
Reading a Numeric Value
- (^) Python takes all the input as a string input by default. To convert it to any other data type we have to convert the input explicitly.
- (^) For example, to convert the input to int or float we have to use the int() and float() method respectively.
- (^) When reading a numeric value from the user
- programmer must use the input() function to get the string of characters
- use the int or float syntax to construct the numeric value that character string represents
year = int(input(“What is your age?” ))
Display on the Screen
Printing Multiple Items
- (^) To print multiple items, pass all the items in sequence separated by commas
- (^) They are automatically separated by commas print(“hello”,”world”) hello world print(“2+3=”,2+3) 2+3= 5 # there is a space after the = operator
Separating Items
- (^) To print multiple items but do not want space to be the separator, pass the separator string as an argument to print() print("hello","world",sep=',') hello,world print("hello","world",sep='<->') hello<->world print("welcome","to","python",sep='-') welcome-to-python
Terminating Items
- (^) By default, print() function prints a newline character ( \n ) after printing all the items
- (^) To control the termination of items, use the keyword end print("Is Python a dynamic language?",end="\nPython") Is Python a dynamic language? Python print("Is Python a dynamic language?\n",end="Python") Is Python a dynamic language? Python print("Is Python a dynamic language?\t",end="Python") Is Python a dynamic language? Python
Quotation Marks contd.
Double quotes
- (^) Any double quote character inside the string needs to be escaped by prefixing it with backslash
- (^) Example print(“Hello, World!”) print(“ ‘Hi’ from \”India\” “)
- (^) The output is Hello, World! ‘Hi’ from “India”
Formatting Strings Using format()
- (^) The format() function searches the string for placeholders
- (^) These placeholders are indicated by braces ( { } ) and indicate that some value needs to be substituted there
- (^) The format() takes any number of parameters
- (^) It is divided into two types of parameters:
- Positional parameters - list of parameters that can be accessed with index of parameter inside curly braces {index}
- Keyword parameters - list of parameters of type key=value, that can be accessed with key of parameter inside curly braces {key}
Keyword Arguments
- (^) Instead of just the parameters, a key-value for the parameters is used; name="Adam" and blc=230.
Formatting Strings - Examples
default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
positional arguments
print("Hello {0}, your balance is {1}.".format("Adam", 230.2346)) #keyword arguments print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
- (^) All the above statements displays the same output Hello Adam, your balance is 230.2346.
Summary
- (^) The print() function prints a list of items
- (^) The input() function prints an optional prompt message and reads in a single line of input
- (^) Output formatting can be done using format() method of String class