KnowledgeBoat Logo
|
OPEN IN APP

Part II: AI — Chapter 3.2

Introduction to Data Types & Variables

Class 9 - Exploring Robotics & AI



Multiple Choice Questions

Question 1

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.
  1. Real Number
  2. Imaginary Number
  3. Complex Number
  4. Floating Number

Answer

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+bja + bj, where aa represents the real part and bb represents the imaginary part with j=1j = \sqrt{-1}.

Question 2

Given: m = 24

What is the data type of the given Python variable?

  1. String
  2. Integer
  3. Float
  4. Double

Answer

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.

Question 3

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

  1. Char
  2. String
  3. Text
  4. Word

Answer

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.

Question 4

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

  1. Elements are written using curly braces { }
  2. Elements are written using parentheses ( )
  3. Elements are written using square brackets [ ]
  4. Elements are written using angle brackets < >

Answer

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 [ ].

Question 5

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

  1. int
  2. decimal
  3. double
  4. float

Answer

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.

Question 6

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

  1. list
  2. tuple
  3. dictionary
  4. String

Answer

dictionary

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

Question 7

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

What data type is the representation of the above object?

  1. list
  2. dictionary
  3. array
  4. tuple

Answer

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.

Question 8

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

  1. Strings
  2. Tuples
  3. Lists
  4. Integers

Answer

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.

Question 9

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

  1. Lists
  2. Dictionaries
  3. Tuples
  4. Sets

Answer

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.

Question 10

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

Which of the following is the correct representation?

  1. boolean m=True
  2. boolean m='True'
  3. boolean m="True"
  4. none of them

Answer

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.

State True or False

Question 1

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.

Answer

  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

Name the constants

Question 1

Name the following constants as defined in Python:

ConstantsName of the constant
0.00345
'A'
True
-999

Answer

ConstantsName of the constant
0.00345Floating Point Constant
'A'String Constant
TrueBoolean Constant
-999Integer Constant

Predict Output — Type casting

Question 1

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)

Answer

Python ExpressionOutputExplicit/Implicit
m = 32.005, n = 45
print(int(m+n))
77Explicit
a = 42.85, b = 12.45
print(a+b)
55.30Implicit

Predict the output

Question 1

Predict the output when executed:

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

StatementOutput
print(list1)
print(list1[0])
print(list1[1:3])
print(list1[2:])

Answer

StatementOutput
print(list1)['Robotics', 2026, 'ICSE', 'Edition']
print(list1[0])Robotics
print(list1[1:3])[2026, 'ICSE']
print(list1[2:])['ICSE', 'Edition']

Question 2

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)

Answer

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)

Assertion and Reason based question

Question 1

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:

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

Answer

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.

Application based question

Question 1

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?

Answer

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

Write short notes

Question 1

Write short notes on Indentation.

Answer

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.

Question 2

Write short notes on Assignment.

Answer

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.

Question 3

Write short notes on Boolean Constant.

Answer

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.

Question 4

Write short notes on Variable.

Answer

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.

Answer the following questions

Question 1

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

Answer

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.

Question 2

What is meant by data type?

Answer

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.

Question 3

In what ways Python data types are categorised?

Answer

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.

Question 4

Explain the following data types:

(a) String

(b) List

(c) Tuple

(d) Dictionary

Answer

(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}

Question 5

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

Answer

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'}.

Question 6

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

Answer

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

Question 7

Explain each of the following with an example:

(i) Implicit type conversion

(ii) Explicit type conversion

Answer

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

Question 8

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

Answer

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.

PrevNext