Revision of Python

ICSE Class 10 Robotics & AI · KIPS Robotics & AI · 37 solved questions

Exercise
1Activity 12.1

Check that the following code snippet will execute properly or not. Justify your answer.

a=10 
b=25 
if a<b: 
print("a" is less than "b") 
else: 
print("b" is less than "a")
Show SolutionHide Solution↓

Output: No output

Justification: The given code snippet will not execute properly because of incorrect indentation. In Python, it is mandatory to indent the statements inside the if and else blocks. If the statements under the if condition are not indented, Python will display an IndentationError and the program will not run. Since the print() statements are not indented under if and else, the code is syntactically incorrect.

2State True or False

Python keywords can be used as variable names.

Show SolutionHide Solution↓

False

Reason — Python keywords should not be used as variable names.

3State True or False

The '+' operator can be used for string concatenation.

Show SolutionHide Solution↓

True

Reason — The + operator is used to perform concatenation of two strings in Python.

4State True or False

In Python, a variable name can consist of letters, digits, and underscore.

Show SolutionHide Solution↓

True

Reason — In Python, a variable name must start with a letter (capital or small) or an underscore (_), it can consist of letters, digits, and underscore only, variable names are case-sensitive, and Python keywords should not be used as variable names.

5State True or False

The 'for' loop uses membership operator 'in'.

Show SolutionHide Solution↓

True

Reason — The for loop in Python uses the membership operator in to check the presence of a value in a sequence and to iterate over each element of that sequence one by one.

6State True or False

The 'while' loop can be used to create an infinite loop.

Show SolutionHide Solution↓

True

Reason — The while loop executes as long as the given condition evaluates to True. If the condition never becomes False, the loop will continue to execute indefinitely, thereby creating an infinite loop.

7State True or False

A loop inside a loop is called nested loop.

Show SolutionHide Solution↓

True

Reason — A loop that is written inside the body of another loop is called a nested loop, where one loop (inner loop) executes completely for each iteration of the outer loop.

8State True or False

In Python, it is necessary to provide at least one argument in a function.

Show SolutionHide Solution↓

False

Reason — A function can take any number of arguments or none. If a function does not accept any arguments, the parentheses are left empty while defining the function. Therefore, it is not necessary to provide at least one argument in a Python function.

9State True or False

The return statement in Python function cannot return multiple values.

Show SolutionHide Solution↓

False

Reason — In Python, a function is allowed to return more than one value using a single return statement by separating the values with commas.

10State True or False

'*args' keyword allows to pass a variable number of arguments and arguments are stored in a tuple.

Show SolutionHide Solution↓

True

Reason — The *args keyword is used to pass a variable number of arguments to a function. The arguments passed using *args are automatically stored in a tuple, which can be accessed inside the function.

1Select the correct option

The NumPy library in Python is mainly used for:

Show SolutionHide Solution↓

Mathematical operations and data analysis

Reason — NumPy is an important library that provides data manipulation, numerical computing, and statistical analysis facilities.

2Select the correct option

Consider the following statement and answer the question:

data =("name": "Arjit", "age": 24)

This statement:

Show SolutionHide Solution↓

Will produce an error

Reason — A dictionary in Python must be defined using curly braces {} with key–value pairs separated by a colon. The given statement does not use curly braces, so it results in an error.

3Select the correct option

The statement 5 % 4 will return ............... .

Show SolutionHide Solution↓

1

Reason — The modulus operator (%) returns the remainder after division of two numbers. When 5 is divided by 4, the remainder obtained is 1, hence the result is 1.

4Select the correct option

Which of the following keywords is not used in conditional statements?

Show SolutionHide Solution↓

go

Reason — The keywords used in Python conditional statements are if, else, and elif. The keyword go is not a conditional keyword in Python.

5Select the correct option

Which logical operator(s) is/are used to combine multiple conditions in 'if' statement?

Show SolutionHide Solution↓

both a and b

Reason — In Python, the logical operators and and or are used to combine multiple conditions in an if statement. The and operator checks if all conditions are true, while the or operator checks if any one of the conditions is true.

6Select the correct option

In Python, what would be the condition to check if a value is not equal to 5?

Show SolutionHide Solution↓

x != 5

Reason — In Python, the != operator is the not equal to comparison operator. It is used to check whether two values are not equal. Therefore, the correct condition to check if a value is not equal to 5 is x != 5.

7Select the correct option

From the following, which is not a comparison operator in Python?

Show SolutionHide Solution↓

<>

Reason — In Python, the valid comparison operators include ==, !=, and >=. The symbol <> is not a comparison operator in Python.

8Select the correct option

Functions in programming:

