Maha Khan 24
Maha Khan 24
LAB # 02
Objective:
Get familiar with operators and input function in Python Programming.
Theory:
Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the operand.
For example:
>>> 2+3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the
output of the operation.
Python Operator
Command Name Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division b/a
** Exponent(Performs a**b
exponential(power)
calculation on operators)
1
Programming Language (CT-153) Practical Workbook
Example#01:
INPUT in Python:
How the input() Function Works:
The input() function pauses your program and waits for the user to enter some text.
Once Python receives the user’s input, it stores it in a variable to make it convenient
for you to work with.
The purpose of an input statement is to get some information from the user of a
program and store it into a Variable.
<variable> = input (<prompt>) or
<variable> =datatype (input (<prompt>))
e.g x=int(input(“number:”))
Here prompt is an expression that serves to prompt the user for input; this is almost
always a string literal (i.e.,some text inside of quotation marks)
For example, the following program asks the user to enter some text, then displays
that message back to the user:
Not e
Example#03:
name = input("Please enter your name: ")
print("Hello, " + name + "!")
2
Programming Language (CT-153) Practical Workbook
Add a space at the end of your prompts (after the colon in the preceding example) to
separate the prompt from the user’s response and to make it clear to your user
where to enter their text.
Output:
Please enter your name: Najam
Hello, Najam!
Example#04:
Example#05:
3
Programming Language (CT-153) Practical Workbook Lab Exercise:
1. Write a script that take user input for a number then adds 3 to that number.
Then multiplies the result by 2, subtract 4, then again adds 3, then print the
result.
x= float(input (“Enter number:”))
a=x+3
b=a*2
c=b-4
d=c+3
print (“Result:”,d)
2. Write a script that takes input as radius then calculate area of circle. (hint: A=
πr²).
r= float (input (“Enter radius of circle:”))
A=3.142*r*r
1
Programming Language (CT-153) Practical Workbook
3. Write a Python program to take user input and then calculate the sum of three
given numbers.
a= float (input (“Enter number 1:”))
S=a+b+c
print (“Sum=”,S)
4. Write a script that print first and last name in reverse order with a space
between them. For e.g.
Input your First Name : Rahim
Input your Last Name : Khan
Hello Khan Rahim
fname= str (input("Enter your first name:"))