Zanurkuj w Pythonie/Operatory and i or: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
m tekst po angielsku
 
Rofrol (dyskusja | edycje)
mNie podano opisu zmian
Linia 1:
W Pythonie, and i są przeprowadzają operacje logiczne jak mógłbyś oczekiwać, ale nie zwracają wartości logicznych; zamiast tego zwracają jedną z aktualnie porównywanych wartości.
 
'''Przykład 4.15. IntroducingPoznaj and'''
 
In Python, and and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing.
 
'''Przykład 4.15. Introducing and'''
 
>>> 'a' and 'b' #(1)
Linia 12 ⟶ 10:
'c'
 
# WhenPodczas usingużywania and, valueswartości are evaluatedwyliczanie inw akontekście booleanlogicznym contextod fromlewej leftdo to rightprawej. 0, <nowiki>''</nowiki>, [], (), {}, andi None are false in afałszem booleankontekście contextlogicznym; everythingwszystko elseinne isjest trueprawdą. WellCóż, almostprawie everythingwszystko. ByDomyślnie default,instancje instancesklasy of classesprawdą arew truekontekście inlogicznym, aale booleanmożesz context,zdefiniować butspecjalne youmetody canw defineswojej specialklasie methodsaby inklasa yourwyliczana classbyła tojako makefałsz. anNauczysz instancesię evaluatewszystkiego too false.klasach You'lli learnspecjalnych allmetodach aboutw classesRozdziale and[[Python:Obiekty speciali methodsklasy|Obiekty ini Chapter 5klasy]]. If all values are true in a boolean context, and returns the last value. In this case, and evaluates 'a', which is true, then 'b', which is true, and returns 'b'.
# If any value is false in a boolean context, and returns the first false value. In this case, '' is the first false value.
# All values are true, so and returns the last value, 'c'.