What would be the output of following Python code snippet:
maintenance ={"car" : 2000, "furniture" : 1000, "Filter" : 1500}
print(maintenance["car"])
print(maintenance["Filter"])Answer
2000
1500
- The dictionary
maintenancestores key–value pairs. maintenance["car"]accesses the value associated with the key"car", which is2000.maintenance["Filter"]accesses the value associated with the key"Filter", which is1500.- Hence, the output is:
2000
1500
Identify suitable data type(s). One has been done for you.
| Scenario | Data Type |
|---|---|
| Storing area of square | int, 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) |
Answer
| Scenario | Data Type |
|---|---|
| Storing area of square | int, float |
| Storing age of a person | int |
| Storing age, height and weight in a single data type | list |
| 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 |
Python is an important language for data analysis.
Answer
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.
Python is popular due to its simplicity and strong libraries.
Answer
True
Reason — Python is considered to be the most appropriate language for beginners due to its simple and clearly defined syntax, and it has a huge standard library with ready-to-use modules.
The object-oriented programming (OOP) paradigm organises data and functions into reusable structures.
Answer
True
Reason — Object-oriented programming works on the concept of objects, which can contain data and functions and help in the reusability of code.
Python is not suitable for game development.
Answer
False
Reason — Python provides modules such as Pygame and Panda3D that allow developers to create 2D and 3D games.
In a Python code, you can start a new statement from anywhere.
Answer
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.
In a tuple, you can change the elements after defining them.
Answer
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.
Python keywords can be used as variable names.
Answer
False
Reason — Python keywords should not be used as variable names.
Python has separate operator for integer division.
Answer
True
Reason — Python has a separate operator for integer division called the floor division operator (//).
The '+' operator can be used for string concatenation.
Answer
True
Reason — The + operator is used to perform concatenation of two strings in Python.
In Python, a variable name can consist of letters, digits, and underscore.
Answer
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.
In Python, important libraries for machine learning is/are:
- TensorFlow
- stat
- math
- list
Answer
TensorFlow
Reason — TensorFlow is a library that provides efficient tools for creating and training machine learning models.
The NumPy library in Python is mainly used for:
- Machine learning
- Plotting
- Mathematical operations and data analysis
- Developing games
Answer
Mathematical operations and data analysis
Reason — NumPy is an important library that provides data manipulation, numerical computing, and statistical analysis facilities.
Which of the following is/ are the features of Python?
- Object-oriented
- High-level
- General-purpose
- All of these
Answer
All of these
Reason — Python is an object-oriented, high-level, and general-purpose programming language.
To define a dictionary in Python, which bracket is used?
- Curly{}
- Square[]
- Small()
- None of these
Answer
Curly{}
Reason — In Python, a dictionary is enclosed in curly braces {} and stores data in the form of key-value pairs.
Consider the following statement and answer the question:
data = ("name": "Arjit", "age": 24)This statement:
- Will create a dictionary
- Will create a tuple
- Will produce an error
- Will create a set
Answer
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.
Which out of the following is a valid variable name?
- @val
- 9val
- _val
- int
Answer
_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.
- @val — Invalid, because special characters like
@are not allowed in variable names. - 9val — Invalid, because a variable name cannot start with a digit.
- _val — Valid, because it starts with an underscore and follows all the rules of variable naming.
- int — Invalid, because
intis a Python keyword and keywords cannot be used as variable names.
The statement 5 % 4 will return ............... .
- 0
- 1
- 2
- 3
Answer
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.
The statement 5 // 2 will return ............... .
- 2
- 2.5
- 5
- 0
Answer
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.
Which of the following is a membership operator?
- and
- or
- in
- None of these
Answer
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.
Which keyword in Python is used to import a module?
- include
- input
- import
- system
Answer
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.
Fill in the blanks:
- Python is a ................ which means it may be used to create a broad variety of applications.
- ................ frameworks are used for web development in Python.
- ................ is a tool kit in Python for GUI development.
- The suitable data type to store only True or False is ................. .
- Lists are ................ and ................ are immutable in Python.
Answer
- Python is a general-purpose language which means it may be used to create a broad variety of applications.
- Django and Flask frameworks are used for web development in Python.
- wxPython is a tool kit in Python for GUI development.
- The suitable data type to store only True or False is bool.
- Lists are mutable and tuples are immutable in Python.
What do you understand by data type? List names of Python built-in data types.
Answer
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.
What is the difference between list and tuple in Python? Explain with the help of an example.
Answer
| List | Tuple |
|---|---|
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) |
Write a Python program to declare and print 'list', 'dictionary', and 'tuple'.
# 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)
Write a Python program to demonstrate use of string operators.
a = "Sun"
b = "Rise"
# String concatenation
print(a + b)
# String replication
print(a * 4)SunRise
SunSunSunSun
Why indentation in Python is important? Explain with the help of an example.
Answer
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.
Write a Python program to demonstrate difference between list and tuple.
Answer
# 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] = 56In 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.
Write a Python program to demonstrate arithmetic operators in Python.
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
Explain relational operators in Python. Give one example for each.
Answer
Relational Operators in Python:
| Operator Name | Operator | Description | Example (a = 5, b = 2) | Result |
|---|---|---|---|---|
| Equality (Is Equal to) | == | Returns true if a equals to b | a == b | False |
| Not Equal to | != | Returns true if a is not equals to b | a != b | True |
| Greater than | > | Returns true if a is greater than b | a > b | True |
| Greater than or equal to | >= | Returns true if a is greater than b or a is equals to b | a >= b | True |
| Less than | < | Returns true if a is less than b | a < b | False |
| Less than or equal to | <= | Returns true if a is less than b or a is equals to b | a <= b | False |
What is the difference between division operator and floor division operator?
Answer
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.
What is the difference between assignment operator and equality operator?
Answer
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 operatorOutput:
True
Write and evaluate the following expression in Python:
(a + b > c) and (a - b == -a + b), for a = 2, b = 3, c = 4
Answer
(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