Chapter-5
Python
Data types
Answer Key
I. Complete the following sentences by filling the blanks:
1. Variable is also known as identifier and used to hold value.
2. + operator is used to combine both text and a variable.
3. A Set is a collection which is unordered and unindexed.
4. Python is a dynamically typed language hence we need not define the types of the
variables while declaring it.
5. A Strings is a collection of one or more characters put in a single quote, double-quote or
triple quote.
6. Comparison operators compares the values and return either True or False according to the
condition.
II. Put a tick mark if the statement is right and a X mark if it is wrong.
1. The first character of the variable name must be a number.
2. A tuple is a collection which is ordered and changeable.
3. + operator is used to combine both text and a variable.
4. A sequence can be indexed with negative numbers.
5. Python allows negative indexing for its sequence.
III. Cross Word
F
A E D
T X C
T C L
R E M O V E
N U A P P E N D
D N R
T
IV. Explain the purpose of each arithmetic operator:
1. + = Addition Operator: Adds two operands.
2. - = Subtraction Operator: Subtracts second operand from the first.
3. * = Multiplication Operator: Multiplies both operands.
4. / = Division Operator: Divides numerator by denominator.
5. % = Modulo Operator: Provides the remainder after an integer division.
6. ** = Exponent Operator: Performs exponential (power) calculation on operands
7. // = Floor Division Operator: The division of operands where the result is the quotient in
which the digits after the decimal point are removed.
V. Find the output for the following:
1. Let us assume my_list=['s','u','c','c','e','s']
Q.no Program Output
i. print(len(my_list)) 6
ii. print(my_list[5]) s
iii. my_list.append(‘s’) ['s', 'u', 'c', 'c', 'e', 's', 's']
print(my_list)
Iv my_list.remove(‘u’) ['s', 'c', 'c', 'e', 's', 's']
print(my_list)
v print(my_list[-4]) c
2.
VI. Answer in one word:
1. Which function returns the type of the variable passed?
Ans: type()
2. Which function is used to get the length of a string or list?
Ans: len()
3. Which method is used to remove the last item from a list?
Ans: pop()
VII. Answer the Following
1. How will you change values in tuples?
Ans: Tuple values can be changed by converting it in to list using list() method, then modify
the list and then convert the list back in to tuple using tuple() method.
2. Write a short note on lists.
Ans: A List is a collection which is ordered and changeable. In python, lists are written with
square brackets.
Example: fruits=["Apple", "Banana"]
3. List any 5 keywords used in Python.
Ans:
4. What are the various types of operators that can be used in Python?
Ans: The different types of operators:
• Arithmetic Operator
• Comparison (or Relational) Operator
• Logical Operator
• Assignment Operator
• Bitwise Operator
• Membership Operator
• Identity Operator
Activity Corner
Activity-1
flowers=["iris", "peony", "tulip", "daffodil", "orchid", "daisy"]
a. Explain what will happen if the following line of code is added to the program”
flowers[0]=“bluebell”
Ans: ['bluebell', 'dandelion', 'tulip', 'daffodil', 'orchid', 'daisy']
b. Write a line of code that would replace “daffodil” with “geranium”.
Ans: flower[3]= “geranium”
c. Explain what will happen if the following line of code is added to the program:
flowers[-2]=“lily”
Ans: ['bluebell', 'dandelion', 'tulip', 'daffodil', 'lily', 'daisy']
d. Explain what happens when the following line of code is added to the program:
flowers[8]=“lotus”
Ans: IndexError: list assignment index out of range
e. Write a line of code that would delete the last flower in the list.
Ans:
flowers=["iris", "dandelion", "tulip", "daffodil", "orchid", "daisy"]
flowers.pop()
print(flowers)
f. Write a line of code that would add the flower lavender to the end of list.
Ans:
g. Write the output for
print(flowers[2:4])
Ans: ['tulip', 'daffodil']
print(flowers[:3])
Ans: ['bluebell', 'dandelion', 'tulip']
print(flowers[4:])
Ans: ['lily', 'daisy']
h. Write the line of code to empty the list.
Ans: flowers.clear()
i. Write a line of code to find if “daisy” is in list or not.
Ans: flowers.index(“daisy”)
Activity-2
Complete the blanks by running each piece of code and writing down the data type.
Code Data type of Variable
print(type(“fred”)) <class 'str'>
print(type(1989)) <class 'int'>
print(type(88.45)) <class 'float'>
print(type(True)) <class 'bool'>
print(type(“LittleMount”)) <class 'str'>
Activity-3
1. Write a code to accept two numbers from the user and perform addition, subtraction, multiplication
and division with the two numbers and display the result.
Ans:
num1=float(input("Enter number1: "))
num2=float(input("Enter number2: "))
add=num1+num2
sub=num1-num2
mul=num1*num2
div=num1/num2
print("Addition of two numbers: ", add)
print("Subtraction of two numbers: ",sub)
print("multiplicatio of two numbers: ", mul)
print("division of two numbers: ", div)
Activity-4
1. Write the line of Python code that calculates and prints the answer to the following arithmetic
expressions:
a. 8 to the 4th power
Ans: print(8**4)
b. The sum of 5 and 6 multiplied by the quotient of 34 and 7 using floating point arithmetic
Ans: print((5+6)*(34/7))
Activity-5
Write an assignment statement that stores the remainder obtained from dividing 87 and 8 in the
variable leftover.
Ans:
leftover=87%8
print(leftover)
Activity-6
a. Write a program that asks the user to enter how much they have spent on their school dinner in a
way that the computer can remember as dinner cost.
b. Then make the program to subtract the dinner cost from the money the user had at the start of the
day.
c. Display the result on the screen.
Ans:
dinnerCost=float(input("Enter the money spent for dinner"))
startCost=float(input("Enter the money on the start of the day"))
amountLeft=startCost-dinnerCost
print("Amount saved: ", amountLeft)
Activity-7
Write a program that uses the following variables to calculate the number of minutes in a week:
Variables:
Days Per Week
Hours Per Week
Minutes Per Hour
Ans:
DaysPerWeek=7
HoursPerDay=24
MinutesPerHour=60
MinutesinaWeek=DaysPerWeek*(HoursPerDay*MinutesPerHour)
print("Number of Minutes in a Week: ", MinutesinaWeek)