Show SolutionHide Solution↓

All of these

Reason — Functions help in achieving modularity by breaking a large program into smaller manageable parts. They promote code reusability by allowing the same block of code to be used multiple times, and they improve program readability by organising code into logical blocks.

9Select the correct option

The structure of function header in Python is:

Show SolutionHide Solution↓

"def" keyword followed by the function name and parameters

Reason — A Python function header consists of the def keyword, followed by the function name, a list of parameters enclosed in parentheses, and ends with a colon.

10Select the correct option

What would be output of the following code snippet?

def multiply(x, y=2): 
    return x*y 
result = multiply(5) 
print(result) 
Show SolutionHide Solution↓

10

Reason — Default arguments are used when a value is not provided during the function call. In the given function multiply(x, y=2), the parameter y has a default value of 2. When the function is called as multiply(5), the value 5 is assigned to x and the default value 2 is assigned to y. Therefore, the function returns 5 × 2 = 10, which is printed as the output.

1Short answer type questions

What do you understand by data type? List names of Python built-in data types.

Show SolutionHide Solution↓

A data type specifies the types of data that may be stored in variables as well as the actions that can be performed on them. The range of values that a variable may contain and the amount of memory required to store the value is also determined by the data type of the variable.

The built-in data types in Python are int, float, complex, bool, string, list, tuple, dictionary, and set.

2Short answer type questions

Write a Python program to declare and print 'list', 'dictionary', and 'tuple'.

Show SolutionHide Solution↓
# Declaring a list
my_list = [10, 20, 30, 40]
print("List:", my_list)

# Declaring a dictionary
my_dict = {"Name": "Amit", "Age": 15, "City": "Delhi"}
print("Dictionary:", my_dict)

# Declaring a tuple
my_tuple = (5, 10, 15, 20)
print("Tuple:", my_tuple)
3Short answer type questions

Write a Python program to demonstrate use of string operators.

Show SolutionHide Solution↓
a = "KIPS"
b = "Learning"

# String concatenation
print(a + b)

# String replication
print(a * 4)
KIPSLearning
KIPSKIPSKIPSKIPS
4Short answer type questions

Explain the use of break statement in Python with the help of an example.

Show SolutionHide Solution↓

The break statement in Python is used to terminate a loop immediately and transfer the control outside the loop, even if the loop condition is still true.

Example:

for txt in "Python Language":
    if txt == "n":
        break
    print(txt)
print("The end")

Output:

P
y
t
h
o
The end

In this example, the loop iterates over each character of the string. When the character 'n' is encountered, the break statement is executed, which stops the loop immediately and the control comes out of the loop.

5Short answer type questions

Write a Python program to check that if a given number is even or odd.

Show SolutionHide Solution↓
num = int(input("Enter a number: "))

if num % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")
Enter a number: 82
The number is even.

Enter a number: 51
The number is odd.
1Long answer type questions

Write a Python program to demonstrate arithmetic operators in Python.

Show SolutionHide Solution↓
a = 5
b = 2

add = a + b
sub = a - b
mul = a * b
div = a / b
mod = a % b
floordiv = a // b
power = a ** b

print("Addition:", add)
print("Subtraction:", sub)
print("Multiplication:", mul)
print("Division:", div)
print("Modulus:", mod)
print("Floor Division:", floordiv)
print("Exponent:", power)
2Long answer type questions

Explain relational operators in Python. Give one example for each.

Show SolutionHide Solution↓

Relational Operators in Python:

Operator NameOperatorDescriptionExample (a = 5, b = 2)Result
Equality (Is Equal to)==Returns true if a equals to ba == bFalse
Not Equal to!=Returns true if a is not equals to ba != bTrue
Greater than>Returns true if a is greater than ba > bTrue
Greater than or equal to>=Returns true if a is greater than b or a is equals to ba >= bTrue
Less than<Returns true if a is less than ba < bFalse
Less than or equal to<=Returns true if a is less than b or a is equals to ba <= bFalse
3Long answer type questions

Write a Python program to demonstrate the use of the if-elif-else statements.

Show SolutionHide Solution↓
num = 4

if num == 1:
    print("Red")
elif num == 2:
    print("Blue")
elif num == 3:
    print("Green")
elif num == 4:
    print("Yellow")
elif num == 5:
    print("Orange")
else:
    print("Invalid input")
Yellow
4Long answer type questions

Describe user-defined functions in Python with the help of an example.

Show SolutionHide Solution↓

A user-defined function is a function that is defined by the user to perform a specific task. It enables the user to write their own code and reuse it whenever required. User-defined functions help in reducing repetition of code and make programs easier to understand and maintain.

