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

Module Assignment 2 Data Types, Operators, Variables Assignment - Answer

Uploaded by

Yuva Teja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Module Assignment 2 Data Types, Operators, Variables Assignment - Answer

Uploaded by

Yuva Teja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

collab link:Google Colab

https://colab.research.google.com/drive/1aK89thB9E22qU3rksOfTERLJ6DEk43fo?usp=sharing

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

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

c. Print the list in reverse order.

list1 = [1, 4, 'Hello', 2 + 6j, True]

list2 = [5, 9.71, 'World', 3 - 2j, False]

concated_list = list1+list2

print(concated_list)

frequency = {}

for element in set(concated_list):

frequency[element] = concated_list.count(element)

print("Frequency of each element:", frequency)

reversed_list = concated_list[::-1]

print("Reversed List:", reversed_list)

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.

b.Find the elements that are not common.

c.Remove element 7 from both the Sets

set1 = set(range(1, 11))

set2 = set(range(5, 16))

print("Set 1:", set1)

print("Set 2:", set2)


common_elements = set1.intersection(set2)

print("Common Elements:", common_elements)

not_common_elements = set1.symmetric_difference(set2)

print("Elements Not Common:", not_common_elements)

set1.discard(7)

set2.discard(7)

print("Set 1 after removing 7:", set1)

print("Set 2 after removing 7:", set2)

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.

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

covid_cases = { "Maharashtra": 1000000, "Karnataka": 850000, "Tamil Nadu": 600000,


"Uttar Pradesh": 700000, "Delhi": 500000 }

print("State Names:", list(covid_cases.keys()))

covid_cases["Nepal"] = 200000

print("Updated COVID-19 Cases Dictionary:", covid_cases)

Operators
Please implement by using Python

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

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?

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

x = 399

y = 543

result = x * 31 + y * 15

print("Result of equation:", result)


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?

positive_div = 5 // 3

negative_div = -5 // 3

print("5 // 3 =", positive_div)

print("-5 // 3 =", negative_div)

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

a=5

b=3

c = 10

a /= b

print("a after a /= b:", a)

c *= 5

print("c after c *= 5:", c)

3. A. How to check the presence of an alphabet ‘S’ in the word “Data Science” . B. How can
you obtain 64 by using numbers 4 and 3

A. How to check the presence of an alphabet ‘S’ in the word “Data Science”

# Check for 'S' in "Data Science"

word = "Data Science"

is_present = 'S' in word

print("Is 'S' in 'Data Science'? :", is_present)

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

result = 4 ** 3

print("4 ** 3 =", result)


Variables
Please implement by using Python

1.What will be the output of the following (can/cannot): a.Age1=5 b.5age=55

a. Valid variable name

Age1 = 5 print("Age1 can be defined with value:", Age1)

b.5age=55

#not valid

2.What will be the output of following (can/cannot): a.Age_1=100 b.age@1=100

a. Valid variable name

Age_1 = 100

print("Age_1 can be defined with value:", Age_1)

3.How can you delete variables in Python ?

# Deleting variables

Age1 = 5

print("Age1 before deletion:", Age1)

del Age1

You might also like