What type of value is returned by math.sqrt()?
- int
- float
- integer
- double
Answer
float
Reason — The math.sqrt() function always returns a float type value for a given argument, regardless of whether the input is an integer or a float. For example, math.sqrt(49) returns 7.0, not 7.
Which of the following syntax is true to find the square root of a number 'n'?
- sqrt(n)
- math.sqrt(n)
- Squareroot(n)
- n * n
Answer
math.sqrt(n)
Reason — The sqrt() function is defined in the math module, so it must be called as math.sqrt(n) after importing the math module.
Choose the correct statement to call Python math module:
- import.Math
- import.maths
- import.math
- import math
Answer
import math
Reason — The math module in Python is called using the keyword import followed by the module name math (which is case-sensitive and lowercase).
Give the output of math.sqrt(a), when a = 49.
- 7
- 7.0
- 7.00
- all
Answer
7.0
Reason — The math.sqrt() function always returns a float type value. Hence, the square root of 49 is returned as 7.0 (with a single decimal point), not 7 or 7.00.
Which of the following is not a decision-making statement in Python?
- if statement
- if-else statement
- if-else-if statement
- if-elif-else statement
Answer
if-else-if statement
Reason — In Python, the correct keyword used between if and else for multiple condition checks is elif, not else-if. Hence, if-else-if is not a valid decision-making construct in Python, the correct form is if-elif-else.
What symbol is used to represent "equal to" in Python's relational operator?
- ==
- =
- :=
- :=:
Answer
==
Reason — In Python, the == operator is the relational operator used to check whether two values are equal. The single = is the assignment operator, used to assign a value to a variable.
Which of the following is a Python valid statement?
if (num>=25):if (num>=25)if (num=>25)if (num=<25)
Answer
if (num>=25):
Reason — In Python, an if statement must end with a colon (:), and the correct relational operators for "greater than or equal to" and "less than or equal to" are >= and <= respectively, not => or =<.
An else statement must be preceded by ............... statement in Python.
- if
- elif
- if or elif
- all of them
Answer
if or elif
Reason — In Python, an else statement can be preceded by either an if statement (in if-else construct) or an elif statement (in if-elif-else construct). It cannot exist on its own without a preceding if or elif.
State whether the following statements are True or False:
- A conditional statement is essentially formed by using relational operators.
- An if-else construct only checks the first condition of the statement.
- The if statement is also called as a conditional statement.
- The 'Run Time' error occurs due to wrong logic.
- Python language provides some built-in functions that can be used in codes.
- The square root of a negative number can be determined by using math.sqrt( ) function.
Answer
- True
- False
Corrected Statement: An if-else construct checks a condition and executes one block of statements if the condition is true; otherwise, it executes another block of statements. - True
- False
Corrected Statement: The 'Run Time' error occurs due to mistakes that interrupt the correct execution of the program, such as division by zero or finding the square root of a negative number. Wrong logic results in a Logical Error, not a Run Time error. - True
- False
Corrected Statement: The square root of a negative number cannot be determined by usingmath.sqrt()function as it results in aValueError. The square root of a negative number is an imaginary number.
Predict the output of the following Mathematical functions:
| Mathematical Function | Output |
|---|---|
| math.sqrt(10.24) | |
| min(-25.5, -12.5) | |
| abs(-99.4) | |
| pow(10,-2) | |
| max(-25.5, -12.5) |
Answer
| Mathematical Function | Output | Explanation |
|---|---|---|
| math.sqrt(10.24) | 3.2 | The math.sqrt() function returns the square root as a float. The square root of 10.24 is 3.2. |
| min(-25.5, -12.5) | -25.5 | The min() function returns the smallest value. Among the two negative values, -25.5 is smaller (higher magnitude means lower value for negative numbers). |
| abs(-99.4) | 99.4 | The abs() function returns the absolute (magnitude) value of the number, which is always positive. |
| pow(10,-2) | 0.01 | The pow(a, b) function returns a raised to the power b. Here, 10-2 = 1/100 = 0.01 (float type). |
| max(-25.5, -12.5) | -25.5 | The max() function returns the largest value. Among the two negative values, -12.5 is greater than -25.5. |
Predict the output of the following snippet:
n = 25
if n < 15:
print("The number is less than 15")
if n <= 30:
print("The number is less than or equal to 30")
else:
print("Try Again!")Answer
The number is less than or equal to 30
The value of n is 25. In the first if statement, the condition n < 15 (i.e., 25 < 15) is False, so its block is skipped. In the second if statement, the condition n <= 30 (i.e., 25 <= 30) is True, so it prints "The number is less than or equal to 30". Since the condition is True, the else block associated with the second if statement is ignored.
Predict the output of the following snippet:
m, n, p = 11, 15, 18
if m > 0:
if n < 15:
print("Correct!")
elif p > 15:
print("OK")
else:
print("Better Next Time!")Answer
OK
In the statement m, n, p = 11, 15, 18, multiple variable assignment is used, where the values 11, 15, and 18 are assigned to m, n, and p respectively. The first condition m > 0 (i.e., 11 > 0) is True, so the control enters the outer if block. Then the condition n < 15 (i.e., 15 < 15) is checked, which is False. Therefore, the control moves to the elif condition p > 15 (i.e., 18 > 15), which is True, so the statement print("OK") gets executed and displays OK. Since the elif condition is True, the else block is skipped.
Assertion (A): In Python, there are some built-in functions which enhance the capability of processing and also reduce the complexity of programming logic.
Reason (R): These functions are defined in Python math module where, you need to define import math in the code to use them to produce the result.
Based on the above discussion, choose an appropriate statement from the options given below:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- Both A and R are false.
Answer
A is true but R is false.
Reason — Assertion (A) is correct because Python does provide built-in functions like max(), min(), pow(), abs(), etc., that enhance processing capability and reduce complexity. However, Reason (R) is false because not all built-in functions are defined in the math module - functions like max(), min(), pow(), and abs() are directly available without importing the math module. Only specific functions like sqrt(), ceil(), floor(), etc., require the math module to be imported.
Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:
............... function is used to return an absolute value (i.e., only magnitude of the number) as an int data type.
- value()
- absolute()
- abs()
- val()
Answer
abs()
Reason — The abs() function returns the magnitude (absolute value) of the given number. For an integer argument, it returns an int data type.
Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:
............... function is used to find the square root of a positive number.
- sqr()
- sqrt()
- root()
- sqroot()
Answer
sqrt()
Reason — The sqrt() function is used to find the square root of a positive number. In Python, it is used as math.sqrt().
Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:
What will be the return value for math.sqrt(-49)?
- 7.0
- 7
- ValueError
- Not known
Answer
ValueError
Reason — The square root of a negative number is an imaginary number. Therefore, math.sqrt(-49) results in a ValueError.
Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:
What will be the return value for max(-91.91, -91.9)?
- -91.90
- -91.9
- -91.19
- -91.00
Answer
-91.9
Reason — The max() function returns the largest value among the given arguments. Between -91.91 and -91.9, the value -91.9 is greater.
Write a program in Python to find and display the diagonal of a square taking side of the square as an input.
import math
s = int(input("Enter the side of the square: "))
d = s * math.sqrt(2)
print("Diagonal of the square:", d)Enter the side of the square: 10
Diagonal of the square: 14.142135623730951
Write a program in Python to calculate and display the hypotenuse of a Right-Angled Triangle by taking perpendicular and base as inputs.
[Hint: ]
import math
p = int(input("Enter the perpendicular: "))
b = int(input("Enter the base: "))
h = math.sqrt(p*p + b*b)
print("Hypotenuse of the right-angled triangle:", h)Enter the perpendicular: 3
Enter the base: 4
Hypotenuse of the right-angled triangle: 5.0
Write a program in Python to accept two numbers. If the two numbers are equal, then find the sum, otherwise find the product.
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a == b:
s = a + b
print("Sum of the two numbers:", s)
else:
p = a * b
print("Product of the two numbers:", p)Enter the first number: 11
Enter the second number: 11
Sum of the two numbers: 22
Enter the first number: 21
Enter the second number: 12
Product of the two numbers: 252
Write a Python program to input a number. Check whether the number is positive, negative or the number is zero. Display the message accordingly.
n = int(input("Enter a number: "))
if n > 0:
print("The number is positive")
elif n < 0:
print("The number is negative")
else:
print("The number is zero")Enter a number: -25
The number is negative
Write a program in Python to input year and check whether it is a leap year or not.
[Hint: if (Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0), said to be a leap year]
For example,
Input: 1600 Output: It is a leap year
Input: 2024 Output: It is a leap year
Input: 1900 Output: It is not a leap year
year = int(input("Enter a year: "))
if (year % 400 == 0) or ((year % 100 != 0) and (year % 4 == 0)):
print("It is a leap year")
else:
print("It is not a leap year")Enter a year: 1600
It is a leap year
Enter a year: 2024
It is a leap year
Enter a year: 1900
It is not a leap year
Write a Python program to enter the cost price and the selling price of an article. If the selling price (SP) is more than the cost price (CP) then calculate the profit and the profit percent. Otherwise, calculate the loss and the loss percent.
cp = float(input("Enter the cost price: "))
sp = float(input("Enter the selling price: "))
if sp > cp:
profit = sp - cp
profit_percent = (profit / cp) * 100
print("Profit:", profit)
print("Profit Percent:", profit_percent)
else:
loss = cp - sp
loss_percent = (loss / cp) * 100
print("Loss:", loss)
print("Loss Percent:", loss_percent)Enter the cost price: 500
Enter the selling price: 650
Profit: 150.0
Profit Percent: 30.0
Enter the cost price: 800
Enter the selling price: 600
Loss: 200.0
Loss Percent: 25.0
Write a program in Python to accept two angles and perform the following tasks:
(i) If the sum is equal to 90 the display "Complementary Angles"
(ii) If the sum is equal to 180 the display "Supplementary Angles"
(iii) Otherwise, display "Neither Complementary nor Supplementary Angles"
a = int(input("Enter the first angle: "))
b = int(input("Enter the second angle: "))
total = a + b
if total == 90:
print("Complementary Angles")
elif total == 180:
print("Supplementary Angles")
else:
print("Neither Complementary nor Supplementary Angles")Enter the first angle: 30
Enter the second angle: 60
Complementary Angles
Enter the first angle: 120
Enter the second angle: 60
Supplementary Angles
Enter the first angle: 40
Enter the second angle: 70
Neither Complementary nor Supplementary Angles
A shopkeeper offers 30% discount on purchasing an article whereas the other shopkeeper offers two successive discounts 20% and 10% for purchasing the same article. Write a program in Python to compute and display the discounts offered by both the shopkeepers. Take the price of the article as an input. Finally, display which is the better deal for the customer.
price = int(input("Enter price of the article:"))
# First shopkeeper
d1 = (30 / 100) * price
sp1 = price - d1
# Second shopkeeper
d2 = (20 / 100) * price
price_after_first = price - d2
d3 = (10 / 100) * price_after_first
total_discount = d2 + d3
sp2 = price - total_discount
print("Discount offered by first shopkeeper =", d1)
print("Discount offered by second shopkeeper =", total_discount)
if(d1 > total_discount):
print("First shopkeeper offers a better deal")
elif(total_discount > d1):
print("Second shopkeeper offers a better deal")
else:
print("Both offers are same")Enter price of the article:1000
Discount offered by first shopkeeper = 300.0
Discount offered by second shopkeeper = 280.0
First shopkeeper offers a better deal
A number is said to be a perfect square number, if its square root is an integer number. Write a Python program to input a positive number and check whether it is perfect square numbers or not.
[Note: The square root of a negative number cannot be determined.]
Sample Input: 81
Sample Output: A perfect square
import math
n = int(input("Enter a positive number: "))
if n < 0:
print("Square root of a negative number cannot be determined")
else:
sq = math.sqrt(n)
if sq == int(sq):
print("A perfect square")
else:
print("Not a perfect square")Enter a positive number: 81
A perfect square
A shopkeeper offers 30% discount on purchasing an article whereas the other shopkeeper offers two successive discounts 20% and 10% for purchasing the same article. Write a program in Python to compute and display the discounts offered by both the shopkeepers. Take the price of the article as an input. Finally, display which is the better deal for the customer.
price = int(input("Enter price of the article:"))
# First shopkeeper
d1 = (30 / 100) * price
sp1 = price - d1
# Second shopkeeper
d2 = (20 / 100) * price
price_after_first = price - d2
d3 = (10 / 100) * price_after_first
total_discount = d2 + d3
sp2 = price - total_discount
print("Discount offered by first shopkeeper =", d1)
print("Discount offered by second shopkeeper =", total_discount)
if(d1 > total_discount):
print("First shopkeeper offers a better deal")
elif(total_discount > d1):
print("Second shopkeeper offers a better deal")
else:
print("Both offers are same")Enter price of the article:1000
Discount offered by first shopkeeper = 300.0
Discount offered by second shopkeeper = 280.0
First shopkeeper offers a better deal
Write a Python program to input the name and basic salary of an employee. Calculate and display the name, gross salary and net salary of the employee when:
da = 30% of basic
hra = 18% of basic
pf = 12.5% of basic
gross = basic + da + hra
net = gross − pf
If the annual gross salary is up to ₹5,00,000 then display the message "Not an Income Tax Payer" otherwise, display the message "An Income Tax Payer".
name = input("Enter employee name:")
basic = int(input("Enter basic salary:"))
da = (30 / 100) * basic
hra = (18 / 100) * basic
pf = (12.5 / 100) * basic
gross = basic + da + hra
net = gross - pf
annual_gross = gross * 12
print("Name:", name)
print("Gross Salary =", gross)
print("Net Salary =", net)
if(annual_gross <= 500000):
print("Not an Income Tax Payer")
else:
print("An Income Tax Payer")Enter employee name:Rahul
Enter basic salary:30000
Name: Rahul
Gross Salary = 44400.0
Net Salary = 40650.0
An Income Tax Payer
Write a program that displays the results of the following evaluations based on the choice entered by the user:
- Absolute value of the number
- Square root of the number
- Cube root of the number
import math
num = int(input("Enter a number:"))
print("Enter 1 for Absolute value")
print("Enter 2 for Square root")
print("Enter 3 for Cube root")
choice = int(input("Enter your choice:"))
match choice:
case 1:
ab = abs(num)
print("Absolute value =", ab)
case 2:
if(num >= 0):
sq = math.sqrt(num)
print("Square root =", sq)
else:
print("Square root of a negative number cannot be determined")
case 3:
cb = pow(num, 1/3)
print("Cube root =", cb)
case _:
print("Invalid Choice!")Enter a number:27
Enter 1 for Absolute value
Enter 2 for Square root
Enter 3 for Cube root
Enter your choice:3
Cube root = 3.0
Enter a number:100
Enter 1 for Absolute value
Enter 2 for Square root
Enter 3 for Cube root
Enter your choice:2
Square root = 10.0
Enter a number:-153
Enter 1 for Absolute value
Enter 2 for Square root
Enter 3 for Cube root
Enter your choice:1
Absolute value = 153
What is meant by the term Indentation? Explain.
Answer
Indentation refers to the alignment of a statement or a group of statements which are part of another statement. In other words, it is the addition of white space before a statement to mark a particular block of code. In Python, all lines of code that belong to the same block must have the same indentation level. The standard indentation is four spaces, but any consistent number of spaces or tabs can be used. Python is very specific about indentation, improperly indented code will result in an error.
Name the different types of errors that occur in a code.
Answer
The three different types of errors that occur in a code are:
- Syntax error
- Logical error
- Run Time error
What is meant by normal flow of control?
Answer
The sequential movement of the flow of control from one statement to the other is termed as normal flow of control. In this flow, all the statements are executed one after the other (by default). The execution starts from 'Statement 1', moves to 'Statement 2', then 'Statement 3' and so on till the last statement of the program.
What do you understand by 'Conditional flow of control'?
Answer
When the execution of the statements is based on a given condition, the control flow is referred to as the Conditional flow of control. In this construct, the control transfers to a defined statement based on whether the specified condition is true or false, while ignoring some statements unexecuted. Python provides conditional statements like if, if-else, and if-elif-else to implement conditional flow of control.
Explain the following with their constructs:
(i) if-else
(ii) if-elif-else
Answer
(i) if-else statement
The if-else statement is used when one of the two actions is to be carried out depending on a condition. If the condition is true, the control executes one block of indented statements; otherwise, it executes another block of indented statements.
Construct:
if condition:
Statement(s) 1
else:
Statement(s) 2
(ii) if-elif-else statement
The if-elif-else is a conditional construct where multiple 'if-else' statements are connected with the help of the keyword elif. It allows the user to execute a statement or a block of statements when the given condition in the first if statement is true. When the first condition is false, it checks another condition using elif and takes the necessary action. At the end, the logical construct stops with the else statement.
Construct:
if condition1:
Statement 1
elif condition2:
Statement 2
else:
Statement 3
What type of error may occur when a number is divided by zero?
Answer
When a number is divided by zero, a Run Time error known as ZeroDivisionError occurs. This is because mathematically, a number cannot be divided by zero.
Distinguish between:
(i) '=' and '=='
(ii) Syntax Error and Logical Error
Answer
(i) '=' and '=='
| '=' | '==' |
|---|---|
| It is the assignment operator. | It is the relational (equality) operator. |
| It is used to assign a value to a variable. | It is used to compare two values to check if they are equal. |
Example: a = 10 (assigns 10 to a) | Example: a == 10 (returns True if a is equal to 10) |
(ii) Syntax Error and Logical Error
| Syntax Error | Logical Error |
|---|---|
| It occurs when the grammar of the programming statements is not followed properly. | It occurs when the program's logic is incorrectly planned. |
| The code does not execute until the error is corrected. | The code executes but does not produce the desired output. |
| Examples include incorrect punctuation, missing parenthesis, undefined terms. | Examples include wrong placement of operators, improper sequencing of statements, or incorrect hierarchy of operations. |
Define the following mathematical functions:
(i) max()
(ii) min()
(iii) pow()
(iv) sqrt()
Answer
(i) max() — This function is used to find the largest of the given arguments. It returns a value depending on the input arguments (i.e., if the arguments are int types then the return value will be an int).
Example: max(41, 89) returns 89
(ii) min() — This function returns the smallest of the given arguments. The return value will depend on the input values of the arguments of the function.
Example: min(49, 68) returns 49
(iii) pow() — This function is used to find the power b raised to a specified base a (i.e., ab). It returns data of type int or float, depending upon the output.
Example: pow(5, 3) returns 125
(iv) sqrt() — This function is used to find the square root of a number p for p > 0. It always returns a float type value. The math module must be imported to use this function.
Example: math.sqrt(81) returns 9.0