List manipulation - membership operator

# List manipulation


"""

The second year of engineering offers the following subjects in two departments. 

CSE = ["PQT", "ADC", "SE", "DPSD", "DS", "OOP"]

IT = ["OS", "PQT", "DS", "SE", "OOP", "SS"]


Write a program to create subject list for the two departments.  

Also find 

1. Number and names of subjects in CSE

2. Number and names of subjects in IT

3. Names of subjects in CSE only

4. Names of subjects in IT only

5. Number and names of subjects in both CSE and IT

6. Number and names of subjects for either CSE or IT, but not in both

7. Number and names of all subjects.

"""


# Create List

# CSE = ["PQT", "ADC", "SE", "DPSD", "DS", "OOP"]

# IT = ["OS", "PQT", "DS", "SE", "OOP", "SS"]


CSE = []

print("Enter subjects for CSE : ")

for count in range(0,6):

sub = input()

CSE.append(sub)

IT = eval(input("Enter subjects for IT : "))


# 1. Number and names of subjects in CSE

print("Number of subjects in CSE = ", len(CSE))

print("Subjects in CSE : ")

for sub in CSE:

print(sub, end=" ")

print()


# 2. Number and names of subjects in IT

print("Number of subjects in IT = ", len(IT))

print("Subjects in IT : ")

print(", ".join(IT))


# 3. Names of subjects in CSE only

print("Names of subjects in CSE only")

for sub in CSE:

if sub not in IT:

print(sub)


# 4. Names of subjects in IT only

print("Names of subjects in IT only")

for sub in IT:

if sub not in CSE:

print(sub)


# 5. Number and names of subjects in both CSE and IT

print("Number and names of subjects in both CSE and IT")

for sub in CSE:

if sub in IT:

print(sub)


# 6. Number and names of subjects for either CSE or IT, but not in both

List1=[]


for sub in IT:

if sub not in CSE:

List1.append(sub)

for sub in CSE:

if sub not in IT:

List1.append(sub)

print("Number of subjects for either CSE or IT, but not in both = ", len(List1))

print(List1)


# 7. Number and names of all subjects.

Allsub=CSE

for sub in IT:

if sub not in Allsub:

Allsub.append(sub)

print("Number and names of all subjects : ", len(Allsub))

string1 = ", ".join(Allsub)

print(string1)



"""

Sample output

>python 4_List.py

Enter subjects for CSE :

PQT

ADC

SE

DPSD

DS

OOP

Enter subjects for IT : ['OS','PQT','DS','SE','OOP','SS']

Number of subjects in CSE =  6

Subjects in CSE :

PQT ADC SE DPSD DS OOP

Number of subjects in IT =  6

Subjects in IT :

OS, PQT, DS, SE, OOP, SS

Names of subjects in CSE only

ADC

DPSD

Names of subjects in IT only

OS

SS

Number and names of subjects in both CSE and IT

PQT

SE

DS

OOP

Number of subjects for either CSE or IT, but not in both =  4

['OS', 'SS', 'ADC', 'DPSD']

Number and names of all subjects :  8

PQT, ADC, SE, DPSD, DS, OOP, OS, SS

""" 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu