Lab 4 - User Input and Arithmetic OperationsBESE-10
Lab 4 - User Input and Arithmetic OperationsBESE-10
Lab 4 - User Input and Arithmetic OperationsBESE-10
Introduction
This lab is designed to develop the understanding of students with python input and arithmetic operations.
Objectives
In this lab the students will learn and practice to attain the user input from keyboard and will perform basic
arithmetic operations on the obtained numbers.
Tools/Software Requirement
Python IDLE
>>> print(1 + 1)
2
Another built-in function that you will find useful is the input function. The input() function reads a line
entered on a console by an input device such as a keyboard and converts it into a string and returns it. You
can save the input string into a variable to use this input string further in your python code:
Example:
>>> species = input(“Enter a value: ”)
Homo sapiens
>>> species
'Homo sapiens'
See the snapshot below for a clear understanding on the usage of input() function:
There are no quotation marks with the output value above which indicates that it is int type.
Similarly, if your program requires the user to enter a float number you can use the following syntax:
>>> x= float(input(“Enter float value: ”))
57.8
>>> x
57.8
Arithmetic Operators
The Python interpreter can be used to express the arithmetic operators in a very simple way as described
below:
Lab Tasks:
1. For each of the following expressions, what value will the expression give? Verify your answers by
typing the expressions into Python. State your answers in the answer box provided below:
a. 9-3
b. 8 * 2.5
c. 9/2
d. 9 / -2
e. 9 // -2
f. 9%2
g. 9.0 % 2
h. 9 % 2.0
i. 9 % -2
j. -9 % 2
k. 9 / -2.0
l. 4+3*5
m. (4 + 3) * 5
2. Which of the following expressions result in Syntax Errors? Identify the Syntax Errors and type the
reason of error along with the correct code by mentioning its sequence number.
a. 6 * -----------8
b. 8 = people
c. ((((4 ** 3))))
d. (-(-(-(-5))))
e. 4 += 7 / 2
Answers:
a. The rabbit is 3.
b. The rabbit is 3 years old.
c. 12.5 is average.
d. 12.5 * 3
e. 12.5 * 3 is 37.5.
4. Write a program that prompts the user to input two values. Assign user defined values to two
variables and then swap their values. Print the swapped values to verify the results of your program.
5. Using only the techniques you have learned so far, write a program that calculates the square and
cube of the numbers from 0 to 10 and uses tabs to print the following table of values:
Enter number 1: 20
Enter number 2: 5
The sum is 25
The difference is 15
The quotient is 4
The modulus is 0
7. Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference
and area. Use the constant value 3.14159 for π(pi). Perform each of these calculations inside the
print statement(s).
8. Using only the techniques you have learned so far write a python program which allows the user
to print any multiplication table. An example is shown below:
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
Deliverables
Compile a single Word document by filling in the solution/answer part (as directed) along with the
snapshots. Name your submission file as given below and submit this Word file on LMS before the
deadline.