Professional Documents
Culture Documents
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.
The program is expected to display double the value of number received and stored in
variable a.
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().
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.
30.8
<class 'float'>
30
<class 'int'>
Int(),float() and str()
icecream = '25'
brownie = '45'
# String concatenation
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.
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
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.
− Import os
− os.system(‘clear’)