Unit 1 - Lesson 7

7.1.1. Introduction to Dictionary

(a) Dictionary is a Python data type to store multiple values.
(d) Keys of the dictionary cannot be changed.

7.1.2. Understanding Dictionary

mydict = {"game":"chess","dish":"chicken","place":"home"}
print(mydict['game'])
print(mydict['dish'])
print(mydict['place'])
mydict['game'] = "cricket" # change game chess to cricket using respective key
print(mydict['game'])

7.1.3. Accessing Elements of Dictionary

# Taking input from the user for keys and values
key1 = input("key1: ")
value1 = input("value1: ")

key2 = input("key2: ")
value2 = input("value2: ")

key3 = input("key3: ")
value3 = input("value3: ")

# Construct the dictionary with the given key and value
mydict={key1:value1, key2:value2, key3:value3}
# Print the values of dictionary using keys
print(mydict)
print(mydict[key1])
print(mydict[key2])
print(mydict[key3])

7.2.1. Data Type Conversion - int,float

a = int(input("Enter a value: "))
b = input("Enter b value: ")

# Convert "a" to floating point value.
a= float(a)
#Convert "b" to floating point value.
b = float(b)
print(a)
print(b)

7.2.2. Data Type Conversion - ord(),hex(),oct and complex()

a = input("Enter character: ")
b = int(input("Enter an integer: "))

# Calculate and print the Unicode code point of 'a'
print(ord(a))
# Calculate and print the hexadecimal representation of 'b'
print(hex(b))
# Calculate and print the octal representation of 'b'
print(oct(b))
# Calculate and print the complex number representation of 'b'
print(complex(b))

7.2.3. Data Type Conversion - str(),eval() and chr()

a = int(input("Enter an integer: "))

# Calculate and print the character representation of 'a'
print(chr(a))
# Calculate and print the string representation of 'a'
print(str(a))

7.2.4. Data Type Conversions

tup1 = tuple(input().split(","))
tup2 = tuple(input().split(","))

# print tup1
print(tup1)
# print tup2
print(tup2)
list1 = list(tup1)# convert tup1 to list data type
list2 = list(tup2)# convert tup2 to list data type

print(list1)
print(list2)

dict1 =  dict(zip(list1,list2)) # create a dictionary with the elements of list1 as keys and list2 as values using dict().
print(dict1)
set1 = set(dict1)# convert dict1 to set data type

print(sorted(set1))

Post a Comment