0% found this document useful (0 votes)
7 views22 pages

Chapter 4-Data Handling

Uploaded by

avd8793
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)
7 views22 pages

Chapter 4-Data Handling

Uploaded by

avd8793
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/ 22

Chapter 4:Data

Handling
Input and Output
 Sometimes, we need to enter data or enter choices into a program.
 In Python, we have the input() function for taking values entered by
input device such as a keyboard.
 The input() function prompts user to enter data. It accepts all user input
(whether alphabets, numbers or special character) as string.
 The syntax for input() is: variable = input([Prompt])
 Prompt is the string we may like to display on the screen prior to taking
the input, but it is optional.
 The input() takes exactly what is typed from the keyboard, converts it
into a string and assigns it to the variable on left hand side of the
assignment operator (=).
 Example 3.8
>>> fname = input("Enter your first name: ")
Enter your first name: Arnab
>>> age = input("Enter your age: ")
Enter your age: 19
 The variable fname gets the string ‘Arnab’ as input. Similarly, the
variable age gets '19' as string.
 We can change the datatype of the string data accepted from user to an
appropriate numeric value.
 For example, the int() function will convert the accepted string to an
integer. If the entered string is non-numeric, an error will be generated
 Example :
 #function int() to convert string to integer
 >>> age = int(input("Enter your age: "))
 Enter your age: 19
 >>> type(age)
 <class ‘int’>
 Python uses the print() function to output data to standard output
device — the screen. The function print() evaluates the expression
before displaying it on the screen.
 The syntax for print() is: print(value)
Debugging
 Due to errors, a program may not execute or may generate wrong
output. i) Syntax errors
ii) Logical errors
iii) Runtime errors
Syntax Errors:
 Like any programming language, Python has rules that determine how a
program is to be written. This is called syntax.
 The interpreter can interpret a statement of a program only if it is
syntactically correct. 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.
 If any syntax error is present, the interpreter shows error message(s)
and stops the execution there. Such errors need to be removed before
execution of the program.
 Logical Errors:
 A logical error/bug (called semantic error) does not stop execution but the
program behaves incorrectly and produces undesired /wrong output.
 Since the program interprets successfully even when logical errors are
present in it, it is sometimes difficult to identify these errors.
 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 and produce the
result 16, which is wrong. The correct code to find the average should
have been (10 + 12) /2 to get the output as 11.
 Runtime Error:
 A runtime error causes abnormal termination of program while it is
executing. Runtime error is when the statement is correct syntactically,
but the interpreter can not execute it.
 For example, we have a statement having division operation in the
program. By mistake, if the denominator value is zero then it will give a
runtime error like “division by zero”.
 The process of identifying and removing logical errors and runtime errors
is called debugging. We need to debug a program so that is can run
successfully and generate the desired output.
 Functions:
 A function refers to a set of statements or instructions grouped under a
name that perform specified tasks. For repeated or routine tasks, we
define a function.
 A function is defined once and can be reused at multiple Notes places in
a program by simply writing the function name, i.e., by calling that
function.
 Suppose we have a program which requires to calculate compound
interest at multiple places. Now instead of writing the formula to
calculate the interest every time, we can create a function called
CalcCompInt and inside that function we write the code to take inputs
(like interest rate, duration, principle), calculate interest, and display
output. We can simply call the function by writing the function name
CalcCompInt whenever compound interest is to be computed and thus
reuse the code to save time and efforts.
 Python has many predefined functions called built‑in functions. We have
already used two built-in functions print() and input().
 A module is a python file in which multiple functions are grouped
together. These functions can be easily used in a Python program by
importing the module using import command. Use of built-in functions
makes programming faster and efficient.
 To use a built‑in function we must know the following about that function:
 • Function Name — name of the function.
 • Arguments — While calling a function, we may pass value(s), called argument, enclosed in
parenthesis, to the function. The function works based on these values. A function may or
may not have argument(s).
 • Return Value − A function may or may not return one or more values. A function performs
operations on the basis of argument (s) passed to it and the result is passed back to the
calling point. Some functions do not return any value.
 Let us consider the following Python program using three built-in functions input(), int() and
print():
 #Calculate square of a number
num = int(input("Enter the first number"))
square = num * num
print("the square of", num, " is ", square)
Observe:
• Two built‑in functions are used in the first statement, int() and input(). The third line has a
function print().
• The input function accepts an argument, “Enter your name”. Argument(s) is the value(s)
passed within the parenthesis.
Similarly the print function has four arguments "the square of", num, "is", square separated by
commas.
• The int function in the first line takes as argument the value entered by the user from the
keyboard and converts it into a string and returns it. Thus the return value from the int() function
is an integer.
Some commonly used built-in
functions in Python:

You might also like