You are on page 1of 12

input ()

input()
Accept values in expression forms for evaluation
print (‘enter a number’)
x=input("x=")
Output is:
Enter a number
x=5
If need to enter a string value then :
print (‘enter a name’)
name=input("name=")
Output is:
Enter a name
name=ABC
input Statement
>>> fname = input("Enter your first name:")
Enter your first name: Arnab
>>> age = input("Enter your age:")
Enter your age: 19
>>> type(age)
<class 'str'>

The variable fname will get the string ‘Arnab’, entered by the user.
Similarly, the variable age will get the string (‘19’).
We can typecast or change the datatype of the string data accepted from user to an
appropriate numeric value,
for example the following statement will convert the accepted string i.e. ‘19’ to integer
19 before assigning it to age.

>>> age = int( input("Enter your age:"))


Enter your age: 19
>>> type(age)
<class 'int'>
input Statement
a = input(“Enter a number and I’ll double it: ”)
a=a*2
print(a)

The program is expected to display double the value of number received and stored in
variable a.

Enter a number and I’ll double it: 2


22

This is because the value returned by the input function is a string ("2") by default.
As a result, the function does string concatenation resulting output as "22".

To get 4 as output, we need to convert the data type of the input entered by the user
through input().

a = input(“Enter a number and I’ll double it: ”)


a = int(a)
a=a*2
print(a)
Now, the program will display the expected output as follows:
Enter a number and I’ll double it: 2
4
Type conversion
As and when required, we can change the data type of a variable in Python from one type
to another.
Such data type conversion can happen in two ways:

either explicitly (forced) when the programmer specifies the interpreter to convert a data
type to another type,

or implicitly, when the interpreter understands such a need by itself and does it
automatically.
(new_data_type) (expression)

Function Description
int(x) Converts x to an integer.
float(x) Converts x to a floating-point number.
str(x) Converts x to a string representation.
chr(x) Converts x to a character.
unichr(x) Converts x to a Unicode character.
Type conversion
(new_data_type) (expression)

With explicit type conversion, there is a risk of loss of information since the
program is forcing an expression to be of that specific type.

For example, Explicit type conversion from float to int


a = 10.2
b = 20.6
c1 = (a + b)
print(c1)
print(type(c1))
c2 = int(a + b)
print(c2)
print(type(c2))

30.8
<class 'float'>
30
<class 'int'>
Int(),float() and str()

int() float() str()


For coverting string For coverting string to For converting number to string
to integer decimal number
x=`10` x=`10.55` x=10
X*2 gives `1010` x*2 gives `10.5510.55` We want to concatenate ‘length’ with
But we want to use But we want to use 10
values values If we write ‘length’+10, error will be
int(x) returns 10 generated
float(x) returns 10.55
y=int(x) cannot concatenate 'str' and 'int'
y*2 gives 20 y=float(x) objects
y*2 gives 21.1 Hence convert x to string using
str() and then concatenate i.e.
Write ‘length’+str(x) to get
‘length10’
Python Commands Output

icecream = '25'

brownie = '45'

# String concatenation

price = icecream + brownie Total Price

print("Total Price Rs." + price ) Rs.2545

# Explicit type conversion-string to integer

price= int(Icecream)+int(brownie) Total Price

print("Total Price Rs." + str(price) ) Rs.70


Implicit Conversion

Implicit conversion also known as coercion happens when data type conversion is
done automatically by Python and not instructed by the programmer.

Example
In the example, an int value a is added to a float value b, and the result was
automatically converted to a float value s without explicitly telling the system. This
is an example of implicit data conversion.

One may wonder why was the float value not converted to integer instead? This is
due to type promotion that allows performing operations (whenever possible) by
converting data into a wider sized data type without any loss of information.
Python Commands Output
# Implicit type conversion from int to float

a = 10 # a is an int

b = 20.0 # b is a float
30.0
s = a + b # s is sum of a float and an int
<class 'float'>
print(s)

print(type(s))
DEBUGGING
A programmer can make mistakes while writing a program, and due to mistakes,
the program may not execute or generate wrong output.

The process of identifying and removing such mistakes, also known as bugs/errors
from a program is called debugging.
Errors occurring in programs can be categorized as:
•syntax errors
•logical errors
•runtime errors

Syntax Errors
Like other programming languages, Python has its own rules that determine its
syntax.
The interpreter interprets the statements only if it is syntactically (as per the rules of
Python) correct.
If any syntax error is present, the interpreter shows error message(s) and stops
execution there.

For example, parentheses must be in pairs, so the expression (10 + 12) is


syntactically correct, whereas (7 + 11 is not due to absence of right parenthesis.
Such errors are removed and the program is rerun to start execution again.
Logical Errors

A logical error is a bug in the program that causes it to behave incorrectly.

A logical error produces an undesired output but without abrupt termination of the
execution of the program.
Since the program interprets successfully even when logical errors are present in it,
it is sometime difficult to identify these errors.

The only evidence to the existence of logical errors is the wrong output. Usually, it is
working backwards by looking at the output of the program and then trying to
identify what went wrong.

For example, if we wish to find the average of two numbers 10 and 12 and we write
the code as 10 + 12 /2, it would run successfully produce the result 16. Surely, 16 is
not the average of 10 and 12.

The correct code to find the average should have been (10 + 12) /2 to give the
correct output as 11.

Logical errors are also called semantic errors. The meaning of the program (its
semantics) is not correct.
Runtime Error

A runtime error causes abnormal termination of program while it is executing.


Runtime errors is when the statement is correct syntactically, but the interpreter can
not execute it.

Runtime errors do not appear until after the program starts running/executing.

For example, you may have a statement having division operation in the program.
By mistake, if the denominator entered is zero then it will give a runtime error like
“division by zero”. Runtime errors are also called exceptions.

To clear the output screen

− Import os
− os.system(‘clear’)

You might also like