0% found this document useful (0 votes)
126 views

Module 2 Data Types, Operators, Variables Assignment

Uploaded by

praveen kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Module 2 Data Types, Operators, Variables Assignment

Uploaded by

praveen kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Module – 2 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]

b. Find the frequency of each element in the concatenated list.


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.count(5))
print(line3.count(8))
print(line3.count(4.5))
print(line3.count('temple'))
print(line3.count(2+3j))
print(line3.count(False))
print(line3.count(2))
print(line3.count(7))
print(line3.count(3.8))
print(line3.count('celebration'))
print(line3.count(4+6j))
print(line3.count(True))

Output:

© 360DigiTMG. All Rights Reserved.


1
1
1
1
1
1
1
1
1
1
1
1

c. Print the list in reverse order.


Answer:
Input:
line1 = [5, 8, 4.5, 'temple', 2+3j, False]
line2 = [2, 7, 3.8, 'celebration', 4+6j, True]
line3 = line1 + line2
line3.reverse()
print(line3)
Output:
[True, (4+6j), 'celebration', 3.8, 7, 2, False, (2+3j), 'temple', 4.5, 8, 5]

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}

© 360DigiTMG. All Rights Reserved.


b. Find the elements that are not common.
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}
not_common = label.symmetric_difference(extend)
print(not_common)
Output:
{1, 2, 3, 4, 11, 12, 13, 14, 15}

c. Remove element 7 from both the 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}
label.remove(7)
extend.remove(7)
print(label)
print(extend)
Output:
{1, 2, 3, 4, 5, 6, 8, 9, 10}
{5, 6, 8, 9, 10, 11, 12, 13, 14, 15}

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'])

b. Update another country and its covid-19 cases in the dictionary.


Answer:
Input:

© 360DigiTMG. All Rights Reserved.


state = {'Kerala': 2456, 'Mumbai': 5424, 'Pune': 4315, 'Tamilnadu':3452, 'Punjab': 2635 }
state['West Bengal'] = 3214
print(state)
Output:
{'Kerala': 2456, 'Mumbai': 5424, 'Pune': 4315, 'Tamilnadu': 3452, 'Punjab': 2635, 'West
Bengal': 3214}

Operators
Please implement by using Python

1. A. Write an equation which relates 399, 543 and 12345


Answer:
Input:
abc=12345
bac=543
c_1=abc%bac
print(c_1)
Output:
399

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

2. a=5, b=3, c=10.. What will be the output of the following:

A. a/=b

© 360DigiTMG. All Rights Reserved.


Answer:

Input:
a=5
b=3
c = 10
a/=b
print(a)
Output:
1.6666666666666667

# a= a/b = 5/3 = 1.66666666667

# The 1.66666667 float value is stored in memory pointer of a variable.

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

# The 50 integer value is stored in a memory pointer of c variable.

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

© 360DigiTMG. All Rights Reserved.


Input:
text = "Data Science"
print('S' in text)

Output:
True

B. How can you obtain 64 by using numbers 4 and 3.


Answer:
#Yes, we can obtain 64 by numbers 4 and 3 using exponential operator.
Input:
ab = 4**3
print(ab)
Output:
64

Variables
Please implement by using Python

1. What will be the output of the following (can/cannot):


a. Age1=5
Answer:
Yes, it can.
Value 5 is stored in the memory pointer of variable Age1.
Variable assigning rules used in this case as follows:
 Alpha numeric
 Start with alphabet
 Case sensitive
 No special characters assigned
 Space not assigned in between variables
 Keywords not assigned

b. 5age=55
Answer:
No, it cannot.

© 360DigiTMG. All Rights Reserved.


Value 55 is not stored in the memory pointer of variable 5age. It shows invalid
syntax error.
Variable assigning rules used in this case as follows:
 Variable can assign with alpha numeric but we should not assign start with
number.

2. What will be the output of following (can/cannot):


a. Age_1=100
Answer:
Yes, it can.
Value 100 is stored in the memory pointer of variable Age_1.
Variable assigning rules used in this case as follows:
 Alpha numeric
 Start with alphabet
 Case sensitive
 No special characters assigned except underscore
 Space not assigned in between variables
 Keywords not assigned

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.

3. How can you delete variables in Python ?

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

© 360DigiTMG. All Rights Reserved.


del(jam)

© 360DigiTMG. All Rights Reserved.

You might also like