Module 2 Data Types, Operators, Variables Assignment
Module 2 Data Types, Operators, Variables Assignment
Data Types
Please implement it by using Python.
1. Construct 2 lists containing all the available data types (integer, float, string, complex and
Boolean) and do the following..
a. Create another list by concatenating above 2 lists
Answer:
Input:
line1 = [5, 8, 4.5, 'temple', 2+3j, False]
line2 = [2, 7, 3.8, 'celebration', 4+6j, True]
line3 = line1 + line2
print(line3)
Output:
[5, 8, 4.5, 'temple', (2+3j), False, 2, 7, 3.8, 'celebration', (4+6j), True]
Output:
2. Create 2 Sets containing integers (numbers from 1 to 10 in one set and 5 to 15 in other set)
a. Find the common elements in above 2 Sets.
Answer:
Input:
label = {1,2,3,4,5,6,7,8,9,10}
extend = {5,6,7,8,9,10,11,12,13,14,15}
common = label & extend #or
common_1 = label.intersection(extend)
print(common)
print(common_1)
Output:
{5, 6, 7, 8, 9, 10}
{5, 6, 7, 8, 9, 10}
3. Create a data dictionary of 5 states having state name as key and number of covid-19 cases as
values.
a. Print only state names from the dictionary.
Answer:
Input:
state = {'Kerala': 2456, 'Mumbai': 5424, 'Pune': 4315, 'Tamilnadu':3452, 'Punjab': 2635 }
print(state.keys())
Output:
dict_keys(['Kerala', 'Mumbai', 'Pune', 'Tamilnadu', 'Punjab'])
Operators
Please implement by using Python
B. “When I divide 5 with 3, I get 1. But when I divide -5 with 3, I get -2”—How would you justify
it?
Answer:
Input:
print(5//3)
print(-5//3)
Output:
1
-2
Reason:
The Division of operands where the result is the quotient in which the digit after the decimal
point are removed. But if the operands is negative the result is floored (i.e., Rounded away from
zero (toward negative infinity))
A. a/=b
Input:
a=5
b=3
c = 10
a/=b
print(a)
Output:
1.6666666666666667
Output: 1.66666666667
B. c*=5
Answer:
Input:
a=5
b=3
c = 10
c*=5
print(c)
Output:
50
# c = c*5 = 10*5 = 50
Output: 50
2. A. How to check the presence of an alphabet ‘S’ in the word “Data Science”.
Answer:
#Yes, we can check the presence of an alphabet 'S' in the word "Data Science" using the
membership operator
Output:
True
Variables
Please implement by using Python
b. 5age=55
Answer:
No, it cannot.
b. age@1=100
Answer:
No, it cannot.
Value 100 is not stored in the memory pointer of variable age@1. It shows invalid
syntax error.
Variable assigning rules used in this case as follows:
Variable should not use special characters except underscore.
Answer:
We are define variables, in some place we do not want to use that variable. So, ‘del’
statement is used to delete variable or objects in python. It will completely remove the
variable in the variable list.
For example:
jam = 58