Skip to main content

Python program to Use and demonstrate basic data structures

Description

This code is a collection of four programs - one that performs mathematical operations, one that manipulates strings, one that manipulates lists, and one that manipulates dictionaries. The programs demonstrate various operations such as addition, subtraction, multiplication, division, printing, appending, popping, deleting, and modifying elements in a list and dictionary.

Code

program1a.py
# Program 1 A [i] 
x = 8.0
y = 4.0
sum = x + y
print(x+y)
sub = x - y
print(x - y)
multi = x * y
print(x * y)
div = x/y
print(x/y)
print(abs(x))
print(x**y)

# ---------------------------------------------
# String Program - Program 1 A [ii]
string1 = "Hello" print("initial string: ")
print(string1)
string1 = "Welcome"
print("Updated String: ")
print(string1)
text = 'welcome to mlbp'
text2 = "good morning students"
print("converted string")
print(text.upper())
print(text2.upper())
print(text.title())
print("original string")
print(text)

# ---------------------------------------------
# List Program - Program 1 A [iii]
list1=[1,2,3,4,"Python"]
print(list1) list1.append(2)
print(list1) a = list1.pop(4)
print(a) print(list1[:])
print(list1[0:2])
print(list1[-1:-3])

# ----------------------------------------------
# Dictionary Program - Program 1 A [iv]
dict1 = {1: 'Java Script', 2: 'xml ', 3: 'oops', 4: 'html'}
print(dict1)
dict1[1] = 'python'
print(dict1)
dict1[5] = 'Shambhavi '
print(dict1)
del dict1[1]
print(dict1)
print(dict1.popitem())
print(dict1)
print(dict1.keys())
print(dict1.values())
# ----------------------------------------------

Explanation of above code

  • In this program, we are performing a variety of operations on data types such as numbers, strings, lists, and dictionaries.

Mathematical operations:

  • We start by performing some basic mathematical operations on two numbers, 8 and 4. We add them to get 12, subtract them to get 4, multiply them to get 32, and divide them to get 2. We also calculate the absolute value of 8 (which is just 8) and raise it to the power of 4 (which is 4096).

String operations:

  • We then move on to string operations. We start with the word "Hello" and say it out loud. Then, we change the word to "Welcome" and say it out loud again. We also convert two sentences to uppercase and title case. Uppercase conversion makes all the letters in a sentence capital, while title case capitalizes the first letter of each word.

List operations:

  • We then move on to list operations. We start with a list containing the elements 1, 2, 3, 4, and "Python". We append the number 2 to the list, pop the element "Python" from the list, and slice the list to take the first two elements and the elements from the last to the third last.

Dictionary operations:

  • Finally, we work with dictionaries. We start with a dictionary that has keys 1, 2, 3, and 4, and their corresponding values 2, 3, 4, and "Python". We update the value of the key 1 to "Python", add a new key-value pair (key 5 with the value "Shambhavi"), and delete the key-value pair with the key 1. We also retrieve all the keys and values from the dictionary.

Conclusion

  • This program provides a good overview of some of the basic operations that can be performed on different data types in Python.

Learn more

Reference