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

Module 2 Data Types, Operators, Variables Assignment

Uploaded by

Ritika Awasthi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Module 2 Data Types, Operators, Variables Assignment

Uploaded by

Ritika Awasthi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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
b. Find the frequency of each element in the concatenated list.
c. Print the list in reverse order.

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.

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.

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?

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

A. a/=b

B. c*=5

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 .

© 360DigiTMG. All Rights Reserved.


Variables
Please implement by using Python

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


a. Age1=5
b. 5age=55

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


a. Age_1=100
b. age@1=100

3. How can you delete variables in Python ?

1. list1 = [1, 2.5, "Hello", 3 + 4j, True]

list2 = [3, 4.5, "World", 5 + 6j, False]

a. List3 = list1 + list2


print("Concatenated list:", List3)

b. frequency = {}
for element in concatenated_list:
if element in frequency:
frequency[element] += 1

else:

frequency[element] = 1

print("The frequency is",frequency)

c. reverse_list =List3[::-1]

© 360DigiTMG. All Rights Reserved.


2. set1 = set(range(1, 11))
set2 = set(range(5, 16))
a. common_elements = set1.intersection(set2)
print("Common elements:", common_elements)

b. not_common_elements = set1.symmetric_difference(set2)

print("Elements not common:", not_common_elements)

c. set1.discard(7)
set2.discard(7)
print("Set 1 after removing 7:", set1)
print("Set 2 after removing 7:", set2)

3. covid_cases = {"State1": 1000, "State2": 2000, "State3": 1500,"State4": 3000,"State5": 500}

a. print("State names from the dictionary:")

for state in covid_cases:


print(state)

b. covid_cases["State6"] = 50000

print(covid_cases)

Operators

1. a. To write an equation that relates 399, 543, and 12345, we can use the concept of arithmetic
operations such as addition, subtraction, multiplication, and division. Here's one possible equation:

(399×3)+543=12345
This equation states that if we multiply 399 by 3 and then add 543 to the result, we get 12345.

© 360DigiTMG. All Rights Reserved.


b. The statement "When I divide 5 by 3, I get 1. But when I divide -5 by 3, I get -2" can be
justified by understanding how integer division works in Python or many programming
languages.

In Python, when you perform integer division (using the // operator) between two positive
integers, the result is the largest integer that is less than or equal to the true mathematical
result of the division.

So, 5/3 mathematically equals 1.666..., but in integer division, it equals 1.

However, when you perform integer division with a negative numerator (like -5), the result
is the smallest integer greater than or equal to the true mathematical result of the division.

So, -5/3mathematically equals -1.666..., but in integer division, it equals -2.

This behavior ensures that the result of integer division maintains the properties of floor
and ceiling functions, which are common mathematical operations.

2. a=a/b

a=5/3 which means dividing 5 by 3 which gives: 1.6667

so a=1.6667

and,

c=c*5

where c given is 10, so, c=10*5=50

3. a. word = "Data Science"

if 'S' in word:

print("Alphabet 'S' is present in the word.")

else:

© 360DigiTMG. All Rights Reserved.


print("Alphabet 'S' is not present in the word.")

3.b. You can obtain 64 by using numbers 4 and 3 :

((4*4)*(4-3)) =64

Variables

1. In Python, variable names must follow certain rules:

Variable names must begin with a letter (a-z, A-Z) or an underscore (_).

The remaining characters in the variable name can be letters (a-z, A-Z), digits (0-9), or underscores
(_). Variable names are case-sensitive.

Now, let's evaluate each case:

a. Age1 = 5

This variable name is valid because it starts with a letter ('A') and is followed by letters and a digit.

b. 5age = 55

This variable name is invalid because it starts with a digit ('5'). Variable names cannot start with a
digit; they must start with a letter or an underscore.

2. Age_1 = 100

This variable name is valid because it starts with a letter ('A') and is followed by letters, a digit, and an
underscore.

age@1 = 100

This variable name is invalid because it contains a special character ('@'). Variable names cannot contain
special characters other than underscores (_)

© 360DigiTMG. All Rights Reserved.


4. You can delete variables using the del statement.
Eg:
X=10
del x

© 360DigiTMG. All Rights Reserved.

You might also like