Python programming : Standard Input and Output

6,924 views 16 slides May 09, 2019
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Python provides numerous built-in functions that are readily available to us at the Python prompt. Some of the functions like input() and print() are widely used for standard input and output operations respectively.


Slide Content

Standard Input & Output
Team Emertxe

Output Statements

Output Statements
Print()
print(), when called simply throws the cursor to the next line
Means, a blank line will be displayed

Output Statements
Print(“string”)
Example Output
print()
Prints the '\n' character
print("Hello") Hello
print('Hello') Hello
print("Hello \nWorld") Hello
World
print("Hello \tWorld") Hello World
print("Hello \\nWorld") Hello \nWorld
print(3 * 'Hello') HelloHelloHello
print("Hello"+"World") HelloWorld
print("Hello","World") Hello World

Output Statements
Print(variable list)
Example Output
a, b = 1, 2
print(a, b)
1 2
print(a, b, sep=",") 1,2
print(a, b, sep=':') 1:2
print(a, b, sep='---') 1---2
print("Hello", end="")
print("World")
HelloWorld
print("Hello", end="\t")
print("World")
Hello World

Output Statements
Print(object)
Example Output
lst = [10, 'A', "Hai"]
print(lst)
[10, 'A', 'Hai']
d = {10: "Ram", 20: "Amar"}
print(d)
{10: 'Ram', 20: 'Amar'}

Objects like list, tuples or dictionaries can be displayed

Output Statements
Print(“string”, variable list)
Example Output
a = 2
print(a, ": Even Number")
print("You typed", a, "as Input")
2 : Even Number
You typed 2 as Input

Output Statements
Print(formatted string)
Syntax: print("formatted string" % (varaible list))
Example Output
a = 10
print("The value of a: %i" % a)
The value of a: 10
a, b = 10, 20
print("a: %d\tb: %d" % (a, b))
a: 10 b: 20
name = "Ram"
print("Hai %s" % name)
print("Hai (%20s)" % name)
print("Hai (%-20s)" % name)
Hai Ram
Hai ( Ram)
Hai (Ram )
print("%c" % name[2]) m
print("%s" % name[0:2]) Ra
num = 123.345727
print("Num: %f" % num)
print("Num: %8.2f" % num)
Num: 123.345727
Num: 123.35

Output Statements
Print(formatted string)
Syntax: print("formatted string" % (varaible list))
Example Output
a, b, c = 1, 2, 3
print("First= {0}". format(a))
print("First= {0}, Second= {1}". format(a, b))
print("First= {one}, Second= {two}". format(one=a, two=b))
print("First= {}, Second= {}". format(a, b))
First= 1
First= 1, Second= 2
First= 1, Second= 2
First= 1, Second= 2
name, salary = "Ram", 123.45
print("Hello {0}, your salary: {1}". format(name, salary))
print("Hello {n}, your salary: {s}". format(n=name, s=salary))
print("Hello {:s}, your salary: {:.2f}". format(name, salary))
print("Hello %s, your salary: %.2f" % (name, salary))
Hello Ram, your salary: 123.45
Hello Ram, your salary: 123.45
Hello Ram, your salary: 123.45
Hello Ram, your salary: 123.45

Input Statements

Input Statements
Input()
Example
str = input()
print(str)
str = input("Enter the name: ")
print(str)
a = int(input("Enter the number: "))
print(a)
b = float(input("Enter the float number: "))
print(b)

Command Line Arguments

CLA
Example
1 #To display CLA
2
3 import sys
4
5 #Get the no. of CLA
6 n = len(sys.argv)
7
8 #Get the arguments
9 args = sys.argv
10
11 #Print the 'n'
12 print("No. Of CLA: ", n)
13
14 #print the arguments in one shot
15 print(args)
16
17 #Print the arguments one by one
18 for i in args:
19 print(i)

CLA
Parsing CLA
●argparse module is useful to develop user-friendly programs
●This module automatically generates help and usage messages
●May also display appropriate error messages

CLA
Parsing CLA: Steps
●Step-1: Import argparse module
import argparse
●Step-2: Create an Object of ArgumentParser
parser = argparse.ArgumentParser(description="This program displays square of two numbers")
●Step-2a: If programmer does not want to display description, then above step can
be skipped
parser = argparse.ArgumentParser()
●Step-3: Add the arguments to the parser
parser.add_argument("num", type=int, help="Enter only int number.")
●Step-4: Retrieve the arguments
args = parser.parse_args()
●Step-4: Retrieve the arguments
●Step-5: Access the arguments
args.num

THANK YOU