0% found this document useful (0 votes)
34 views5 pages

Maha Khan 24

This document discusses operators and input functions in Python programming. It provides examples of common operators like addition, subtraction, multiplication, division, and exponentiation. It also demonstrates how to take user input in Python using the input() function and store it in a variable. Examples show prompting the user to enter their name and a number, then printing the results. The lab exercise asks the student to write scripts that perform calculations based on user-provided numeric input and strings for names.

Uploaded by

Maha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views5 pages

Maha Khan 24

This document discusses operators and input functions in Python programming. It provides examples of common operators like addition, subtraction, multiplication, division, and exponentiation. It also demonstrates how to take user input in Python using the input() function and store it in a variable. Examples show prompting the user to enter their name and a number, then printing the results. The lab exercise asks the student to write scripts that perform calculations based on user-provided numeric input and strings for names.

Uploaded by

Maha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Language (CT-153) Practical Workbook

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

% Module(Divides left b%a=0


hand operand by right
hand operand and
returns remainder)

** 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

Writing Clear Prompts:


Each time you use the input() function, you should include a clear, easy-tofollow
prompt that tells the user exactly what kind of information you’re looking for. Any
statement that tells the user what to enter should work.

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:

Using int() to Accept Numerical Input:


It takes the string value, then reads through it and converts it into a value the
computer "understands" as a whole number (i.e. one without decimal points and
fractions and such). The type of value it returns back is called an "integer" and for
short an "int".

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

print (“Area of circle:”,A)

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:”))

b= float (input (“Enter number 2:”))

c= float (input (“Enter number 3:”))

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:"))

lname= str (input("Enter your last name:"))

print ("Hello"+" "+lname+" "+fname)


OR
print(“Hello”,lname,fname)

You might also like