Unit 3 Python Programming Class 11 AI Code 843 Book Solution
CBSE/NCERT BOOK EXERCISES
A. Objective Type Questions
1. Identify the datatype L = “45”
a. String
b. int
c. float
d. tuple
Ans:– a. String
2. Which of the following function converts a string to an integer in python?
a. int(x)
b. long(x)
c. float(x)
d. str(x)
Ans:- a. int(x)
3. Which special symbol is used to add comments in python?
a. $
b. //
c. /*…. */
d. #
Ans:- d. #
4. Which of the following variable is valid?
a. Str name
b.1str
c. _str
d. #Str
Ans:- c. _str
5. Elements in the list are enclosed in _____ brackets
a. ( )
b. { }
c. [ ]
d. /* */
Ans:- c. [ ]
6. Index value of last element in list is ____________________
a. 0
b.-10
c. -1
d.10
Ans:- c. -1
7. What will be the output of the following code?
a = [10,20,30,40,50]
print(a[0])
a. 20
b. 50
c. 10
d.40
Ans:- c. 10
8. Name the function that displays the data type of the variable.
a. data( )
b. type( )
c. datatype( )
d. int( )
Ans:- b. type()
9. Which library helps in manipulating csv files?
a. files
b.csv
c. math
d. print
Ans:- b. csv
10. Which keyword can be used to stop a loop?
a. stop
b.break
c. brake
d. close
Ans:- c. brake
11. What is the primary data structure used in NumPy to represent arrays of any dimension?
a) Series
b) DataFrame
c) ndarray
d) Panel
Ans:- c. ndarray
12. Which of the following is not a valid method to access elements of a Pandas DataFrame?
a) Using column names as attributes.
b) Using row and column labels with the .loc[] accessor.
c) Using integer-based indexing with the .iloc[] accessor.
d) Using the .get() method.
Ans:- d) Using the .get() method.
13. What is the purpose of the head() method in Pandas?
a) To display the first few rows of a DataFrame.
b) To display the last few rows of a DataFrame.
c) To count the number of rows in a DataFrame.
d) To perform aggregation operations on a DataFrame.
Ans:- a) To display the first few rows of a DataFrame.
14. Which method is used to drop rows with missing values from a DataFrame in Pandas?
a) drop_rows()
b) remove_missing()
c) dropna()
d) drop_missing_values
Ans:- c. dropna( )
15. Which is not a module of Sklearn?
a) load_iris
b)train_test_split
c)metrics
d) Scikit
Ans:- d) Scikit
B. Answer Questions
1. input() function accepts the value as string only. How can you convert string to int?
Ans:- Using int( ) function together with input( ), we can convert string to int.
2. What are variables? What are the rules of declaring variables in Python?
Ans:- Named labels whose value can be used and processed during program run. Generally, keywords (list given above) are not used as variables. Variable names cannot start with digit and also it can’t contain any special characters except underscore.
3. What do you mean by type casting?
Ans:- A variable of particular datatype can be converted into another datatype using some functions. The explicit conversion of an operand to a specific type is called type casting.
4. “Python supports dynamic typing”, True or False. Justify your answer.
Ans:- True.
A variable pointing to a value of certain data type can be made to point to a value/object of another data type. This is called Dynamic Typing. Python supports Dynamic Typing.
5. Name any four features of python language.
Ans:-
❖ High Level language
❖ Interpreted Language
❖ Free and Open Source
❖ Platform Independent (Cross-Platform)
6. Give examples for keywords.
Ans:- and, as, continue, if, not try, del, pass
7. Expand CSV.
Ans:- Comma Separated Values
8. How do you read data from a CSV file into a Pandas DataFrame?
Ans:-
import pandas as pd
pd.read_csv(“filename.csv”)
C. Long Answer Questions
1. Describe the data types supported by Python, providing relevant examples.
Ans:-
Data Type | Description | Example |
Integer | Stores whole number | a=10 |
Floaing Point | Stores numbers with fractional part | x=5.5 |
Complex | Stores a number having real and imaginary part | num=a+bj |
String | immutable sequences Stores text enclosed in single or double quote or triple quotes | name= “Ria” |
List | mutable sequences Stores list of comma separated values of any data type between square [ ] | lst=[ 25, 15.6, “car”, “XY”] |
Tuple | Immutable sequence Stores list of comma separated values of any data type between parentheses ( ) | tup=(11, 12.3, “abc”) |
Dictionary | Unordered set of comma-separated key:value pairs within braces {} | dict= { 1 : “One”, 2: “Two”, 3: “Three”} |
2. Define an operator and provide examples of different operators along with their functions.
Ans:- Operators are symbols or keywords that perform operations on operands to produce a result. Python supports a wide range of operators:
- Arithmetic operators (+, -, *, /, %)
- Relational operators (==, !=, <, >, <=, >=)
- Assignment operators (=, +=, -=)
- Logical operators (and, or, not)
- Bitwise operators (&, |, ^, <<, >>)
- Identity operators ( is, is not)
- Membership operators (in, not in)
D. Practice Programs
1. Write a Tipper program where the user inputs the total restaurant bill. The program should then display two amounts: 15 percent tip and 20 percent tip.
Ans:
2. Write a program to check whether the user is eligible for driving license or not.
Ans:-
3. Your father always gives his car for service after 15000 km. Check whether his car needs service or not. Read the kilometer reading from the user and give the output.
Ans:-
4. Write a program to display the first ten even natural numbers (use for loop).
Ans:-
5. Write a program to accept the Basic salary from the user and calculate the Net Salary.
Net Salary= Basic Salary + HRA + DA
PF HRA=30% of Basic DA=20% of Basic PF=12% of Basic
Ans:-
6. Write a program to create series from an array in Python.
Ans:-
7. Consider the following admission.csv and answer the following questions:
a. Create a dataframe from the admission.csv
Ans: import pandas as pd
import csv
df=pd.read_csv(“admission.csv”)
print(df)
b. Display first 3 rows of the dataframe
Ans:- print(df.head(3))
c. Display the details of Ravi
Ans: print(df . loc[‘Ravi’])
d. Display the total number of rows and columns in the data frame
Ans:- print(df.shape)
e. Display the column “Gender”.
Ans: print(df[‘Gender’])
E. Competency-Based Questions
1. Help Priya to differentiate the given information into various data types of Python.
Name of the student, email id, student id, marks in 5 subjects which can be changed
at any point, 3 extra subjects chosen which cannot be changed later.
Ans: The different datatypes of Python for the given information are:
a. Name of the student-String type
b. Email id-String type
c. Student id-Integer type
d. Marks in 5 subjects which can be changed at any point-List type
e. Extra 3 subjects which cannot be changed later-Tuple type.
2. For any analysis to be done, a huge amount of data needs to be collected and stored in a proper format. Rohan has stored the information in a delimited file that stores tabular data which is separated by comma. Which type of file is Rohan using?
Ans: CSV file (Comma Separated Values)
3. Athrav is confused about the different libraries of Python. Help him choose the correct library for the following tasks.
a. Data manipulation and aggregation functionalities
b. Numerical computing
c. Mathematical operations like square root, cosine values
d. Machine learning and statistical modeling
Ans: a. pandas
b. numpy
c. math
d. scikit-learn
4. Rohit wants to input the data about the runs scored by his 50 classmates. Which is the most appropriate loop to be used in this case?
Ans: ‘for’ loop
5. Samhita needs guidance to identify these concepts related to Python. As a Python language expert, help her.
a. ___ is inserted if a corresponding value for a column is missing.
Ans:- NaN
b. ____ is used when we need to work on multiple columns at a time.
Ans:- Data Frame
c. Unordered set of comma-separated key:value pairs within braces {} is known as ______.
Ans:- dictionary.
d. ______ are symbols or keywords that perform operations on operands to produce a result.
Ans:- Operators
e. ______ is a dataset which is classic and widely used in ML, particularly for classification tasks.
Ans:- Iris dataset
f. It is a type of supervised learning algorithm used for classification tasks.
Ans:- KNN – K nearest neighbors