Input and output
statements
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.
Let us see the input section first.
2
Input statements
To allow flexibility, we might want to take the input from the
user. In Python, we have the input() function to allow this.
The syntax for input() is:
input([prompt])
where [prompt] is the string we wish to display on the
screen. It is optional.
3
● When input() function executes program flow will be stopped until
the user has given an input.
● The text or message display on the output screen to ask a user to
enter input value is optional i.e. the prompt, will be printed on the
screen is optional.
● Whatever you enter as input, input function convert it into a string. if
you enter an integer value still input() function convert it into a string.
4
How the input function works in Python :
5
Example 1
>>> num =
input('Enter a
number: ')
Enter a number: 10
>>> num
'10'
Example 2
x=input('enter your
name:')
enter your name:anu
>>> print(x)
anu
This same operation can be
performed using the eval()
function. But eval takes it further.
It can evaluate even expressions,
provided the input is a string.
Always use string as argument
for eval().
6
>>> int('2+3')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
int('2+3')
ValueError: invalid literal for int() with base
10: '2+3'
>>> eval('2+3')
5
eval() for input
Data Type conversions
The process of converting the value of one data type
(integer, string, float, etc.) to another data type is called
type conversion. Python has two types of type
conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion
7
In Implicit type conversion, Python automatically converts
one data type to another data type. This process doesn't
need any user involvement.
Python promotes the conversion of the lower data type
(integer) to the higher data type (float) to avoid data loss.
See the Example 1 given below:
8
Implicit Type Conversion
Example 1
Example 2
As we can see from the output, we got TypeError. Python is not able to use
Implicit Conversion in such conditions.
10
In Explicit Type Conversion, users convert the data type of
an object to required data type.
This type of conversion is also called typecasting because
the user casts (changes) the data type of the objects.
Casting in python is therefore done using constructor
functions:
11
Explicit Type Conversion
int() - constructs an integer
number from an integer literal, a
float literal (by rounding down
to the previous whole number),
or a string literal (providing the
string represents a whole
number)
12
int()
float() - constructs a float
number from an integer
literal, a float literal or a
string literal (providing the
string represents a float or
an integer)
13
float()
str() - constructs a string from a
wide variety of data types,
including strings, integer literals
and float literals.
14
str()
Key Points to Remember
1. Type Conversion is the conversion of object from one data type to another data typ
2. Implicit Type Conversion is automatically performed by the Python interpreter.
3. Python avoids the loss of data in Implicit Type Conversion.
4. Explicit Type Conversion is also called Type Casting, the data types of objects are
converted using predefined functions by the user.
5. In Type Casting, loss of data may occur as we enforce the object to a specific data
type.
15
16

Input and Output Statements.pdf

  • 1.
  • 2.
    Python provides numerousbuilt-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. Let us see the input section first. 2
  • 3.
    Input statements To allowflexibility, we might want to take the input from the user. In Python, we have the input() function to allow this. The syntax for input() is: input([prompt]) where [prompt] is the string we wish to display on the screen. It is optional. 3
  • 4.
    ● When input()function executes program flow will be stopped until the user has given an input. ● The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional. ● Whatever you enter as input, input function convert it into a string. if you enter an integer value still input() function convert it into a string. 4 How the input function works in Python :
  • 5.
    5 Example 1 >>> num= input('Enter a number: ') Enter a number: 10 >>> num '10' Example 2 x=input('enter your name:') enter your name:anu >>> print(x) anu
  • 6.
    This same operationcan be performed using the eval() function. But eval takes it further. It can evaluate even expressions, provided the input is a string. Always use string as argument for eval(). 6 >>> int('2+3') Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> int('2+3') ValueError: invalid literal for int() with base 10: '2+3' >>> eval('2+3') 5 eval() for input
  • 7.
    Data Type conversions Theprocess of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion. 1. Implicit Type Conversion 2. Explicit Type Conversion 7
  • 8.
    In Implicit typeconversion, Python automatically converts one data type to another data type. This process doesn't need any user involvement. Python promotes the conversion of the lower data type (integer) to the higher data type (float) to avoid data loss. See the Example 1 given below: 8 Implicit Type Conversion
  • 9.
  • 10.
    Example 2 As wecan see from the output, we got TypeError. Python is not able to use Implicit Conversion in such conditions. 10
  • 11.
    In Explicit TypeConversion, users convert the data type of an object to required data type. This type of conversion is also called typecasting because the user casts (changes) the data type of the objects. Casting in python is therefore done using constructor functions: 11 Explicit Type Conversion
  • 12.
    int() - constructsan integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number) 12 int()
  • 13.
    float() - constructsa float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) 13 float()
  • 14.
    str() - constructsa string from a wide variety of data types, including strings, integer literals and float literals. 14 str()
  • 15.
    Key Points toRemember 1. Type Conversion is the conversion of object from one data type to another data typ 2. Implicit Type Conversion is automatically performed by the Python interpreter. 3. Python avoids the loss of data in Implicit Type Conversion. 4. Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user. 5. In Type Casting, loss of data may occur as we enforce the object to a specific data type. 15
  • 16.