Python - tips & tricks

or how to simplify our daily job as programmers

Me

 

  • Software Developer  @
  • "Informatics" student @ New Bulgarian University
  • Using Python & Django on a daily basis.

@v_tashev

wencakisa

hacksoft.io

The Zen of Python

>>> 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.
...

dict (ionary)

dict

>>> my_dict = {
  'key1': 'value',
  'key2': 2,
  'key3': None
}

>>> my_dict['key1']
'value'

>>> my_dict['key2']
2

>>> my_dict['key3']
None

dict

>>> 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? 🤔

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()
[]

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:
    print('Dict is not 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
>>> my_dict.values()
['value', 2, None]

>>> empty_dict.values()
[]

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:
    print('Dict is not 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
>>> my_dict
{
    'key1': 'value',
    'key2': 2,
    'key3': None
}

>>> empty_dict
{}

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!')

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

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
>>> bool(0)
False

>>> bool(1)
True

>>> bool(-42)
True

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
if len(my_dict):
    print('Dict is not 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
if len(my_dict)

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

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
if len(my_dict)
if my_dict:
    print('Dict is not empty!')

list

>>> my_list = ['a', 2, None]

>>> my_list[0]
'a'

>>> my_list[1]
2

>>> my_list[2]
None

list

>>> 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? 🤔

list

>>> 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

list

>>> 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

list

>>> 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

Python - Tips & Tricks (EN)

By Ventsislav Tashev

Python - Tips & Tricks (EN)

Career Show 2020 presentation

  • 739