Python - tips & tricks

или как да опростим работата си като програмисти

За мен

 

  • Софтуерен разработчик  @
  • Студент "Информатика" @ НБУ
  • Използвам Python всеки ден.

@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 (речник)

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

Как да проверим дали в речника се съдържат някакви елементи? 🤔

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_dict[0]
'a'

>>> my_dict[1]
2

>>> my_dict[2]
None

list (списък)

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

>>> my_dict[0]
'a'

>>> my_dict[1]
2

>>> my_dict[2]
None

Как да итерираме елементите на списъка и техните индекси? 🤔

list (списък)

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

>>> my_dict[0]
'a'

>>> my_dict[1]
2

>>> my_dict[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_dict[0]
'a'

>>> my_dict[1]
2

>>> my_dict[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_dict[0]
'a'

>>> my_dict[1]
2

>>> my_dict[2]
None
for index, element in enumerate(my_list):
    print(index, element)
    
# 0 'a'
# 1 2
# 2 None

Python - Tips & Tricks (BG)

By Ventsislav Tashev

Python - Tips & Tricks (BG)

Career Show 2020 presentation

  • 802