Python Programming

ICSE Class 9 Robotics & AI · Solutions for Class 9 KIPS ICSE Robotics & Artificial Intelligence · 34 solved questions

Exercise
1Activity 7.1

What would be the output of following Python code snippet:

maintenance ={"car" : 2000, "furniture" : 1000, "Filter" : 1500}
print(maintenance["car"])
print(maintenance["Filter"])
Show SolutionHide Solution↓
2000
1500
  • The dictionary maintenance stores key–value pairs.
  • maintenance["car"] accesses the value associated with the key "car", which is 2000.
  • maintenance["Filter"] accesses the value associated with the key "Filter", which is 1500.
  • Hence, the output is:
2000
1500
1Activity 7.2

Identify suitable data type(s). One has been done for you.

ScenarioData Type
Storing area of squareint, float
Storing age of a person 
Storing age, height and weight in a single data type 
Storing age, height and weight in a single data type (It should not be changed, once stored) 
Storing age, height and weight in a single data type (index name should reflect that the data corresponds to age, height, and weight) 
Show SolutionHide Solution↓
ScenarioData Type
Storing area of squareint, float
Storing age of a personint
Storing age, height and weight in a single data typelist
Storing age, height and weight in a single data type (It should not be changed, once stored)tuple
Storing age, height and weight in a single data type (index name should reflect that the data corresponds to age, height, and weight)dictionary
1State True or False

Python is an important language for data analysis.

Show SolutionHide Solution↓

True

Reason — Python is widely used in data analysis because it provides powerful libraries such as NumPy, Pandas, and SciPy, which offer facilities for data manipulation, numerical computing, and statistical analysis.

3State True or False

The object-oriented programming (OOP) paradigm organises data and functions into reusable structures.

Show SolutionHide Solution↓

True

Reason — Object-oriented programming works on the concept of objects, which can contain data and functions and help in the reusability of code.

4State True or False

Python is not suitable for game development.

Show SolutionHide Solution↓

False

Reason — Python provides modules such as Pygame and Panda3D that allow developers to create 2D and 3D games.

5State True or False

In a Python code, you can start a new statement from anywhere.

Show SolutionHide Solution↓

False

Reason — In Python, indentation is used to define blocks of code, and statements with the same indentation belong to the same block. Starting a statement from anywhere may result in a syntax or logical error.

6State True or False

In a tuple, you can change the elements after defining them.

Show SolutionHide Solution↓

False

Reason — A tuple is an immutable data type, which means the elements of a tuple cannot be changed or updated after it is created.

7State 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.

8State True or False

Python has separate operator for integer division.

Show SolutionHide Solution↓

True

Reason — Python has a separate operator for integer division called the floor division operator (//).

9State 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.

10State 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.

1Select the correct option

In Python, important libraries for machine learning is/are:

Show SolutionHide Solution↓

TensorFlow

Reason — TensorFlow is a library that provides efficient tools for creating and training machine learning models.

2Select 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.

3Select the correct option

Which of the following is/ are the features of Python?

Show SolutionHide Solution↓

All of these

Reason — Python is an object-oriented, high-level, and general-purpose programming language.

4Select the correct option

To define a dictionary in Python, which bracket is used?

Show SolutionHide Solution↓

Curly{}

Reason — In Python, a dictionary is enclosed in curly braces {} and stores data in the form of key-value pairs.

5Select 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.

6Select the correct option

Which out of the following is a valid variable name?

Show SolutionHide Solution↓

_val

Reason — According to the rules for declaring variable names in Python, a variable name must start with a letter (capital or small) or an underscore (_). It can consist only of letters, digits, and underscore, variable names are case-sensitive, and Python keywords should not be used as variable names.

  1. @val — Invalid, because special characters like @ are not allowed in variable names.
  2. 9val — Invalid, because a variable name cannot start with a digit.
  3. _val — Valid, because it starts with an underscore and follows all the rules of variable naming.
  4. int — Invalid, because int is a Python keyword and keywords cannot be used as variable names.
7Select 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.

8Select the correct option

The statement 5 // 2 will return ............... .

Show SolutionHide Solution↓

2

Reason — The floor division operator (//) performs integer division and returns the quotient without the decimal part after division of two numbers. Hence, when 5 is divided by 2, it returns 2 and not 2.5.

9Select the correct option

Which of the following is a membership operator?

Show SolutionHide Solution↓

in

Reason — The in operator is a membership operator used in Python to check whether a value exists in a sequence such as a string, list, or tuple.

10Select the correct option

Which keyword in Python is used to import a module?

Show SolutionHide Solution↓

import

Reason — In Python, the import keyword is used to import modules at the start of a program so that the functions and features of those modules can be used.

1Fill blanks

Fill in the blanks:

Show SolutionHide Solution↓
  1. Python is a general-purpose language which means it may be used to create a broad variety of applications.
  2. Django and Flask frameworks are used for web development in Python.
  3. wxPython is a tool kit in Python for GUI development.
  4. The suitable data type to store only True or False is bool.
  5. Lists are mutable and tuples are immutable in Python.
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

What is the difference between list and tuple in Python? Explain with the help of an example.

Show SolutionHide Solution↓
ListTuple
Elements of list are enclosed within square brackets [ ].Elements of tuple are enclosed within parentheses ( ).
List is mutable (elements can be changed).Tuple is immutable (elements cannot be changed).
Example: my_list = [1, 1.0+3j, "great", True]Example: my_tuple = (1, 2, 3, 4)
3Short 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)
List: [10, 20, 30, 40]
Dictionary: {'Name': 'Amit', 'Age': 15, 'City': 'Delhi'}
Tuple: (5, 10, 15, 20)
4Short answer type questions

Write a Python program to demonstrate use of string operators.

Show SolutionHide Solution↓
a = "Sun"
b = "Rise"

# String concatenation
print(a + b)

# String replication
print(a * 4)
SunRise
SunSunSunSun
1Long answer type questions

Why indentation in Python is important? Explain with the help of an example.

Show SolutionHide Solution↓

Indentation refers to the whitespaces or tabs that are used at the beginning of a block of statements. The statements or instructions with the same indentation belong to the same block, i.e., multiple statements with the same indent are called a block of code. Indentation is used in Python to include all the statements belonging to a particular block. Inappropriate indentation may result in a syntactical or logical error.

Example:

i = 1
if i == 1:
    print("i is equal to 1")
else:
    print("i is not equal to 1")

Here, the statement next to the if statement is indented, which shows that this statement is under the scope of the if statement and will be executed if the condition is true. Otherwise, the else part will be executed.

2Long answer type questions

Write a Python program to demonstrate difference between list and tuple.

Show SolutionHide Solution↓
# Declaring a list
my_list = [5, 8, 9, 12, 45]
print(my_list[1])

# Modifying an element of the list
my_list[1] = 56
print(my_list)

# Declaring a tuple
my_tuple = (5, 8, 9, 12, 45)
print(my_tuple[1])

# Trying to modify an element of the tuple
my_tuple[1] = 56

In the above program, a list is created using square brackets [ ] and it is a mutable data type, so its elements can be changed after creation. Therefore, modifying the value at index 1 in the list is possible. A tuple is created using parentheses ( ) and it is an immutable data type, so its elements cannot be changed after creation. Attempting to change a tuple element results in a TypeError, which shows the difference between a list and a tuple.

3Long 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)
Addition: 7
Subtraction: 3
Multiplication: 10
Division: 2.5
Modulus: 1
Floor Division: 2
Exponent: 25
4Long 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
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)

What is the difference between assignment operator and equality operator?

Show SolutionHide Solution↓

The assignment operator (=) is used to assign a value to a variable, whereas the equality operator (==) is used to compare two values and returns True or False based on whether the values are equal.

Example:

a = 10      # assignment operator
b = 10

print(a == b)   # equality operator

Output:

True
3Higher Order Thinking Skills (HOTS)

Write and evaluate the following expression in Python:

(a + b > c) and (a - b == -a + b), for a = 2, b = 3, c = 4

Show SolutionHide Solution↓

(a + b > c) and (a - b == -a + b)
= (2 + 3 > 4) and (2 - 3 == -2 + 3)
= (5 > 4) and ( 2 - 3 == -2 + 3)
= True and (-1 == 1)
= True and False
= False

Press ↓ to reveal a solution and move to the next question. Back to Solutions for Class 9 KIPS ICSE Robotics & Artificial Intelligence.