Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Python Logiska Operatorer | Behärska Boolesk Logik i Python
Datatyper i Python

book
Python Logiska Operatorer

I verkliga livet uttrycker nästan alla sina tankar med fraser som "Ja eller Nej", "Definitivt Inte", "Ja, ja och ja!". Python-språket tillåter dig att göra nästan samma sak, men det använder mer formella fraser baserade på uttalandena and, or och not. X och Y kan vara vilket logiskt uttalande som helst (True eller False):

Låt oss titta på koden nedan för att förstå logiken för varje operator:

# Example of `and` operator
x = True
y = False
result = x and y
print('Result of x and y:', result) # Output: False

# Example of `or` operator
result = x or y
print('Result of x or y:', result) # Output: True

# Example of `not` operator
result = not x
print('Result of not x:', result) # Output: False
12345678910111213
# Example of `and` operator x = True y = False result = x and y print('Result of x and y:', result) # Output: False # Example of `or` operator result = x or y print('Result of x or y:', result) # Output: True # Example of `not` operator result = not x print('Result of not x:', result) # Output: False
copy
Uppgift

Swipe to start coding

Föreställ dig att du är en revisor som kontrollerar statusen för en finansiell rapport. Ersätt ___ med True eller False för att matcha följande villkor:

  • Variabeln report_is_valid ska vara True endast om rapporten lämnades in i tid och undertecknades av chefen.
  • Variabeln report_is_rejected ska vara False om rapporten inte lämnades in eller inte granskades av revisorn.

Lösning

# The report was submitted on time and signed by the manager
report_is_valid = True and True

# The report was not submitted or not reviewed by the accountant
report_is_rejected = False or False

# Print results
print(report_is_valid)
print(report_is_rejected)
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
single

single

# The report was submitted on time and signed by the manager
report_is_valid = True and ___

# The report was not submitted or not reviewed by the accountant
report_is_rejected = False or ___

# Print results
print(report_is_valid)
print(report_is_rejected)

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

We use cookies to make your experience better!
some-alt