0% found this document useful (0 votes)
32 views23 pages

ProgFund Lect Week 3

Uploaded by

Mohi Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views23 pages

ProgFund Lect Week 3

Uploaded by

Mohi Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Programming Fundamentals

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.

🞂 ​ Most programs today use a dialog box as a way


of asking the user to provide some type of
input.

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.

🞂 ​ Typecastingis when you convert a


variable value from one data type to
another.

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

🞂 ​ Developer often wants a user to


enter multiple values or inputs in one
line.

🞂 ​ In C++/C user can take multiple inputs in


one line using scanf() but in Python user can
take multiple values or inputs in one line by
the method called split() method.

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.

🞂​The message can be a string, or any


other object, the object will be
converted into a string before written
to the screen.
Syntax:
print(object(s), sep=separator, end=end,
file=file, flush=flush)
14
Print()
print() Parameters:
🞂 ​ objects - object to the printed. * indicates
that there may be more than one object
🞂 ​ sep - objects are separated by sep.
Default value: ' '
🞂 ​ end - end is printed at last
🞂 ​ file - must be an object with write(string)
method. If omitted it, sys.stdout will be
used which prints objects on the screen.
🞂 ​ flush - If True, the stream is forcibly
flushed. Default value: False
15
Example 4: using print()
🞂 ​ Printmore than one
object: print("Hello", "how
are you?")
🞂 ​ Three objects are passed:
a=4
b=a
pri
nt('
a
=',
a,
16
Example 4: using print()
🞂 ​ print()
with separator and end
parameters: a = 5
print("a =", a, sep='0000', end='\n\n')
print("a =", a, sep='0', end='')

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.

🞂 ​ flush=True: Ordinarily, print() buffers its output and only


writes to the output stream intermittently. flush=True
specifies that the output stream is forcibly flushed with each
print()
🞂 ​ These two keyword arguments are presented here for 18
Eval()
🞂 ​ The eval() method parses the
expression passed to this method and
runs python expression (code) within
the program.
🞂 ​ Syntax:
eval(expression, globals=None,
locals=None)
🞂​
Example
:x = 1
19
print(eval(
Example 5: Take three input as integers and
displays their average using eval()
🞂​ #Description: Prompt the user to enter
three numbers
number1 = eval(input("Enter the first number:
")) number2 = eval(input("Enter the second
number: "))
number3 = eval(input("Enter the third number:
"))
🞂 ​ # Compute average
average = (number1 + number2 + number3) / 3
🞂 ​ # Display result
print("The average of", number1,
number2, number3, "is",round(average))
RQ 17
Practice Question:
🞂 ​ Whathappens if the user enters 5a
when executing the following code?
radius = eval(input("Enter a radius:
"))

Answer written in words?

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

You might also like