In Python, a user-defined function is created using the def keyword, followed by the function name, parameters enclosed in parentheses, and a colon. The statements that define the task of the function form the function body.

Example:

def add(a, b):
    return a + b
result = add(5, 6)
print(result)

In the above example, the function add() is a user-defined function that takes two parameters and returns their sum using the return statement. This function can be called any number of times with different arguments.

5Long answer type questions

Distinguish between void functions and non-void functions with the help of an example.

Show SolutionHide Solution↓

Void functions are those functions that do not return any value. They may or may not contain a return statement, but even if the return statement is present, it returns no value. When such a function is called, it automatically returns None. Void functions are also called non-fruitful functions.

Example of a void function:

def msg():
    print("Welcome")
msg()

In this example, the function msg() only prints a message and does not return any value. Hence, it is a void (non-fruitful) function.

A non-void function is a function that returns a value using the return statement after execution. The value returned is passed back to the calling statement. Non-void functions are also known as fruitful functions.

Example of a non-void function:

def add(a, b):
    return a + b
result = add(4, 6)
print(result)

In this example, the function add() returns the sum of two numbers using the return statement. Hence, it is a non-void (fruitful) function.

1Higher Order Thinking Skills (HOTS)

What is the difference between division operator and floor division operator?

Show SolutionHide Solution↓

The division operator (/) divides two numbers and returns the result in decimal (floating-point) form, whereas the floor division operator (//) performs integer division and returns only the quotient without the decimal part.

Example:

  • 7 / 3 = 2.3333333333333335
  • 7 // 3 = 2

Thus, the division operator gives the exact result, while the floor division operator gives the integer quotient.

2Higher Order Thinking Skills (HOTS)

Write a python program to print a pyramid using 'for' loop.

Show SolutionHide Solution↓
rows = 5

for i in range(1, rows + 1):
    for j in range(rows - i):
        print(" ", end="")
    for k in range(1, 2 * i):
        print("*", end="")
    print()
    *
   ***
  *****
 *******
*********
3Higher Order Thinking Skills (HOTS)

Write a program to store marks of your class students and show their percentage.

Show SolutionHide Solution↓
n = int(input("Enter number of students: "))
max_marks = int(input("Enter maximum marks: "))

names = []
percentages = []

for i in range(n):
    name = input("Enter student name: ")
    marks = int(input("Enter total marks: "))
    percentage = (marks / max_marks) * 100

    names.append(name)
    percentages.append(percentage)

print("\nStudent Name and Percentage")
for i in range(n):
    print(names[i], "-", percentages[i], "%")
Enter number of students: 3
Enter maximum marks: 300
Enter student name: Asha
Enter total marks: 250
Enter student name: Ravi
Enter total marks: 208
Enter student name: Neha
Enter total marks: 290

Student Name and Percentage
Asha - 83.33333333333334 %
Ravi - 69.33333333333334 %
Neha - 96.66666666666667 %
4Higher Order Thinking Skills (HOTS)

Write a program to print the longest word in the given list of words.

Show SolutionHide Solution↓
words = ["apple", "banana", "grapes", "watermelon", "kiwi"]

longest = words[0]

for w in words:
    if len(w) > len(longest):
        longest = w

print("The longest word is:", longest)
The longest word is: watermelon
5Higher Order Thinking Skills (HOTS)

Write a function which returns if a number passed as as argument is a perfect number or an Armstrong number.

Show SolutionHide Solution↓
def check_number(n):
    s = 0
    temp = n
    digits = len(str(n))

    while temp > 0:
        d = temp % 10
        s = s + d ** digits
        temp = temp // 10

    p = 0
    for i in range(1, n):
        if n % i == 0:
            p = p + i

    if p == n:
        return "Perfect Number"
    elif s == n:
        return "Armstrong Number"
    else:
        return "Neither Perfect nor Armstrong"

num = int(input("Enter a number: "))
result = check_number(num)
print(result)
Enter a number: 9474
Armstrong Number

Enter a number: 28
Perfect Number
6Higher Order Thinking Skills (HOTS)

Write a function which takes a number and a list as arguments and counts the occurrence of that number in the given list.

Show SolutionHide Solution↓
def count_occurrence(num, lst):
    count = 0
    for x in lst:
        if x == num:
            count = count + 1
    return count

numbers = eval(input("Enter list: "))
num = int(input("Enter the number to count: "))

result = count_occurrence(num, numbers)
print("Occurrence:", result)
Enter list: [2, 4, 5, 2, 7, 2, 9]
Enter the number to count: 2
Occurrence: 3

Press ↓ to reveal a solution and move to the next question. Back to KIPS Robotics & AI.

Free sample bank