Dict & Set data type

Dic data type

In Python, dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Use a dictionary data type to store data as a key-value pair.

The disctionary type is represented using a dict class. For example, if you want to store the name and roll number of all students, then you can use the dict type.

In a dictionary, duplicate keys are not allowed, but the value can be duplicated. If we try to insert a value with a duplicated key, the old value will be replaced with the new value.

Dictionary has some characteristics which are listed below:

  1. A heterogeneous (i.e. str, list, tuple) elements are allowed for both key and value in a dictionary. But an object can be a key in a dictionary if it is hashable.
  2. The dictionary is mutable which means we can modify its items
  3. Dictionary is unordered so we can't perform indexing and slicing

We can create a dictionary using the two ways:

  1. By enclosing key and values in the curly brackets { }
  2. Using a dict () class.

Example dictionary creation and manipulation

# create a dictionary
dict = {1: "John", 2: "Jenny", 3: "Jess"}

# display dictionary
print(dict) # {1: 'John', 2: 'Jenny', 3: 'Jess'}
print(type(dict)) # class 'dict'

# create a dictionary using a dict class
dict2 = dict({1: "John", 2: "Jenny", 3: "Jess"})

# display dictionary
print(dict2) # {1: 'John', 2: 'Jenny', 3: 'Jess'}
print(type(dict2)) # class 'dict'

# access value using a key name
print(dict2[1]) # John

# change the value of a key
dict2[1] = "Johnson"
print(dict2[1]) # Johnson

Set data type

In Python, a set is an unordered collection of data items that are unique. In other words, Python Set is a collection of elements (or objects) that contains no duplicate elements.

In Python, the set data type used to represent a group of unique elements as a single entity. For example, if we want to store student ID numbers, we can use the set datat type.

The set data type in Python is represented using a set class.

We can create a set using the two ways:

  1. By enclosing values in the curly brackets { }
  2. Using a set() class.

The set data type has the following characteristics:

  1. It is mutable which means we can change set items.
  2. Duplidate elements are not allowed.
  3. Heterogeneous (values of all data types) elements are allowed.
  4. Insertion order of elements is not preserved, so we can't perform indexing on a set.

Example set creation and manipulation

# create a set using curly brackets {,}
set = {10, 20, "John"}
print(set) # {10, 20, 'John'}
print(type(set)) # class 'set'

# create a set using set class
set2 = set({10, 20, "Jenny"})
print(set2) # {10, 20, 'Jenny'}
print(type(set2)) # class 'set'

# add element to set
set2.add(30)
print(set2) # {10, 20, 'Jenny', 30}

# remove element from set
set2.remove(10)
print(set2) # {20, 'Jenny', 30}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值