Python Forum
Doubt about conditionals in Python.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doubt about conditionals in Python.
#5
(Mar-29-2023, 06:01 AM)buran Wrote:
(Mar-29-2023, 01:43 AM)Carmazum Wrote: Python only interprets the first statement it detects as True.
To be precise, it works top down until (and if) it reach a if/elif that is evaluated as True or eventually (if present) else. That may have effect, because some code may be executed even if the condition as whole is False. In other words, code may be executed while evaluating the condition, even if the result is False and the respective code block under that condition is not executed.
Simplified example

def spam():
    print('Inside spam') # just to show that function has been called
    return False


if spam():
    print('spam is True')
else:
    print('spam is False')
output

Output:
Inside spam spam is False

Oh, so, Python does interpret the code it finds even if it is False. The detail is that, if it is False, it will not execute the code inside the conditional.

Did I understand correctly? Sorry if I didn't, I haven't programmed anything for a while. Sad

Also, sorry for the late reply! (>_<)
Reply


Messages In This Thread
Doubt about conditionals in Python. - by Carmazum - Mar-28-2023, 08:00 AM
RE: Doubt about conditionals in Python. - by buran - Mar-29-2023, 06:01 AM
RE: Doubt about conditionals in Python. - by Carmazum - Mar-30-2023, 09:18 PM
RE: Doubt about conditionals in Python. - by buran - Mar-31-2023, 05:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple python doubt mohamedrabeek 2 886 Mar-26-2023, 07:24 PM
Last Post: deanhystad
  conditionals based on data frame mbrown009 1 1,005 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,518 May-06-2022, 05:16 PM
Last Post: Mark17
  Nested conditionals vs conditionals connected by operators dboxall123 8 3,301 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  doubt about python tkinter and sqlite3 LONDER 2 2,300 Aug-14-2021, 08:48 AM
Last Post: ibreeden
  Nested Conditionals shen123 3 2,803 Jul-28-2021, 08:24 AM
Last Post: Yoriz
  Python Doubt csrlima 5 2,779 Jan-23-2021, 12:23 AM
Last Post: csrlima
  Invalid syntax using conditionals if - else jperezqu 1 2,434 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  conditionals with boolean logic?? ridgerunnersjw 3 2,126 Sep-26-2020, 02:13 PM
Last Post: deanhystad
  two conditionals with intermediate code Skaperen 5 3,014 Jul-12-2020, 07:18 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020