ProgFund Lect Week 3
ProgFund Lect Week 3
Department of CS&IT
Formatted input/output
Why taking user inputs?
🞂 Developers often have a need to interact with
users, either to get data or to provide some
sort of result.
2
Formatted I/O
🞂 Formatted output converts the internal
binary representation of the data to ASCII
characters which are written to the output file.
🞂 Formatted input reads characters from the input
file and converts them to internal form.
🞂 Formatted input/output is very portable. It is
a simple process to move formatted data files
to various computers, even computers
running different operating systems, if they all use
the ASCII character set.
🞂 print() and input() are examples for
formatted input and output functions.
3
Reading Input From the Keyboard
🞂 Programs often need to obtain data from the
user, usually by way of input from the
keyboard.
🞂 The simplest way to accomplish this in Python
is with input().
input([<prompt>])
“Reads a line of input from the keyboard”
🞂 input() pauses program execution to allow the
user to type in a line of input from the keyboard.
Once the user presses the Enter key, all characters
typed are read and returned as a string.
4
Example#1:
🞂 name=input('What is your
name?') “name” is a variable
“input()” is a formatted function
('What is your name?') is a prompt or
passing string.
Output:
>>> %Run EX1.py
What is your name?XYZ
>>>
5
Example#2:how to display input value
#Description: Program to check
input #Taking input from user
num = input('Enter a number: ')
#Display input value using
Print() print("Input
number:“ ,num)
Output:
>>> %Run EX2.py
Enter a number:
2 Input number:
2
>>> 6
Typecasting in Python
🞂 input()always returns a string. If you want
a numeric type, then you need to convert
the string to the appropriate type with the
int(), float(), or complex() built-in
functions.This is called typecasting.
7
value = 5.6
int(value)
5
value = 5.6
round(value)
6
name=“programming"
print(name.upper())
name="programming "
print(name.lower())
name=" programming "
print(name.title())
string="15"
number=7
string_number=int(string)
sum=number+string_number
print("the sum is :", sum)
Ans:???
For Float Value precision
n=12.123456789
print ("value is: ", n)
print("upto 2 decimal places: % .2f" %n)
OR
You can use format()
Try it ur self
print("upto 2 decimal places:",
format(n,".2f"))
Example 3: Typecasting
🞂 n = input('Enter a number: ')
🞂 print(n +
100) Output:
🞂 Enter a
number: 50
🞂 Traceback
(most recent
call last):
🞂 File
"<stdin>", line
1, in <module>
🞂 TypeError:
must be str, not
int 1
1
Taking multiple inputs from user in Python
RQ 9
Split() method
🞂 This function helps in getting a multiple
inputs from user. It breaks the given input by
the specified separator.
Syntax :
input().split(separator)
# Description: Python program
showing how to multiple input using split
# Taking two inputs at a time
x, y = input("Enter a two
value: ").split() # Display inputs
print("Number of
boys: ", x) print("Number
of girls: ", y) 13
Displaying Output/Formatted output
🞂 The
print() function prints the
specified message to the screen.
17
Output Stream Keyword Arguments
print() accepts two additional keyword arguments, both of
which affect handling of the output stream:
🞂 file=<stream>:
By default, print() sends its output to a
default stream called sys.stdout, which is usually equivalent
to the console.The file=<stream> argument causes output to
be sent to an alternate stream designated by <stream>
instead.
21
Practice Question:
🞂 Write a program that reads a Celsius
degree from the console and converts it to
Fahrenheit and displays the result.The
formula for the conversion is as follows:
🞂 fahrenheit = (9 / 5) * celsius + 32
🞂 Here is a sample run of the
🞂 program: Enter a degree in
🞂 Celsius:43
43 Celsius is 109.4 Fahrenheit
22
Data Types
x = ["apple", "banana", "cherry"] // List
x = ("apple", "banana", "cherry") //Tuple
x = range(6) // Range
x = {"name" : "John", "age" : 36} // Dict