[go: up one dir, main page]

0% found this document useful (0 votes)
46 views14 pages

Program 1

The document demonstrates basic operations on lists, dictionaries, and tuples in Python. It covers creating, modifying, and deleting elements in lists and dictionaries, as well as performing operations like concatenation and summation on tuples. The output sections illustrate the results of various operations performed on these data structures.

Uploaded by

saranya
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)
46 views14 pages

Program 1

The document demonstrates basic operations on lists, dictionaries, and tuples in Python. It covers creating, modifying, and deleting elements in lists and dictionaries, as well as performing operations like concatenation and summation on tuples. The output sections illustrate the results of various operations performed on these data structures.

Uploaded by

saranya
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/ 14

#PROGRAM: 1 PROGRAM USING ELEMENTARY DATA ITEMS,

LISTS, DICTIONARIES AND TUPLES

#Demonstrating List Operations

#Creating a List

print("#####################################")

print(" DEMONSTRATING LIST OPERATIONS ")

print("#####################################")

list1=[]

print("Blank list:")

print(type(list1))

#creating a list of number

list=[10,20,30,40,50,60]

print("\nList of number:")

print(list)

print("Accessing a element from the list:")

print(list[0])

print(list[3])

#calculating the len()

print("The length of the list and list1:")

print(len(list))

print(len(list1))

#adding of tht element inthe list

list.append(70)
list.append(80)

print("\nAfter addition of three number:")

print(list)

#addition of element at specific position(using insert method)

list.insert(0,'computer science')

print("\nList after insert operation:")

print(list)

#reverse a list

print("Reverse List:")

list.reverse()

print(list)

#removing element for a list

list.remove(30)

list.remove(60)

print("\nList after removal of two element:")

print(list)
OUTPUT:

#####################################

DEMONSTRATING LIST OPERATIONS

#####################################

Blank list:

<class 'list'>

List of number:

[10, 20, 30, 40, 50, 60]

Accessing a element from the list:

10

40

The length of the list and list1:

After addition of three number:

[10, 20, 30, 40, 50, 60, 70, 80]

List after insert operation:

['computer science', 10, 20, 30, 40, 50, 60, 70, 80]
Reverse List:

[80, 70, 60, 50, 40, 30, 20, 10, 'computer science']

List after removal of two element:

[80, 70, 50, 40, 20, 10, 'computer science']

#DEMONSTRATING DICTIONARY OPERATIONS

#Creating a dictionary

print("*********************************")

print(" IMPLEMENTATION OF DICTIONARY ")

print("*********************************")

#Creating an Empty Dictionary

dict={}

print("Empty Dictionary: ")

print(dict)

#creating a dictionary

employee={"Name": "Roja","Age": 22,"Salary":


25000,"Company": "GOOGLE"}

print(type(employee))

print("Printing employee data..")

print(employee)
#To display

print("Name: %s"%employee["Name"])

print("Age: %d"%employee["Age"])

print("Salary; %d"%employee["Salary"])

print("Company: %s"%employee["Company"])

#Get employee details from the user

print("Enter the details of the new employee...")

employee["Name"]=input("name: ")

employee["Age"]=int(input("age: "))

employee["Salary"]=int(input("salary: "))

employee["Company"]=input("company: ")

print("Printing the new data")

print(employee)

#DELETING name and company from the employee data

del employee["Name"]

del employee["Company"]

print("Printing the modified information")

print(employee)

#Deleting dictionary(employee)

print("Deleting the dictionary:", employee)

del employee
print("Lets try to print it again")

print(employee)

#Name employee is not defined


OUTPUT:

**************************************

IMPLEMENTATION OF DICTIONARY

**************************************

Empty Dictionary: {}

<class 'dict'>

Printing employee data..

{'Name': 'Roja', 'Age': 22, 'Salary': 25000, 'Company': 'GOOGLE'}

Name: Roja

Age: 22

Salary; 25000

Company: GOOGLE

Enter the details of the new employee...

name: MINI

age: 26

salary: 50000

company: IBM

Printing the new data:{'Name': 'MINI', 'Age': 26, 'Salary': 50000,


'Company': 'IBM'}

Printing the modified information: {'Age': 26, 'Salary': 50000}

Deleting the dictionary: {'Age': 26, 'Salary': 50000}

Lets try to print it again

Traceback (most recent call last):

File "E:\pRIYA\Roja 1 pg cs\prog1c.py", line 45, in <module>

print(employee)
NameError: name 'employee' is not defined

#Python program to dictionary built-in-functins

print(" IMPLEMENTATION OF DICTIONARY ")

print("*********************************")

#DICTIONARY BUILT IN FUNCTIONS

squares={0:0,1:1,3:9,5:25,7:49,9:81}

print(all(squares))

print(any(squares))

print(len(squares))

print(sorted(squares))

for i in squares:

print(squares[i])

print(squares.popitem())

print(squares)

print(squares.popitem())

print(squares)

#remove all items

squares.clear()

print(squares)
#delete the dictionary itself

del squares

#throws error

print(squares)

OUTPUT:

IMPLEMENTATION OF DICTIONARY
***************************************
False
True

[0, 1, 3, 5, 7, 9]

25

49

81

(9, 81)

{0: 0, 1: 1, 3: 9, 5: 25, 7: 49}

(7, 49)

{0: 0, 1: 1, 3: 9, 5: 25}

{}

Traceback (most recent call last):

File "E:\pRIYA\Roja 1 pg cs\prog1d.py", line 29, in <module>

print(squares)
NameError: name 'squares' is not defined

#Creating Tuples, Nested Tuples, Repeating Tuples Elements

print("*************************")

print(" Working With Tuples ")

print("*************************")

#Creating an empty tuple

empty_tuple=0

print("Empty tuple :",empty_tuple);

#Creating tuple having integers

int_tuple=(4,6,8,10,12,3)

print("Tuple with integer :",int_tuple)

#Creating a tuple having objects of different data types

mixed_tuple=(2,"roja",2.0)

print("Tuple having mixed data types :",mixed_tuple)


#Creating a nested tuples

nested_tuple=("roja",{1,2,3,4},[2,3])

print("Nested tuples :",nested_tuple)

#To shaw repetition in tuples

tuple_1=('python',"tuple")

print("Original tuple :",tuple_1)

#Repeating the tuple elements

tuple_1=tuple_1*3

print("New tuple is :",tuple_1)

#To show how to concatenate tuples reating a tuple

tuple_2=("Python","tuple","ordered","immutable")

#Adding a tuple to the tuple

print("Adding a tuple to the tuples :")

print(tuple_2+(4,5,6))
OUTPUT:

*************************

Working with Tuples

*************************

Empty tuple :0

Tuple with integer : (4, 6, 8, 10, 12, 3)

Tuple having mixed data types : (2, 'roja', 2.0)

Nested tuples : ('roja', {1, 2, 3, 4}, [2, 3])

Original tuple : ('python', 'tuple')

New tuple is : ('python', 'tuple', 'python', 'tuple',


'python', 'tuple')

Adding a tuple to the tuples :('Python', 'tuple', 'ordered', 'immutable',


4, 5, 6)
#Python program to perform concatenation of two string tuples

print(" Working With Tuples(concatenation of string) " )

print(" *********************************************")

import operator

# Intialing and printing tupels

strTup1=("python","learn","web");

strTup2=("programming","coding","developement")

print("The elements of tuples 1 :"+str(strTup1))

print("The elements of tuples 2 :"+str(strTup2))

#Performing concotenation of string tuples

concTup=tuple(map(operator.concat,strTup1,strTup2))

print("The tuple with concotenated string :"+str(concTup))

#Python program tofind the sum pf the tuples of integer values

#Creating and printing the tuples of integer values

myTuple=(2,3,4,5,5)

#Printing original tuple

print("The original tuple is :"+str(myTuple))

#Finding sum of all tuple elements

tupSum=sum(list(myTuple))

#Printing the tuples sum


print("The summation of tuple elements are :"+str(tupSum))

OUTPUT:

Working With Tuples(concotenation of string)

************************************************

The elements of tuples 1 :('python', 'learn', 'web')

The elements of tuples 2 :('programming', 'coding',


'developement')

The tuple with concotenated string :('pythonprogramming',


'learncoding', 'webdevelopement')

The original tuple is :(2, 3, 4, 5, 5)

The summation of tuple elements are :19

You might also like