Skip to content

use pythonic if #2

@sanan-fataliyev

Description

@sanan-fataliyev

Bunu gördüm:

https://github.com/HuseynM/algorithms/blob/572a11c2593c244889f32557ada74bb369a10724/src/operators_and_expressions/operators_and_expressions.py#L8

Deməli pythonda if (else if, while) təkcə boolean qiymətlə yox, başqa tip qiymətlərlə də çalışa bilir (təxminən jsdəki kimi).

məsələn:

ls = [1,2,3]
if ls:
    print('not empty')

balance = 0
if not balance:
    print('dial *150# yes')

halında şərtlər ödənilmiş sayılacaq.
boolean-a çevirmə avtomatik aparılır.

bəzi nümunələr:

>>> bool(None)
False
>>> bool(0)
False
>>> bool('')
False
>>> bool([])
False
>>> bool(3)
True
>>> bool(-4)
True
>>> bool(['some', 'elements'])
True
>>> bool('i am exist!')
True

python "Explicit is better than implicit" desə də, bu üsul çox praktikdi və çox istifadə olunur.
Tasklarda bucür yazmağa çalış.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions