Introduction to Data Types & Variables

ICSE Class 9 Robotics & AI · Solutions for Class 9 ICSE Exploring Robotics & Artificial Intelligence · 29 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

Which of the following numbers depict the given clip?

Which of the following numbers depict the given clip. Ethical considerations in AI, APC ICSE Robotics & Artificial Intelligence Solutions Class 9.
Show SolutionHide Solution↓

Complex Number

Reason — The given clip represents a complex number, which is the combination of a real number and an imaginary number. In Python, it is expressed as $a + bj$, where $a$ represents the real part and $b$ represents the imaginary part with $j = \sqrt{-1}$.

21 Mark · Multiple Choice Questions

Given: m = 24

What is the data type of the given Python variable?

Show SolutionHide Solution↓

Integer

Reason — The value 24 is a whole number without a decimal point. All whole numbers (positive or negative) without a decimal point are said to be of integer (int) type in Python.

31 Mark · Multiple Choice Questions

Which data type in Python is used to store a sequence of characters?

Show SolutionHide Solution↓

String

Reason — In Python, a string is defined as a collection of one or more characters enclosed within single, double or triple quotes. It is used to store a sequence of characters.

41 Mark · Multiple Choice Questions

Which of the following is the correct representation to define a list in Python?

Show SolutionHide Solution↓

Elements are written using square brackets [ ]

Reason — In Python, a list is defined as an ordered sequence of one or more data items separated by commas and enclosed within square brackets [ ].

51 Mark · Multiple Choice Questions

Which of the following data type would be used for a variable to store the value 3.14 in Python?

Show SolutionHide Solution↓

float

Reason — The value 3.14 is a fractional number with a decimal point. All fractional numbers are said to be of float type in Python, also known as floating point numbers.

61 Mark · Multiple Choice Questions

Which of the following data types would be used in Python to store values in terms of key and value?

Show SolutionHide Solution↓

dictionary

Reason — In Python, a dictionary is an unordered set of elements where data is stored as "Key:Value" pairs, enclosed within curly brackets { }.

71 Mark · Multiple Choice Questions

Given My_Data = [12, 25, 'Hello', "World"]

What data type is the representation of the above object?

Show SolutionHide Solution↓

list

Reason — The elements are enclosed within square brackets [ ] and contain data items of different types (integers and strings) separated by commas, which represents a list in Python.

81 Mark · Multiple Choice Questions

Which of the following is a mutable data type in Python?

Show SolutionHide Solution↓

Lists

Reason — Lists in Python are mutable, meaning their elements can be altered, added, or removed after the list is created. Strings, tuples and integers are immutable in Python.

91 Mark · Multiple Choice Questions

Which of the following data types does not allow duplicate values in Python?

Show SolutionHide Solution↓

Sets

Reason — A set in Python is a built-in data type that represents an unordered collection of unique elements. It automatically removes duplicate elements when present.

101 Mark · Multiple Choice Questions

Boolean data is used to test a particular condition i.e., True or False.

Which of the following is the correct representation?

Show SolutionHide Solution↓

none of them

Reason — In Python, there is no 'boolean' keyword used while declaring a variable. The Boolean values True or False are assigned directly to a variable (e.g., m = True) without enclosing them in quotes. Hence, none of the given representations are correct.

1State True or False

State whether the following statements are True or False:

  1. Comments are executable statements.
  2. The import statements are used to include modules or libraries into the program to accomplish tasks.
  3. Operators are the symbols or tokens to perform various mathematical or logical operations on data.
  4. All whole numbers (positive or negative numbers) without a decimal point are said to be float type numbers.
  5. The tuple is an ordered set of elements containing one or more data items of the same or different types.
  6. In Python, we cannot assign the same integer value to different variables.
  7. Python is case sensitive and thus it treats uppercase and lowercase letters differently.
  8. Boolean constants are special literals and represent only two Boolean values.
Show SolutionHide Solution↓
  1. False
    Corrected Statement: Comments are non-executable statements.
  2. True
  3. True
  4. False
    Corrected Statement: All whole numbers (positive or negative numbers) without a decimal point are said to be int (integer) type numbers.
  5. True
  6. False
    Corrected Statement: In Python, we can assign the same integer value to different variables.
  7. True
  8. True
1Name the constants

Name the following constants as defined in Python:

ConstantsName of the constant
0.00345
'A'
True
-999
Show SolutionHide Solution↓
ConstantsName of the constant
0.00345Floating Point Constant
'A'String Constant
TrueBoolean Constant
-999Integer Constant
1Predict Output — Type casting

Predict the output and type of casting for the given Python expressions:

Python ExpressionExplicit/Implicit
m = 32.005, n = 45
print(int(m+n))
a = 42.85, b = 12.45
print(a+b)
Show SolutionHide Solution↓
Python ExpressionOutputExplicit/Implicit
m = 32.005, n = 45
print(int(m+n))
77Explicit
a = 42.85, b = 12.45
print(a+b)
55.30Implicit
1Predict the output

Predict the output when executed:

Given: list1 = ['Robotics', 2026, 'ICSE', 'Edition']

StatementOutput
print(list1)
print(list1[0])
print(list1[1:3])
print(list1[2:])
Show SolutionHide Solution↓
StatementOutput
print(list1)['Robotics', 2026, 'ICSE', 'Edition']
print(list1[0])Robotics
print(list1[1:3])[2026, 'ICSE']
print(list1[2:])['ICSE', 'Edition']
2Predict the output

Predict the output when executed:

StatementOutput
a = 24
m = float(a)
print(m)
b = 32.86
n = int(b)
print(n)
c = 3
p = complex(c)
print(p)
Show SolutionHide Solution↓
StatementOutput
a = 24
m = float(a)
print(m)
24.0
b = 32.86
n = int(b)
print(n)
32
c = 3
p = complex(c)
print(p)
(3+0j)
1Assertion and Reason based question

Assertion (A): Variable is a named memory location which contains a value.

Reason (R): When you create a variable, it reserves some space in the memory.

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

Show SolutionHide Solution↓

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

Reason — A variable is a named memory location which contains a value. When a variable is created, the system reserves some space in the memory to store its value, and this memory location is identified by the variable name. Hence, R correctly explains why A is true.

1Application based question

In Python programming, it is an unordered collection data type that is iterable, mutable and has no duplicate elements. It is represented by { } where the values are enclosed in curly braces.

Read the above paragraph and answer the following questions:

(a) Name the data type that is used in Python.

(b) What will happen, if duplicate elements are present in this data types and executed?

Show SolutionHide Solution↓

(a) The data type used in Python is Set.

(b) If duplicate elements are present in a set and executed, the set will automatically remove the duplicates and retain only the unique values. This is because a set does not allow duplicate elements. For example, executing My_set = {"red", "blue", "green", "yellow", "blue", "green"} will result in {"red", "blue", "green", "yellow"} being stored, as the duplicate values "blue" and "green" are eliminated.

1Write short notes

Write short notes on Indentation.

Show SolutionHide Solution↓

Indentation is the fundamental aspect of Python programming that defines the structure and flow of code. Unlike many other programming languages that use curly braces { } or keywords to delimit blocks of code, Python relies on indentation levels to group statements. It refers to the aligning of a statement or group of statements which are part of another statement to get the desired execution of a specific block. All the statements within a block must be indented by the same amount, and it is a common practice to use 4 spaces per indentation level. Mixing tabs and spaces is not allowed and will result in an Indentation Error. Therefore, proper indentation is crucial in Python as it directly affects the execution of the code and its readability.

2Write short notes

Write short notes on Assignment.

Show SolutionHide Solution↓

Assignment means to store values in variables using a token '='. Here, the symbol '=' acts as an assignment operator. The 'equal to' sign (=) is used to assign values to the variables. The operand to the left of the '=' operator is the variable name and the operand to the right of the '=' operator is the value to be stored.

Syntax: <variable> = <value>

For example:

  • m = 15 : 15 is stored in the variable m which is of integer type.
  • n = 45.24 : 45.24 is stored in the variable n which is of float type.
  • ch = 'k' : The letter 'k' is stored as a string character in variable ch.
  • wd = "Computer Science" : A word is stored in the variable wd.
3Write short notes

Write short notes on Boolean Constant.

Show SolutionHide Solution↓

Boolean constants are special literals that represent only two Boolean values, i.e., True or False. They can be used in a Python code to check whether a given logical condition is satisfied or not. The Boolean constants are written with their initial characters in capital letters (i.e., 'T' or 'F'). Writing them in lowercase (true or false) is incorrect and will raise an error in Python. It must be noted that Boolean constants (True or False) are never enclosed within single or double quotes. This characteristic makes Boolean constants different from string constants.

4Write short notes

Write short notes on Variable.

Show SolutionHide Solution↓

A variable is a named memory location which contains a value. When a variable is created, it reserves some space in the memory to store the data. The value of a variable can change depending upon the requirements in a program. For example, n = 25 means that the integer value 25 is assigned to the variable 'n', and the data value 25 will be stored in the memory location identified by the name 'n'. Whenever we refer to 'n', we will get 25. However, the value of 'n' can be changed at any time, and when a variable is reassigned with a new value, the original value will get replaced by the new value.

1Answer the following questions

Name the components which are essentially involve in the basic structure of Python programming.

Show SolutionHide Solution↓

The components essentially involved in the basic structure of Python programming are as follows:

  1. Comments — Non-executable statements used to explain the purpose of the code.
  2. Import Statements — Used to include modules or libraries into the program.
  3. Variables — Used to store data in memory for performing various tasks and later use.
  4. Data Types — Built-in types like integers, floats, strings, boolean, lists, tuple, etc.
  5. Operators — Symbols or tokens to perform various mathematical or logical operations on data.
  6. Control Structures — Used to control the flow of instructions (if-else, for loops, while loops).
  7. Functions — Used to group related statements together and give them a name for reuse.
2Answer the following questions

What is meant by data type?

Show SolutionHide Solution↓

Data types are referred to as the classification or categorisation of data items. They represent a particular type of value which determines what operations can be performed on that particular data. Python supports several built-in data types including integers, floats, strings, boolean, lists, tuples, sets and dictionaries to perform various tasks during programming.

3Answer the following questions

In what ways Python data types are categorised?

Show SolutionHide Solution↓

Python data types are categorised into the following groups:

  1. Numeric — Represents data with numeric values. It includes:
    • Integer (int): whole numbers without a decimal point.
    • Float: fractional numbers with a decimal point.
    • Complex: combination of a real number and an imaginary number.
  2. Non-Numeric — Includes Boolean data type (True or False).
  3. Sequences — Ordered collection of items, includes:
    • String
    • List
    • Tuple
  4. Sets — Unordered collection of unique elements.
  5. Dictionary — Unordered set of key-value pairs.
4Answer the following questions

Explain the following data types:

(a) String

(b) List

(c) Tuple

(d) Dictionary

Show SolutionHide Solution↓

(a) String: In Python, a string is defined as a collection of one or more characters enclosed within single, double or triple quotes. When a string is assigned to a variable, each character sets itself to an index and can be accessed individually with reference to memory locations. The index starts from 0 in the forward direction and from -1 in the backward direction.

For example: wd = "Artificial Intelligence"

(b) List: A list is defined as an ordered sequence of one or more data items of the same or different data types. The data items in a list are separated by commas and enclosed within square brackets [ ]. One of the most important features of a list is that it is mutable, i.e., an element can be altered in its place. The values can be accessed with their indices, starting from the 0th index till the (length – 1)th index.

For example: list1 = ['Python', 2026, 'Edition', 'ICSE']

(c) Tuple: Like a list, a tuple is also an ordered set of elements containing one or more data items of the same or different types. The data items in a tuple are separated by commas but enclosed within parentheses ( ). The main difference between lists and tuples is that a list is mutable (can be modified) whereas a tuple is immutable (the elements cannot be updated).

For example: tuple1 = ('Robotics', 'Artificial', 'Intelligence', 'with', 'Python')

(d) Dictionary: A dictionary is an unordered set of elements where data is stored as keys along with their corresponding values. Every key has its corresponding value, forming a pair in the form of Key:Value. The collection of such pairs is enclosed within curly brackets { }. Generally, dictionary keys are strings, whereas values can be any arbitrary Python object.

For example: dict = {"Robotics":1, "Artificial":2, "Intelligence":3, "with":4, "Python":5}

5Answer the following questions

What is meant by Python Sets? Explain with the help of an example.

Show SolutionHide Solution↓

In Python, a set is a built-in data type that represents an unordered collection of unique elements. Sets are used to store multiple items in a single variable, with elements enclosed within curly braces { } and separated by commas. A set is unordered, unchangeable (in terms of modifying individual elements) and unindexed.

Characteristics of a Set:

  • Unordered: The elements do not have a specific order, so they may appear in different orders.
  • Unique Elements: A set automatically removes duplicate elements.
  • Mutable: Elements can be added or removed from a set after its creation.

Example:

My_set = {"red", "blue", "green", "yellow"}
print(My_set)

Output: {'red', 'green', 'yellow', 'blue'}

If we try adding duplicates, the set will retain only unique values. For example, My_set = {"red", "blue", "green", "yellow", "blue", "green"} will store only {'red', 'blue', 'green', 'yellow'}.

6Answer the following questions

What are the rules to assign a variable in a Python code?

Show SolutionHide Solution↓

The rules to assign a variable in a Python code are as follows:

  1. A variable may have any number of characters.
  2. A variable name cannot start with a number, but it can start with characters or an underscore (e.g., _rollnum).
  3. The underscore can be used in between the characters to separate the words of a variable name (e.g., reg_no).
  4. The variable names should be meaningful, which easily explains their purpose.
  5. Python is case sensitive. So, it treats uppercase and lowercase letters differently (e.g., Name and name are different variables).
7Answer the following questions

Explain each of the following with an example:

(i) Implicit type conversion

(ii) Explicit type conversion

Show SolutionHide Solution↓

(i) Implicit Type Conversion: In a mixed mode expression, the result gets automatically converted into the highest data type available in the expression, without any intervention from the user. This system of conversion is known as implicit type conversion or coercion.

Example:

a = 15; b = 4; c = 12.4
ans = (a + b) - c
print(ans)

Here, (a + b) gives an integer (int – int = int), then int – float gives a float. So, the result is 6.6 (float type), obtained automatically without any user intervention.

(ii) Explicit Type Conversion: Explicit type conversion is another way of type conversion, in which one data type gets converted into another data type depending upon the user's choice. When the data type is converted after the user's intervention, the conversion is known as explicit type conversion or type casting.

Example:

a = 32; b = 12; c = 4
ans = int((a - b) / c)
print(ans)

Here, (a - b) / c would normally give 5.0 (float). But the user explicitly applies int() to forcibly convert it to int type, resulting in 5. This forceful conversion by the user is called type casting.

8Answer the following questions

How will you assign multiple objects to multiple variables? Explain with the help of an example.

Show SolutionHide Solution↓

Python language facilitates assigning multiple objects to multiple variables in a single statement. The values may be of mixed data types like integer, floating point numbers, strings, etc. This is done by separating the variables and their corresponding values with commas.

Syntax: var1, var2, var3 = value1, value2, value3

Example:

m, n, wd = 146, 2020, "Computer"
print(m)
print(n)
print(wd)

Output:

146
2020
Computer

Here, two integers with values 146 and 2020 are assigned to the variables m and n respectively, and one string with the value "Computer" is assigned to the variable wd, all in a single statement.

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