KnowledgeBoat Logo
|
OPEN IN APP

Part II: AI — Chapter 3.3

Introduction to Operators

Class 9 - Exploring Robotics & AI



Multiple Choice Questions

Question 1

Which of the following Python operators is depicted in the given clip?

OperatorDescription
isIt returns true, if two variables point the same object and false otherwise.
is notIt returns false, if two variables point the same object and true otherwise.
  1. Not Operator
  2. Membership Operator
  3. Identity Operator
  4. In Operator

Answer

Identity Operator

Reason — Python provides two special operators viz. is and is not, which are referred to as the Identity Operators. These operators are used to check whether two values are located at the same memory location or not, and results in True or False accordingly.

Question 2

Which of the following is a valid arithmetic operator in Python?

  1. //
  2. ?
  3. <
  4. None of them

Answer

//

Reason — In Python, // is the floor division operator which is a valid arithmetic operator. The symbols ? is not an operator in Python and < is a comparison operator, not an arithmetic operator.

Question 3

In Python, which of the following symbol is used to perform floor division?

  1. /
  2. //
  3. ÷
  4. \

Answer

//

Reason — The floor division operator (//) performs the division but results in only the integer part of the quotient. For example, 32//6 results in 5.

Question 4

What will be the return data type of (m/n), where m and n are two integers?

  1. int
  2. float
  3. double
  4. All of them

Answer

float

Reason — The division operator (/) always returns the value in the float type (by default), irrespective of the data type that are being used during the operations. So, int / int will return a float.

Question 5

If a = 5, b = 3.5, then what will be the return data type for (a + b)?

  1. int
  2. double
  3. int or float
  4. float

Answer

float

Reason — When an integer value (int) is added to a float value (float), the resultant data type will always be float. So, 5 + 3.5 returns 8.5 which is of float type.

Question 6

Which of the following is a valid arithmetic operator in Python?

  1. //
  2. ?
  3. <
  4. None of them

Answer

//

Reason — In Python, // is the floor division operator which is a valid arithmetic operator. The symbols ? is not an operator in Python and < is a comparison operator, not an arithmetic operator.

Question 7

Which of the following is a comparison or relational operator in Python?

  1. =
  2. //
  3. ==
  4. None of them

Answer

==

Reason — In Python, '==' is the equality operator which is a comparison or relational operator. It returns True if both the operands are equal, otherwise False. The '=' sign is an assignment operator and '//' is the floor division operator.

Question 8

If a = 25; b = 5; c = 0 and c = a % b, what will be the value of c?

  1. 5.0
  2. 5
  3. 0
  4. 0.5

Answer

0

Reason — The modulus operator (%) is used to obtain the remainder when the first number is divided by the second. Here, 25 % 5 = 0 since 25 is exactly divisible by 5 leaving no remainder. As both operands are integers, the result is also an integer (0).

Question 9

What will be the value of m when the given statement is executed?

m=8; m**2;
  1. 88
  2. 64
  3. 16
  4. 0

Answer

64

Reason — The exponentiation operator (**) results in power raised to a base. Here, m**2 means 8 raised to the power 2, which equals 8 × 8 = 64.

Question 10

What will be the output of 100/10?

  1. 10
  2. 10.0
  3. 10.00
  4. None of them

Answer

10.0

Reason — The division operator (/) always returns the value in the float type (by default), irrespective of the data type of the operands. So, 100/10 returns 10.0 which is of float type.

Question 11

If x = 5; y = 10; z = 11 and c = x*y + z/2, then what will be the value stored in c?

  1. 55.0
  2. 55.5
  3. 55
  4. 50

Answer

55.5

Reason — Evaluating the expression: x*y = 5*10 = 50 (int) and z/2 = 11/2 = 5.5 (float, since division always returns float). So, c = 50 + 5.5 = 55.5.

Question 12

What will the output of the following Python statement?

print(4*2>2**4)
  1. True
  2. true
  3. False
  4. false

Answer

False

Reason — Evaluating the expression: 4*2 = 8 and 2**4 = 16. So, the statement becomes 8>16 which is False. In Python, Boolean values are written as True and False with capital first letter.

Fill in the blanks

Question 1

Fill in the blanks:

  1. In Python, the ............... mode is used to make a program file for the future purpose.
  2. The single line comment is written simply by beginning a line with a ............... character.
  3. By default, Python program file is saved in the system with the extension ...............
  4. ............... refers to as a set of variables, constants and operators used together to obtain a meaningful result.
  5. By default, the input( ) function accepts the data as a ...............
  6. The operators used to compare two or more statements, known as ............... operators.
  7. The ............... operator is used to find remainder when a number is divided by other.
  8. The ............... are referred to as the non-executable statements.

Answer

  1. In Python, the Script mode is used to make a program file for the future purpose.
  2. The single line comment is written simply by beginning a line with a hash (#) character.
  3. By default, Python program file is saved in the system with the extension .py
  4. Expression refers to as a set of variables, constants and operators used together to obtain a meaningful result.
  5. By default, the input( ) function accepts the data as a string
  6. The operators used to compare two or more statements, known as comparison (relational) operators.
  7. The modulus (%) operator is used to find remainder when a number is divided by other.
  8. The comments are referred to as the non-executable statements.

Write Python Expressions

Question 1

Write down the Python expressions for the following Arithmetic expressions:

Arithmetical ExpressionsPython Expressions
abc
ab - bc + ca
a2 + b2 - c2
2a(b + 3cd)

Answer

Arithmetical ExpressionsPython Expressions
abca*b*c
ab - bc + caa*b - b*c + c*a
a2 + b2 - c2a*a + b*b - c*c
2a(b + 3cd)2*a*(b + 3*c*d)

Predict Output

Question 1

The comparison operators predict the output in terms of True or False. Predict the result when the following expressions will be executed?

Given: a = 8; b = 7; c = 9

ExpressionResultExpressionResult
a < ba==b
b > cc <= b
c!=aa >= c
b <= cb!=a

Answer

ExpressionResultExpressionResult
a < bFalsea==bFalse
b > cFalsec <= bFalse
c!=aTruea >= cFalse
b <= cTrueb!=aTrue

Do as directed

Question 1

If m, n, p, q = 7.1, 4.4, 9, 5 then find the output of the following Python statements:

StatementOutput
print(p%q)
print(n%q)
print(m/q)
print(n*q)
print(q**2)

Answer

StatementOutput
print(p%q)4
print(n%q)4.4
print(m/q)1.42
print(n*q)22.0
print(q**2)25

Assertion and Reason based question

Question 1

Assertion (A): Comments are non-executable statements used in a program to understand the logic, especially when somebody else has developed it.

Reason (R): In Python, a comment statement starts with a # (hash) sign. It can be positioned either at the beginning, along with the statements or at the end of a line.

Based on the above discussion, choose an appropriate statement from the options given below:

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true and R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.
  5. Both A and R are false.

Answer

Both A and R are true and R is the correct explanation of A.

Reason — Comments are non-executable statements that help users understand the programming logic, especially when developed by someone else. In Python, comments start with a # (hash) sign and can be positioned at the beginning, along with statements, or at the end of a line. The Python interpreter ignores them while executing the code. Hence, R correctly explains A.

Application based question

Question 1

Operators are symbols to perform various arithmetical and logical operations. Further, the arithmetic operator comprises a number of operators such as addition, subtraction, modulus, division, etc. There are some Python expressions written on Interactive mode. It is as under:

(i) >>> 27%5
(ii) >>> 27*5
(iii) >>> 27//5
(iv) >>> 27/5

Now, choose the correct options from below when executed:

  1. 135, 5, 2, 5.4
  2. 2, 5, 135, 5.4
  3. 2, 135, 5, 5.4
  4. 5, 135, 2, 5.4

Answer

2, 135, 5, 5.4

Reason — Evaluating each expression:

(i) 27%5 = 2 (remainder when 27 is divided by 5)

(ii) 27*5 = 135 (multiplication)

(iii) 27//5 = 5 (floor division, only the integer part of the quotient)

(iv) 27/5 = 5.4 (division always returns float)

Python Programs

Question 1

Write a program in Python to input the length of a rod in mm. Display the length of rod in cm and meter.

Sample Input:
Length of rod in mm = 24568

Output:
2456.8 cm
24.568 meter

Solution
mm = int(input("Enter length of rod in mm:"))
cm = mm / 10
m = mm / 1000
print(cm, "cm")
print(m, "meter")
Output
Enter length of rod in mm:24568
2456.8 cm
24.568 meter

Question 2

Write a program in Python to input two numbers and swap them by using a third variable.

Sample Input:
a = 55
b = 71

After using the third variable:

Output:
a = 71
b = 55

Solution
a = int(input("Enter first number:"))
b = int(input("Enter second number:"))
c = a
a = b
b = c
print("After swapping:")
print("a =", a)
print("b =", b)
Output
Enter first number:55
Enter second number:71
After swapping:
a = 71
b = 55

Question 3

Write a program in Python to input three different numbers. Display the sum of first two numbers and product of last two numbers.

Sample Input:
First number = 45
Second number = 80
Third number = 72

Output:
Sum = 45 + 80 = 125
Product = 80 * 72 = 5760

Solution
a = int(input("Enter first number:"))
b = int(input("Enter second number:"))
c = int(input("Enter third number:"))
s = a + b
p = b * c
print("Sum =", a, "+", b, "=", s)
print("Product =", b, "*", c, "=", p)
Output
Enter first number:45
Enter second number:80
Enter third number:72
Sum = 45 + 80 = 125
Product = 80 * 72 = 5760

Question 4

Write a program in Python to input length and breadth of a rectangle. Display area and perimeter of the rectangle.

[Hint: Area of rectangle = length × breadth

Perimeter of rectangle = 2(length + breadth)]

Solution
l = float(input("Enter length of rectangle:"))
b = float(input("Enter breadth of rectangle:"))
area = l * b
perimeter = 2 * (l + b)
print("Area of rectangle =", area)
print("Perimeter of rectangle =", perimeter)
Output
Enter length of rectangle:9
Enter breadth of rectangle:5
Area of rectangle = 45.0
Perimeter of rectangle = 28.0

Question 5

Write a Program in Python to calculate the area of a triangle by taking base and height as inputs.

Area of triangle = 1⁄2 × base × height

Solution
b = float(input("Enter base of triangle:"))
h = float(input("Enter height of triangle:"))
area = (1/2) * b * h
print("Area of triangle =", area)
Output
Enter base of triangle:12
Enter height of triangle:6
Area of triangle = 36.0

Question 6

Write a program in Python to input time in seconds. Display the time in hours, minutes and seconds.

Sample Input:
Time in Seconds = 8730

Output:
2 hrs. 25 min. 30 sec.

[Hint: Use % and // operators]

Solution
t = int(input("Enter time in seconds:"))
hrs = t // 3600
rem = t % 3600
mins = rem // 60
secs = rem % 60
print(hrs, "hrs.", mins, "min.", secs, "sec.")
Output
Enter time in seconds:8730
2 hrs. 25 min. 30 sec.

Question 7

Write a program in Python to input three numbers. Now swap them in such a way that the:

(i) first number gets the value of first and second
(ii) second number gets the value of second and third
(iii) third number gets the value of third and first

Sample Input:
First number = 5
Second number = 12
Third number = 18

Output:
First number = 5 + 12 = 17
Second number = 12 + 18 = 30
Third number = 18 + 5 = 23

Solution
a = int(input("Enter first number:"))
b = int(input("Enter second number:"))
c = int(input("Enter third number:"))
n1 = a + b
n2 = b + c
n3 = c + a
print("After swapping:")
print("First number =", a, "+", b, "=", n1)
print("Second number =", b, "+", c, "=", n2)
print("Third number =", c, "+", a, "=", n3)
Output
Enter first number:5
Enter second number:12
Enter third number:18
After swapping:
First number = 5 + 12 = 17
Second number = 12 + 18 = 30
Third number = 18 + 5 = 23

Question 8

Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a program in Python to calculate:

(i) the interest for the first year
(ii) the interest for the second year
(iii) the amount after three years.

Take sum as an input from the user.

Sample Input:
Principal = ₹5000, Rate = 10%, Time = 3 yrs

Output:
Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605

Solution
p = float(input("Enter principal amount: "))
r = float(input("Enter rate of interest: "))

i1 = (p * r) / 100
a1 = p + i1

i2 = (a1 * r) / 100
a2 = a1 + i2

i3 = (a2 * r) / 100
a3 = a2 + i3

print("Interest for the first year: ₹", i1)
print("Interest for the second year: ₹", i2)
print("Interest for the third year: ₹", i3)
print("Amount after three years: ₹", a3)
Output
Enter principal amount: 5000
Enter rate of interest: 10
Interest for the first year: ₹ 500.0
Interest for the second year: ₹ 550.0
Interest for the third year: ₹ 605.0
Amount after three years: ₹ 6655.0

Write short answers

Question 1

What is an operator? Name the different types of operators used in Python.

Answer

An operator is a symbol or token that performs various mathematical or logical operations on operands. Python comprises a number of operators to perform various tasks.

The different types of operators used in Python are:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators

Question 2

What is meant by comparison operators? What are its types?

Answer

The comparison operators compare the values of two operands and determine the relationship between them. It always returns a Boolean value (i.e., True or False).

Python provides six types of comparison operators:

  1. Less than (<)
  2. Greater than (>)
  3. Less than or equal to (<=)
  4. Greater than or equal to (>=)
  5. Equal to (==)
  6. Not equal to (!=)

Question 3

What is meant by comment?

Answer

Comments are referred to as the non-executable statements which enable the user to understand the programming logic. They are ignored by the Python interpreter while executing the code. In Python, a comment statement starts with a # (hash) sign and can be positioned either at the beginning, along with the statements or at the end of a line.

Question 4

Name two ways to give comment in a Python code.

Answer

There are two ways to give comments in Python:

(a) Single Line Comment — written by beginning a line with a hash (#) character.

(b) Multiline Comment — when a comment statement using a hash (#) character is expressed in multiple lines.

Question 5

What is the purpose of giving comment in a program?

Answer

The purpose of giving comment in a program is to enable the user to understand the programming logic. Comments mention the purpose and action being taken in different steps, especially when somebody else has developed the program.

Question 6

Why do we need to use input() in a Python code? Explain.

Answer

The use of input statement ensures the interaction or communication with the user. Python language provides the input() function for reading data from the standard input device (i.e., keyboard). This system provides a user-friendly environment to input data values, and the user may have the option to change the data values during the next execution of the program. Without input(), values must be assigned within the code itself, making the program restricted to those specific values. By default, the input() function returns a String type value.

Syntax: variable = input(<"Message"><Prompt>)

Question 7

Define the following operators:

(a) Arithmetic Operator (b) Logical Operator (c) Identity Operator (d) Membership Operator

Answer

(a) Arithmetic Operator — The operators which are used to perform arithmetical calculations in a program are known as arithmetic operators. These include addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%) and exponentiation (**).

(b) Logical Operator — Python language provides three logical operators namely and, or and not. These operators yield True or False depending upon the outcome of different conditions.

(c) Identity Operator — Identity operators are the operators which are used to compare the memory location of two objects. Python provides two special operators viz. is and is not.

(d) Membership Operator — There are two operators viz. in and not in which are said to be the Membership Operators. These operators are used to test whether a variable or value is an element of a sequence (i.e., string, list, tuple or dictionary) or not.

Question 8(a)

Distinguish between '%' and '/' operator.

Answer

'%' operator'/' operator
The '%' operator gives the remainder when the left operand is divided by the right operand.The '/' operator divides the left operand by the right operand and always results in floating value.
Example: 7%2 results in 1.Example: 7/2 results in 3.5.

Question 8(b)

Distinguish between '*' operator and '**' operator

Answer

'*' operator'**' operator
The '*' operator finds the product of two or more values.The '**' operator results in power raised to a base.
Example: 5*3 results in 15.Example: 5**3 results in 125.

Question 8(c)

Distinguish between '//' operator and '/' operator

Answer

'//' operator'/' operator
The '//' operator performs division but results in only the integer part of the quotient.The '/' operator provides the complete result of the division including the digits after the decimal point.
It is called floor division or integer division operator.It is called floating point division operator.
Example: 19//6 results in 3.Example: 19/6 results in 3.166666666.

Question 8(d)

Distinguish between '=' and '==' operator

Answer

'=' operator'==' operator
The '=' sign is used as an assignment operator which assigns a value to a variable.The '==' is referred to as a comparison or relational operator.
Example: a = 10 assigns the value 10 to variable a.Example: a == 10 returns True if both operands are equal, otherwise False.

Question 9

Will 24.0/5 and 24.0//5 produce the same result? Justify your answer.

Answer

No, 24.0/5 and 24.0//5 will not produce the same result.

  • 24.0/5 results in 4.8 — The '/' operator is known as the floating point division operator. It provides the complete result of the division including the digits after the decimal point. Therefore, it returns a float value.

  • 24.0//5 results in 4.0 — The '//' operator is known as the floor division or integer division operator. It returns only the integer part of the quotient.

Thus, although both involve division by 5, the '/' operator gives the exact division (4.8), while '//' gives only the integer part of the quotient (4.0).

Question 10

How will you justify the following statements?

(a) 'sum=12' and 'sum==12' are different.

(b)
a = 12.65
b = 12.65

print(a *is* b) results in False

Answer

(a) 'sum=12' and 'sum==12' are different because:

  • sum=12 uses the assignment operator (=) which assigns the value 12 to the variable 'sum'. After this statement, the variable sum holds the value 12.

  • sum==12 uses the comparison operator (==) which compares the value of the variable 'sum' with 12. It returns True if sum is equal to 12, otherwise False.

(b) print(a *is* b) results in False because:

The is operator is an Identity Operator that checks whether two variables point to the same memory location (i.e., the same object), not whether their values are equal. Although both a and b have the same value (12.65), they are stored at different memory locations in memory when assigned separately as float values. Hence, the is operator returns False since they are different objects in memory, even though their values are identical.

PrevNext