or how to simplify our daily job as programmers
@v_tashev
wencakisa
hacksoft.io
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
...
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
How we can check whether there are any items in the dict? 🤔
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
>>> my_dict.keys()
['key1', 'key2', 'key3']
>>> empty_dict.keys()
[]
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0:
print('Dict is not empty!')
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
>>> my_dict.values()
['value', 2, None]
>>> empty_dict.values()
[]
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0:
print('Dict is not empty!')
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
>>> my_dict
{
'key1': 'value',
'key2': 2,
'key3': None
}
>>> empty_dict
{}
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
if len(my_dict) > 0:
print('Dict is not empty!')
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
if len(my_dict) > 0
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
if len(my_dict) > 0
>>> bool(0)
False
>>> bool(1)
True
>>> bool(-42)
True
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
if len(my_dict) > 0
if len(my_dict):
print('Dict is not empty!')
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
if len(my_dict) > 0
if len(my_dict)
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict) > 0
if len(my_dict)
>>> bool(0)
False
>>> bool([])
False
>>> bool('')
False
if len(my_dict.values()) > 0
>>> my_dict = {
'key1': 'value',
'key2': 2,
'key3': None
}
>>> my_dict['key1']
'value'
>>> my_dict['key2']
2
>>> my_dict['key3']
None
if len(my_dict.keys()) > 0
if len(my_dict.values()) > 0
if len(my_dict) > 0
if len(my_dict)
if my_dict:
print('Dict is not empty!')
>>> my_list = ['a', 2, None]
>>> my_list[0]
'a'
>>> my_list[1]
2
>>> my_list[2]
None
>>> my_list = ['a', 2, None]
>>> my_list[0]
'a'
>>> my_list[1]
2
>>> my_list[2]
None
How to iterate over the list's elements, along with their indices? 🤔
>>> my_list = ['a', 2, None]
>>> my_list[0]
'a'
>>> my_list[1]
2
>>> my_list[2]
None
index = 0
while index < len(my_list):
print(index, my_list[index])
index += 1
# 0 'a'
# 1 2
# 2 None
>>> my_list = ['a', 2, None]
>>> my_list[0]
'a'
>>> my_list[1]
2
>>> my_list[2]
None
range(3)
# [0, 1, 2]
for index in range(len(my_list)):
print(index, my_list[index])
# 0 'a'
# 1 2
# 2 None
>>> my_list = ['a', 2, None]
>>> my_list[0]
'a'
>>> my_list[1]
2
>>> my_list[2]
None
for index, element in enumerate(my_list):
print(index, element)
# 0 'a'
# 1 2
# 2 None