KnowledgeBoat Logo
OPEN IN APP

Chapter 3

Python Programming Fundamentals

Class 11 - Informatics Practices Preeti Arora



Fill in the Blanks

Question 1

The smallest individual unit in a program is known as a Token.

Question 2

A word having special meaning reserved by a programming language is known as Keyword.

Question 3

A String literal is a sequence of characters surrounded by quotes.

Question 4

Operators are tokens that trigger same computation/action when applied to variables or operands.

Question 5

A Variable is a reserved named location that stores values.

Question 6

To determine the type of an object, we use type() function.

Question 7

The input() function always returns a value of String type.

Question 8

Blocks of codes are represented through indentation.

Question 9

In dynamic typing, a variable can hold values of different types at different times.

Question 10

In Python, the floating-point numbers have precision of 16 digits.

Question 11

Operators that act upon two operands are referred to as binary operators.

Question 12

Floor division truncates fractional remainders and gives only the whole part as the result.

Question 13

The process of identifying and removing errors from a computer program is called debugging.

Question 14

A logical error does not stop execution but the program behaves incorrectly and produces undesired/wrong output.

Question 15

Trying to access a variable or file that doesn't exist is a kind of Run-time error.

State True or False

Question 1

Python supports Unicode coding standard.

Answer

True

Reason — Python supports Unicode coding standard.

Question 2

An identifier must be a keyword of Python.

Answer

False

Reason — An identifier cannot be a Python keyword. Keywords are reserved words with special meanings and cannot be used as identifiers.

Question 3

Integers and strings are immutable data types.

Answer

True

Reason — In Python, integers and strings are immutable data types, meaning their values cannot be changed after they are created.

Question 4

3+C = A is a valid statement.

Answer

False

Reason — In Python, assignment statements follow the syntax variable = value, where the variable (L-value) is on the left side and the value (R-value) is on the right side of the equal sign. Therefore, 3+C = A is an invalid statement in Python because the left side (3+C) is not a valid variable name.

Question 5

The input() method always returns a value of an integer type.

Answer

False

Reason — The input() method in Python returns the input from the user as a string, regardless of whether the user enters an integer, float, or any other type of input.

Question 6

The print() method without any value or name or expression prints a blank line.

Answer

True

Reason — When the print() function is called without any arguments, it outputs a newline character ('\n'), which results in printing a blank line.

Question 7

In Python, boolean type is a sub-type of integer.

Answer

False

Reason — In Python, the boolean data type is distinct and represents one of the two possible values — True or False.

Question 8

"AISSCE-2020" is a valid string variable in Python.

Answer

False

Reason — In Python, variable names cannot contain hyphens. They can only contain letters (both uppercase and lowercase), digits, and underscores, but they cannot start with a digit. Therefore, "AISSCE-2020" is invalid string variable name because it contains a hyphen.

Question 9

In Python, integer data type has a fractional part.

Answer

False

Reason — In Python, the float data type represents numbers with a fractional part, while the integer data type represents whole numbers without any fractional part.

Question 10

Null literal in Python means "there's nothing here".

Answer

False

Reason — In Python, the absence of a value is represented by the None keyword, not "null" literal.

Question 11

Python supports multiple assignments to multiple variables.

Answer

True

Reason — Python supports multiple assignments to multiple variables in a single line.
For example, msg, day, time = 'Meeting', 'Mon', 9

Multiple Choice Questions

Question 1

Which of the following are not the fundamental building blocks of a Python program?

  1. Identifiers
  2. Constants
  3. Keywords
  4. Errors

Answer

Errors

Reason — The fundamental building blocks of a Python program include identifiers, constants, and keywords. Errors, on the other hand, are issues that arise during program execution due to mistakes in the code.

Question 2

Identifier name cannot be composed of special characters other than ............... .

  1. #
  2. Hyphen (-)
  3. $\text{\textdollar}
  4. Underscore (_)

Answer

Underscore (_)

Reason — In Python, an identifier name can only be composed of letters (both uppercase and lowercase), digits, and underscores (_). Special characters like #, hyphen (-), and $\text{\textdollar} are not allowed in identifier names.

Question 3

Python takes ............... indented spaces after the function declaration statement by default.

  1. 5
  2. 6
  3. 4
  4. 3

Answer

4

Reason — In Python, the default indentation level after a function declaration statement is 4 spaces.

Question 4

Single line comments in Python begin with ............... symbol.

  1. #
  2. "
  3. '''
  4. %

Answer

#

Reason — Single line comments in Python begin with # symbol.

For example,

x = 10
y = x + 100 #value of x + 100 is assigned to variable y
print(y)

Question 5

Which of the following does not represent a complex number?

  1. K = 2 + 3j
  2. K = complex(2,3)
  3. K = 2 + 3i
  4. K = 4 + 3j

Answer

K = complex(2,3)

Reason — Complex numbers are made up of pairs of real and imaginary numbers. They take the form 'x+yj' or 'x+yi', where x is the real part and y is the imaginary part. Therefore, according to this, K = complex(2,3) is not a complex number.

Question 6

What is the order of precedence of arithmetic operators given below in Python?

(1) Parenthesis
(2) Exponential
(3) Multiplication
(4) Division
(5) Addition
(6) Subtraction

  1. 1,2,3,4,5,6
  2. 2,3,4,5,6,1
  3. 1,3,2,6,4,5
  4. 4,6,5,2,3,1

Answer

1,2,3,4,5,6

Reason — The order of precedence of arithmetic operators in Python is Parenthesis (), Exponential (**), Multiplication (*), Division (/), Addition (+), Subtraction (-).

Question 7

What will be the output of the following snippet?

x, y = 2, 6
x, y = y, x + 2
print(x, y)
  1. 6 6
  2. 4 4
  3. 4 6
  4. 6 4

Answer

6 4

Reason — Initially, x is assigned the value 2, and y is assigned 6 using multiple assignments. Then, the expression y, x + 2 is evaluated, where y is 6, and x + 2 calculates to 4. After evaluating the right-hand side, x is reassigned to y, making x equal to 6, and y is reassigned to x + 2, making y equal to 4. Thus, when print(x, y) executes, it outputs 6 4, showing the final values after the assignments.

Question 8

The extension of a Python file is given as-

  1. .pt
  2. .pwy
  3. .py or .pyw
  4. .yppy

Answer

.py or .pyw

Reason — The extension of a Python file is given as .py or .pyw.

Question 9

The reserved words used by Python interpreter to recognize the structure of a program are termed as ............... .

  1. Identifiers
  2. Tokens
  3. Literals
  4. Keywords

Answer

Keywords

Reason — Keywords are the reserved words used by Python interpreter to recognize the structure of a program.

Question 10

Which of the following is a syntactically correct string?

  1. "This is great !"
  2. 'she shouted 'HELLO' loudly'
  3. "Goodbye'
  4. "This "course" is good"

Answer

"This is great !"

Reason — In Python, strings enclosed in either single quotes (') or double quotes (") are valid, but the entire string must be enclosed in the same type of quote and when we want to include quotes within a string, we should use one type of quote to enclose the string and the other type of quote inside the string itself. The "This is great !" string uses double quotes (") to properly enclose the entire string. The other options have mismatched and same nested quotes, which cause syntax errors in Python.

Question 11

What can be the maximum possible length of an identifier?

  1. 31
  2. 63
  3. 79
  4. Can be of any length

Answer

Can be of any length

Reason — In Python, identifiers can be of any length.

Question 12

Which of the following operators is floor division?

  1. +
  2. /
  3. //
  4. >

Answer

//

Reason — The floor division operator in Python is represented by //.

Question 13

Which of the following is an invalid statement?

  1. a=b=c=20
  2. a,b,c=10,20,30
  3. abc = 20 30 40
  4. a_b_c=20

Answer

abc = 20 30 40

Reason — In Python, the statement abc = 20 30 40 is invalid because it attempts to assign multiple values (20, 30, and 40) to a single variable (abc) in a single assignment statement.

Question 14

What is the result of this statement:

10>5 and 7>12 or not 18>3
  1. 10
  2. True
  3. False
  4. None

Answer

False

Reason — The expression evaluates as follows: 10 > 5 is True because 10 is greater than 5. 7 > 12 is False because 7 is not greater than 12. 18 > 3 is True because 18 is greater than 3, and not True results in False. Combining these using Python's operator precedence, 10 > 5 and 7 > 12 evaluates to False (True and False). Then, False or not True evaluates to False (False or False). Therefore, the entire expression 10 > 5 and 7 > 12 or not 18 > 3 ultimately evaluates to False.

Question 15

What will be the output of the following Python code?

>>> 6*3+4**2//5-8
  1. 13
  2. 14
  3. Error
  4. None

Answer

13

Reason — The expression 6*3+4**2//5-8 follows Python's operator precedence rules, where exponentiation (4**2) is evaluated first resulting in 16. Then, floor division (16//5) is calculated as 3. Next, multiplication (6*3) gives 18. Adding these results (18 + 3) yields 21. Finally, subtracting 8 from 21 results in 13.

Question 16

What will be the output of the following Python code?

>>> a=72.55
>>> b=10
>>> c=int(a + b)
>>> c
  1. 72.55
  2. 72
  3. 82
  4. None of these

Answer

82

Reason — In the above code, a is assigned the floating-point number 72.55, and b is assigned the integer 10. The variable c is assigned the expression a + b resulting in 72.55 + 10 = 82.55. Then, the int() function is used to convert the result of a + b to an integer. Therefore, int(82.55) results in 82.

Assertions and Reasons

Question 1

Assertion (A): Each component of a programming statement is known as a token.

Reasoning (R): Token is not executed by Python interpreter, only used in the program coding.

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

Answer

A is true but R is false.

Explanation
A token is the smallest element of a Python script that is meaningful to the interpreter. Each component of a programming statement is referred to as a token. Comments are not executed by the interpreter, but tokens are executed by it.

Question 2

Assertion (A): The data type of a variable is declared as per the type of value assigned to it.

Reasoning (R): Python exhibits the feature of dynamic typing. In dynamic typing, the type of the variable is determined only during runtime.

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

Answer

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

Explanation
In Python, we are not required to explicitly declare a variable with its type. Whenever we declare a variable with a value, Python automatically assigns the relevant data type to it based on the assigned value. This process is known as dynamic typing.

Question 3

Assertion (A): In Python, integers are zero, positive or negative whole numbers without a fractional part.

Reasoning (R): -45, 655.55, -1000, 55.89 are the integer values in Python.

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

Answer

A is true but R is false.

Explanation
Integer represents whole numbers without any fractional part. They can be zero, positive or negative. -45, -1000 are the integer values in Python while 655.55, 55.89 are floating-point numbers.

Question 4

Assertion (A): In Python, strings, lists and tuples are called sequences.

Reasoning (R): Sequence is referred to as an ordered collection of values having similar or different data types.

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

Answer

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

Explanation
A sequence is an ordered collection of items in Python, and these items can have similar or different data types. They are indexed by integers. The three main types of sequence data types in Python are strings, lists, and tuples.

Question 5

Assertion (A): Comments are non-executable statements that enable the users to understand the program logic.

Reasoning (R): Comments are basically statements used to put remarks. The comments are used to explain the code and to make it more informative for the users.

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

Answer

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

Explanation
Comments are statements in a script that are ignored by the Python interpreter. Comments are basically statements used to put remarks and are used to explain the code and to make it more informative for the users. They make the code more readable and understandable for human beings.

Question 6

Assertion (A): A set of valid characters recognized by Python constitutes a character set.

Reasoning (R): Character set in Python is a subset of Python tokens.

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

Answer

A is true but R is false.

Explanation
Character set is a set of valid characters recognized by Python. A character represents any letter, digit or any other symbol present on the keyword. On the other hand, a token is the smallest element of a Python script that is meaningful to the interpreter which includes keywords, identifiers, literals, operators and delimiters/punctuators. A character set can be used to form tokens, but not all tokens are part of the character set.

Question 7

Assertion (A): 'Rollnumber' and 'rollnumber' are same identifiers.

Reasoning (R): Python is a case-sensitive language, i.e., it treats uppercase and lowercase letters differently.

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

Answer

A is false but R is true.

Explanation
Python is a case-sensitive language, meaning it distinguishes between uppercase and lowercase letters. Therefore, 'Rollnumber' and 'rollnumber' are not considered the same identifiers in Python.

Question 8

Assertion (A): Floor division operator (//) in Python is different from division operator.

Reasoning (R): Consider the given two statements:

>>>10//3 will give the output as 3.

On the other hand,

>>>10/3 will give the output as 3.3333333333333335

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

Answer

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

Explanation
In Python, the floor division operator (//) performs integer division. On the other hand, the division operator (/) performs float division. For example, the statement 10 // 3 will give the output 3, whereas 10 / 3 will give the output 3.3333333333333335.

Solutions to Unsolved Questions

Question 1

Write a Python command/instruction/statement to display your name.

Answer

print("Ankita Sharma")
Output
Ankita Sharma

Question 2

Write a Python command to display your school name, class and section, separated by "-".

Answer

print("Kendriya Vidyalaya", "11", "A", sep = "-")
Output
Kendriya Vidyalaya-11-A

Question 3

Evaluate the following expressions manually:

(a) (2 + 3) ** 3 - 6/2

(b) (2 + 3) * 5//4 + (4 + 6)/2

(c) 12 + (3 * 4 - 6)/3

(d) 12 + (3 ** 4 - 6)//2

(e) 12 * 3 % 5 + 2 * 6//4

(f) 12 % 5 * 3 + (2 * 6)//4

Answer

(a) (2 + 3) ** 3 - 6/2
= 5**3 - 6/2
= 125 - 6/2
= 125 - 3.0
= 122.0

(b) (2 + 3) * 5//4 + (4 + 6)/2
= 5 * 5//4 + 10/2
= 25 // 4 + 10/2
= 6 + 10/2
= 6 + 5.0
= 11.0

(c) 12 + (3 * 4 - 6)/3
= 12 + (12 - 6)/3
= 12 + 6/3
= 12 + 2.0
= 14.0

(d) 12 + (3 ** 4 - 6)//2
= 12 + (81 - 6)//2
= 12 + 75//2
= 12 + 37
= 49

(e) 12 * 3 % 5 + 2 * 6//4
= 36 % 5 + 2 * 6//4
= 1 + 2 * 6//4
= 1 + 12 // 4
= 1 + 3
= 4

(f) 12 % 5 * 3 + (2 * 6)//4
= 2 * 3 + (2 * 6)//4
= 6 + 12 // 4
= 6 + 3
= 9

Question 4

Evaluate the above expressions by using IDLE as a calculator and verify the results that you get manually.

Answer

(a)

>>>(2 + 3) ** 3 - 6 / 2
Output
122.0

(b)

>>>(2 + 3) * 5 // 4 + (4 + 6) / 2
Output
11.0

(c)

>>>12 + (3 * 4 - 6) / 3
Output
14.0

(d)

>>>12 + (3 ** 4 - 6) // 2
Output
49

(e)

>>>12 * 3 % 5 + 2 * 6 // 4
Output
4

(f)

>>>12 % 5 * 3 + (2 * 6) // 4
Output
9

Question 5

Name three runtime errors that occur during Python program execution.

Answer

Three runtime errors that occur during Python program execution are:

  1. TypeError
  2. ZeroDivisionError
  3. IndexError

Question 6

What is the difference between an error and exception?

Answer

An Error is a bug in the code that causes irregular output or stops the program from executing whereas an Exception is an irregular unexpected situation occurring during runtime on which the programmer has no control.

Question 7

Explain the difference between syntax error and runtime error with examples.

Answer

Syntax errors are errors that occur due to incorrect format of a Python statement. They occur while the statement is being translated into machine language and before being executed. They are the most common type of errors which are easily traceable. These errors can be corrected by the user as the reason for the error and an appropriate message about what is wrong in the program is displayed.

For example, print "Hello World" has syntax error as it violates the language protocol by not giving parentheses with the print() function. So, the corrected statement should be: print("Hello World").

Runtime errors are errors that occur while a program is executing, causing it to crash or behave unexpectedly. These errors are due to invalid operations or conditions that the program encounters during execution, which were not detected during compilation or before the program ran.

For example, 10 * (1/0) will generate runtime error as division of any number by 0 is undefined.

Question 8

Identify invalid variable names from the following, giving reason for each:

Group, if, total marks, S.I., volume, tot_strength, #tag, tag$\text{\textdollar}, 9a

Answer

  1. Group — It is a valid variable name.
  2. if — It is a reserved keyword in Python and cannot be used as a variable name.
  3. total marks — It contains a space, which is not allowed in variable names.
  4. S.I. — It contains a dot (.), which is not allowed in variable names.
  5. volume — It is a valid variable name.
  6. tot_strength — It is a valid variable name.
  7. #tag — It starts with a hash (#), which is not allowed in variable names.
  8. tag$\text{\textdollar} — It contains a dollar sign ($\text{\textdollar}), which is not allowed in variable names.
  9. 9a — It starts with a number, which is not allowed in variable names.

Question 9(a)

Find the output of the following code:

x=3
y=x+2
x+=y
print(x,y)

Answer

Output
8 5
Explanation
x = 3
y = x + 2
  • x is initialized to 3.
  • y is set to x + 2, which means y = 3 + 2 = 5.
x += y

The += operator adds the value of y to x and assigns the result back to x.

  • Current values are:
    • x = 3
    • y = 5
  • Calculation: x + y = 3 + 5 = 8
  • Updating x: x += 5 means x = x + 5
  • So, x = 3 + 5 = 8.
print(x, y)

The final values of the variables are:

  • x = 8
  • y = 5

Hence, the output of the program is:

8 5

Question 9(b)

Find the output of the following code:

x=-2
y=2
x+=y
y-=x
print (x,y)

Answer

Output
0 2
Explanation
x += y

The += operator adds the value of y to x and assigns the result back to x.

  • Current values are:
    • x = -2
    • y = 2
  • Calculation: x + y = -2 + 2 = 0
  • Updating x: x += 2 means x = x + 2
  • So, x = -2 + 2 = 0.
y -= x

The -= operator subtracts the value of x from y and assigns the result back to y.

  • Current values are:
    • x = 0
    • y = 2
  • Calculation: y - x = 2 - 0 = 2
  • Updating y: y -= 0 means y = y - 0
  • So, y = 2 - 0 = 2.
print(x, y)

The final values of the variables are:

  • x = 0
  • y = 2

Hence, the output of the program is:

0 2

Question 9(c)

Find the output of the following code:

a=5       
b=2*a         
a+=a+b        
b*=a+b        
print (a,b)  

Answer

Output
20 300
Explanation
a = 5
b = 2 * a
  • a is initialized to 5.
  • b is set to 2 * a, which means b = 2 * 5 = 10.
a += a + b

The += operator adds the value on the right to a and assigns the result back to a.

  • Current values are:
    • a = 5
    • b = 10
  • Calculation: a + b = 5 + 10 = 15
  • Updating a: a += 15 means a = a + 15
  • So, a = 5 + 15 = 20.
b *= a + b

The *= operator multiplies b by the value on the right and assigns the result back to b.

  • Current values are:
    • a = 20
    • b = 10
  • Calculation: a + b = 20 + 10 = 30
  • Updating b: b *= 30 means b = b * 30
  • So, b = 10 * 30 = 300.
print(a, b)

The final values of the variables are:

  • a = 20
  • b = 300

Hence, the output of the program is:

20 300

Question 9(d)

Find the output of the following code:

p=10
q=20
p*=q/3
q+=p+q*2
print (p,q)

Answer

Output
66.66666666666667 126.66666666666667
Explanation
p *= q / 3

The *= operator multiplies p by the value on the right and assigns it to p again.

  • First, compute the division: q / 3 = 20 / 3 ≈ 6.6667
  • p *= 6.6667 means p = p * 6.6667
  • So, p = 10 * 6.6667 = 66.6667
q += p + q * 2

The += operator adds the value on the right to q and assigns it to q again.

  • First, compute q * 2 = 20 * 2 = 40
  • Then add values: p + q * 2 = 66.6667 + 40
  • Finally, add it to q: q += 106.6667 means q = q + 106.6667
  • So, q = 20 + 106.6667 = 126.6667
print(p, q)

The variables now have these final values:

  • p = 66.6667
  • q = 126.6667

The precision difference is due to floating-point arithmetic. For simplicity of explanation we have taken lesser precision values.

Question 9(e)

Find the output of the following code:

p = 5 % 2
q = p ** 4
r = p // q
p+= p + q + r
r+= p + q + r
q-= p + q * r
print(p, q, r)

Answer

Output
4 -10 7
Explanation
p = 5 % 2

The modulo % operator calculates the remainder of the division.

  • 5 % 2 = 1
  • Thus, p = 1
q = p ** 4

The exponent ** operator raises p to the power of 4.

  • 1 ** 4 = 1
  • Thus, q = 1
r = p // q

The floor division // operator divides and returns the integer quotient of the division.

  • 1 // 1 = 1
  • Thus, r = 1
p += p + q + r

The += operator adds the value on the right to the variable and assigns it to the variable again.

  • Initially, p = 1
  • Calculation inside the parentheses: (p + q + r) = 1 + 1 + 1 = 3
  • p += 3 means p = p + 3
  • So, p = 1 + 3 = 4
r += p + q + r

The += operator adds the value on the right to the variable and assigns it to the variable again.

  • Initially, r = 1
  • Calculation inside the parentheses: (p + q + r) = 4 + 1 + 1 = 6
  • r += 6 means r = r + 6
  • So, r = 1 + 6 = 7
q -= p + q * r

The -= operator subtracts the value on the right from the variable and assigns it to the variable again.

  • Initially, q = 1
  • Calculation inside the parentheses: (p + q * r) = 4 + 1 * 7 = 4 + 7 = 11
  • q -= 11 means q = q - 11
  • So, q = 1 - 11 = -10
print(p, q, r)

The variables now have these final values:

  • p = 4
  • q = -10
  • r = 7

Hence, the output of the program is:

4 -10 7

Question 9(f)

Find the output of the following code:

p = 21 // 5
q = p % 4
r = p * q
p+= p + q - r
r*= p - q + r
q+= p + q
print(p, q, r)

Answer

Output
8 8 0
Explanation
p = 21 // 5

The floor division // operator divides and returns the integer quotient of the division.

  • ( 21 // 5 = 4 )
  • Thus, ( p = 4 )
q = p % 4

Here, the modulo % operator calculates the remainder of the division.

  • ( p % 4 = 4 % 4 = 0 )
  • Thus, ( q = 0 )
r = p * q

Now it performs a multiplication.

  • p x q = 4 x 0 = 0
  • Thus, r = 0
p += p + q - r

The += operator adds the value on the right to the variable and assigns it to the variable again.

  • Initially, ( p = 4 )
  • Calculation inside the parentheses: ( p + q - r = 4 + 0 - 0 = 4 )
  • p += 4 means p = p + 4
  • So, p = 4 + 4 = 8
r *= p - q + r

The *= operator multiplies the variable by the value on the right and assigns it to the variable again.

  • Initially, ( r = 0 )
  • Calculation inside the parentheses: ( p - q + r = 8 - 0 + 0 = 8 )
  • r *= 8 means r = r x 8
  • So, r = 0 x 8 = 0
q += p + q

The += operator adds the value on the right to the variable and assigns it to the variable again.

  • Initially, q = 0
  • Calculation inside the parentheses: p + q = 8 + 0 = 8
  • q += 8 means q = q + 8
  • So, q = 0 + 8 = 8
print(p, q, r)

The variables now have these final values:

  • p = 8
  • q = 8
  • r = 0

Hence, the output of the program is:

8 8 0

Question 10

Write Python expressions equivalent to the following arithmetic/algebraic expressions:

(a) a+b2\dfrac{a + b}{2}

(b) 32+932\dfrac{3^2 + 9^3}{2}

(c) 32+9353^2 + \dfrac{9^3}{5}

(d) a+a+2b\sqrt{a} + \dfrac{a + 2}{b}

(e) 86+6sum7var8 - 6 + \dfrac{6 * \text{sum}}{7} - \sqrt{\text{var}}

(f) ut+12at2ut + \dfrac{1}{2} at^2 (u, a, t are variables)

Answer

(a) (a + b) / 2

(b) (3**2 + 9**3) / 2

(c) 3**2 + 9**3 / 5

(d) a**0.5 + (a + 2) / b

(e) 8 - 6 + 6 * sum / 7 - var**0.5

(f) u * t + 1 / 2 * a * t**2

Question 11

Write Python expressions to represent the following situations:

(a) Add remainder of 8/3 to the product of 8 and 3.

(b) Find the square root of the sum of 8 and 43.

(c) Find the sum of the square roots of 8 and 43.

(d) Find the integral part of the quotient when 100 is divided by 32.

Answer

(a) 8 * 3 + 8 % 3

(b) (8 + 43)**(1/2)

(c) 8**(1/2) + 43**(1/2)

(d) 100//32

Question 12

What are operators? What is their function? Give examples of some unary and binary operators.

Answer

Operators are tokens that trigger some computation/action when applied to variables and other objects in an expression. Unary plus (+), Unary minus (-), Bitwise complement (~), Logical negation (not) are a few examples of unary operators. Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/).

Question 13

What is an expression and a statement?

Answer

An expression is any legal combination of symbols that represents a value. For example, 2.9, a + 5, (3 + 5) / 4.

A statement is a programming instruction that does something i.e. some action takes place. For example:
print("Hello")
a = 15
b = a - 10

Question 14

What all components can a Python program contain?

Answer

A Python program can contain various components like expressions, statements, comments, functions, blocks and indentation.

Question 15

What are variables? How are they important for a program?

Answer

Variables are named labels whose values can be used and processed during program run. Variables are important for a program because they enable a program to process different sets of data.

Question 16

What is 'Dynamic Typing' feature in Python?

Answer

A variable pointing to a value of a certain type can be made to point to a value/object of different type.This is called Dynamic Typing. For example:

x = 10
print(x)
x = "Hello World"
print(x)

Question 17

Which data type will be used to represent the following data values and why?

(a) Number of months in a year

(b) Resident of Delhi or not

(c) Mobile number

(d) Pocket money

(e) Volume of a sphere

(f) Perimeter of a square

(g) Name of the student

(h) Address of the student

Answer

S.
No.
Data ValueData TypeReason
(a)Number of months in a yearintA year has 12 months which is a whole number
(b)Resident of Delhi or notboolIt is a true or false value
(c)Mobile numberstrMobile numbers can contain leading zeros and may include country codes, so they are stored as strings.
(d)Pocket moneyfloatMoney can be a decimal value.
(e)Volume of a spherefloatVolume of a sphere is calculated using the formula ((4/3) * (22/7) * (r ** 3)) , which often results in a decimal value.
(f)Perimeter of a squarefloatThe perimeter of a square can be a whole number, using float allows for more flexibility (e.g., in case side length is a decimal).
(g)Name of the studentstrNames are sequences of characters.
(h)Address of the studentstrAddresses are text data, which may include letters, numbers, and special characters.

Question 18

Write a function called calculate_area() that takes base and height as an input argument and returns an area of a triangle as an output. The formula used is:

Area of a Triangle = ½*base*height

Solution
def calculate_area(base, height):
    area = (1/2) * base * height
    return area

base_value = int(input("Enter the base value: "))
height_value = int(input("Enter the height value: "))
triangle_area = calculate_area(base_value, height_value)
print("Area of the triangle:", triangle_area)
Output
Enter the base value: 10
Enter the height value: 5
Area of the triangle: 25.0

Question 19

Modify the above function to take a third parameter called shape type. Shape type should be either triangle or rectangle. Based on the shape, it should calculate the area. Formula used:

Area of a Rectangle = length * width.

Solution
def calculate_area(base, height, shape_type):
    if shape_type == "triangle":
        area = (1/2) * base * height
    elif shape_type == "rectangle":
        area = base * height
    else:
        area = None
        print("Invalid shape type. Please specify either 'triangle' or 'rectangle'.")
    return area

shape_type = input("Enter the shape type, triangle or rectangle: ")
base_value = int(input("Enter the base value: "))
height_value = int(input("Enter the height value: "))
area = calculate_area(base_value, height_value, shape_type)
print("Area of the", shape_type, "is ", area)
Output
Enter the shape type, triangle or rectangle: triangle
Enter the base value: 10
Enter the height value: 5
Area of the triangle is  25.0


Enter the shape type, triangle or rectangle: rectangle
Enter the base value: 8
Enter the height value: 9
Area of the rectangle is  72

Question 20

Write a function that takes amount in dollars and performs dollar-to-rupee price conversion; it then displays the amount converted to rupees.

Solution
def convert_dollars_to_rupees(amount_in_dollars, conversion_rate):
    amount_in_rupees = amount_in_dollars * conversion_rate
    return amount_in_rupees

amount = float(input("Enter amount in dollars "))
conversion_rate = float(input("Enter conversion rate "))
converted_amount = convert_dollars_to_rupees(amount, conversion_rate)
print("Converted amount:", converted_amount)
Output
Enter amount in dollars 55
Enter conversion rate 82
Converted amount: 4510.0

Question 21

What would the following code do?

a = b = 70

Answer

The above code performs multiple assignment, where the value 70 is assigned to both variables a and b simultaneously.

Question 22

What is the error in the following code?

z, p = 6

Answer

The error arises because the right-hand side (RHS) has one value (6) while the left-hand side (LHS) has two variables (z, p). This mismatch causes a ValueError.

Question 23(a)

Find out the error(s) in the following code fragment:

temperature = 90
Print temperature

Answer

The call to print function is missing parenthesis. The correct way to call print function is this:

print(temperature)

Question 23(b)

Find out the error(s) in the following code fragment:

a = 30
b = a + b
print (a And b)

Answer

There are two errors in this code fragment:

  1. In the statement b = a+b, variable b is undefined.

  2. In the statement print(a And b), And should be written as and.

Question 23(c)

Find out the error(s) in the following code fragment:

a, b, c = 2, 8, 9
print(a, b, c)
c, b, a + a, b, c
print(a; b; c)

Answer

There are two errors in this code fragment:

  1. The line c, b, a + a, b, c attempts to unpack values but is incomplete and incorrect. It lacks a right-hand side (RHS) to assign values from.

  2. In the line print(a; b; c), the print function should use commas (,) to separate arguments, not semicolons (;).

Question 23(d)

Find out the error(s) in the following code fragment:

x = 24
4 = x

Answer

The statement

4 = x

is incorrect as 4 cannot be a Lvalue. It is a Rvalue.

Question 23(e)

Find out the error(s) in the following code fragment:

Print ("X =" X)

Answer

There are two errors in this code fragment:

  1. Variable X is undefined.

  2. "X =" and X should be separated by a comma like this print("X =", X)

Question 24(a)

What will be the output produced by the following code fragment?

X = 10
X = X+10
X = X-5
print (X)
X, Y = X - 2, 22
print (X, Y)

Answer

Output
15
13 22
Explanation
  1. X = 10 ⇒ assigns an initial value of 10 to X.
  2. X = X + 10 ⇒ X = 10 + 10 = 20. So value of X is now 20.
  3. X = X - 5 ⇒ X = 20 - 5 = 15. X is now 15.
  4. print (X) ⇒ print the value of X which is 15.
  5. X, Y = X - 2, 22 ⇒ X, Y = 13, 22.
  6. print (X, Y) ⇒ prints the value of X which is 13 and Y which is 22.

Question 24(b)

What will be the output produced by the following code fragment?

first = 2
second = 3
third = first * second
print (first, second, third)
third = second * first
print (first, second, third)

Answer

Output
2 3 6
2 3 6
Explanation
  1. first = 2first is assigned the value 2.
  2. second = 3second is assigned the value 3.
  3. third = first * secondthird is calculated as first * second, which is 2 * 3 = 6.
  4. print(first, second, third) ⇒ The print statement outputs 2 3 6, showing the values of first, second, and third.
  5. third = second * firstthird is reassigned with second * first, which is 3 * 2 = 6.
  6. print(first, second, third) ⇒ The second print statement outputs 2 3 6 again, reflecting the updated value of third.

Question 24(c)

What will be the output produced by the following code fragment?

side = int (input ('side')) #Side given as 7
area = side * side
print (side, area)

Answer

Output
side7
7 49
Explanation
  1. side = int(input('side') ) ⇒ This statements asks the user to enter the side. We enter 7 as the value of side.
  2. area = side * side ⇒ area = 7 * 7 = 49.
  3. print (side, area) ⇒ prints the value of side and area as 7 and 49 respectively.

Question 25

"Comments are useful and an easy way to enhance readability and understandability of a program." Elaborate with examples.

Answer

Comments are statements in a script that are ignored by the Python interpreter and therefore, have no effect on the actual output of the code. Comments make the code more readable and understandable for human beings. This makes the revision/modification of the code easier by the original author of the code and also by some new author who has been assigned the responsibility to modify it. They are highly recommended if the script is too complex or if it is to be reviewed by multiple people over an interval of time.

For example,

# Calculate the area of a rectangle
length = 5
width = 3
# Formula: area = length * width
area = length * width
print("Area of the rectangle is:", area)

Question 26(a)

Find errors in the following code fragment.

a = 3
print(a)
b = 4
print(b)
s = a + b
print (s)

Answer

There are no errors in the above code fragment.

Question 26(b)

Find errors in the following code fragment.

Name = "Prateek"
Age = 26
print("your name & age are", Name + Age)

Answer

The error in the above code is in the print statement. We cannot concatenate a string (Name) and an integer (Age) directly using the '+' operator. The corrected code is:

Name = "Prateek"
Age = 26
print("Your name & age are", Name + str(Age))

Question 26(c)

Find errors in the following code fragment.

A = 3
S = A + 10
A = "New"
Q = A / 10

Answer

The error in the given code fragment occurs in the Q = A / 10 line, where it attempts to divide the string "New" by the integer 10. This is not a valid operation and will raise a TypeError.

Question 27

Give the output.

x = 40
y = x + 1
x, y = 20, y + x
print (x, y)

Answer

Output
20 81
Explanation
x = 40
y = x + 1
  • x is initialized to 40.
  • y is set to x + 1, which means y = 40 + 1 = 41.
x, y = 20, y + x

The values of x and y are updated simultaneously. This means both assignments are evaluated first and then applied.

  • The left-hand side of the assignment is x, y.
  • The right-hand side is 20, y + x.
  • x is set to 20.
  • y is set to y + x, which means y = 41 + 40 = 81.
print(x, y)

The final values of the variables are:

  • x = 20
  • y = 81

Hence, the output of the program is:

20 81

Question 28

Give the output.

x, y = 20, 60
y, x, y = x, y - 10, x + 10
print (x, y)

Answer

Output
50 30
Explanation
  1. x, y = 20, 60 ⇒ assigns an initial value of 20 to x and 60 to y.
  2. y, x, y = x, y - 10, x + 10 ⇒ y, x, y = 20, 60 - 10, 20 + 10 ⇒ y, x, y = 20, 50, 30 First RHS value 20 is assigned to first LHS variable y. After that second RHS value 50 is assigned to second LHS variable x. Finally third RHS value 30 is assigned to third LHS variable which is again y. After this assignment, x becomes 50 and y becomes 30.
  3. print (x, y) ⇒ prints the value of x and y as 50 and 30 respectively.

Question 29

Give the output.

a, b = 12, 13
c, b = a * 2, a/2
print(a, b, c)

Answer

Output
12 6.0 24
Explanation
  1. a, b = 12, 13 ⇒ assigns an initial value of 12 to a and 13 to b.
  2. c, b = a*2, a/2 ⇒ c, b = 12*2, 12/2 ⇒ c, b = 24, 6.0. So c has a value of 24 and b has a value of 6.0.
  3. print (a, b, c) ⇒ prints the value of a, b, c as 12, 6.0 and 24 respectively.

Question 30

Give the output.

a, b, c = 10, 20, 30
p, q, r = c - 5, a + 3, b - 4
print(p, q, r)

Answer

Output
25 13 16
Explanation

In the given code, using multiple assignment, three variables a, b, and c are initially assigned values 10, 20, and 30 respectively and variables p, q, and r are assigned new values: p is assigned c - 5 (resulting in 25), q is assigned a + 3 (resulting in 13), and r is assigned b - 4 (resulting in 16). When print(p, q, r) is executed, it outputs 25 13 16, representing the values after the assignments.

Question 31

Find errors in the following code fragment. (The input entered as XI)

c = int (input ("Enter your class:"))
print ("Your class is", c)

Answer

The input value XI is not int type compatible.

Question 32

Find errors in the following code fragment.

cl = input("Enter your class")
print("Last year you were in class", cl - 1)

Answer

The error in the above code fragment occurs when trying to perform arithmetic operation on the result of the input() function. The input() function returns a string, even if the user enters a number. Therefore, attempting to subtract 1 from cl (which is a string) will raise a TypeError. The corrected code is:

cl = int(input("Enter your class: "))
print("Last year you were in class", cl - 1)

Question 33

Write a Python program that accepts radius of a circle and prints its area.

Solution
r = float(input("Enter radius of circle: "))
a = 3.14159 * r * r
print("Area of circle =", a)
Output
Enter radius of circle: 7.5
Area of circle = 176.7144375

Question 34

Write Python program that accepts marks in 5 subjects and outputs total and average marks.

Solution
subject1 = float(input("Enter marks for subject 1: "))
subject2 = float(input("Enter marks for subject 2: "))
subject3 = float(input("Enter marks for subject 3: "))
subject4 = float(input("Enter marks for subject 4: "))
subject5 = float(input("Enter marks for subject 5: "))
total_marks = subject1 + subject2 + subject3 + subject4 + subject5
average_marks = total_marks / 5
print("Total marks:", total_marks)
print("Average marks:", average_marks)
Output
Enter marks for subject 1: 76
Enter marks for subject 2: 87
Enter marks for subject 3: 95
Enter marks for subject 4: 81
Enter marks for subject 5: 90
Total marks: 429.0
Average marks: 85.8

Question 35

Write a program to find the area of a triangle.

Solution
h = float(input("Enter height of the triangle: "))
b = float(input("Enter base of the triangle: "))
area = 0.5 * b * h
print("Area of triangle = ", area)
Output
Enter height of the triangle: 2.5
Enter base of the triangle: 5
Area of triangle =  6.25

Question 36

Write a program to input a number and print its first five multiples.

Solution
n = int(input("Enter number: "))
print("First five multiples of", n, "are")
print(n, n * 2, n * 3, n * 4, n * 5)
Output
Enter number: 5
First five multiples of 5 are
5 10 15 20 25

Question 37

Write a program to read details like name, class, age of a student and then print the details, firstly in the same line and then in separate lines.

Solution
name = input("Enter name of student: ")
c = int(input("Enter class of student: "))
age = int(input("Enter age of student: "))
print("Name:", name, "Class:", c, "Age:", age)
print()
print("Name:", name)
print("Class:", c)
print("Age:", age)
Output
Enter name of student: Kavya
Enter class of student: 9
Enter age of student: 14
Name: Kavya Class: 9 Age: 14

Name: Kavya
Class: 9
Age: 14

Question 38

Write a program to read three numbers in three variables and swap first two variables with the sums of first and second, second and third numbers, respectively.

Solution
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("The three number are", a, b, c)
a, b = a + b, b + c
print("Numbers after swapping are", a, b, c)
Output
Enter first number: 10
Enter second number: 15
Enter third number: 20
The three number are 10 15 20
Numbers after swapping are 25 35 20

Question 39

Write Python codes for the following statements:

(a) Assign value 10 to variable a.

(b) Assign value 10 to variables a, b and c.

(c) Assign value 20 to x and delete 5 from x and assign the variable x to y.

Answer

(a)

a = 10

(b)

a = b = c = 10

(c)

x = 20
x = x - 5
y = x
PrevNext