Python Python-What does this code signifies in relation to boolean logic?

Click For Summary
The discussion centers around a Python code snippet that uses conditional statements to categorize age. It highlights how boolean values and comparisons are essential for controlling program flow with if, elif, and else statements. The example provided, while deemed confusing by some, serves to illustrate how these comparisons yield True or False outcomes that dictate the program's behavior. There is a suggestion that the complexity may arise from the timing of introducing functions in learning materials. The conversation also emphasizes the value of self-learning in Python, with participants sharing experiences of trial and error, utilizing resources like StackExchange, and reviewing others' code to enhance their understanding.
shivajikobardan
Messages
637
Reaction score
54
Code:
my_age = 10

if my_age >= 100:
  print("One hundred years old! Very impressive.")
elif my_age <= 3:
  print("Awwww. Just a baby.")
else:
  print("Ah - a very fine age indeed")

https://www.fullstackpython.com/blog/python-basic-data-types-booleans.html
Article says-:
Booleans are used in your code to make it behave differently based on current conditions within your program. You can use boolean values and comparisons in conjunction with the if, elif, and else keyoards as one means to achieve this.
 
Technology news on Phys.org
This is a weird example, I agree, but I think their idea is that the comparisons will resolve to True/False booleans which will then dictate the conditional flow.
 
exactly very confusing example. I am tired of self learning. always stumbled upon these types of things.
 
What is confusing about it? If you don't like it, consider this:
Python:
def age_class(my_age):
  if my_age >= 100:
    print("One hundred years old! Very impressive.")
  elif my_age <= 3:
    print("Awwww. Just a baby.")
  else:
    print("Ah - a very fine age indeed")
and a call age_class(10) to the interpreter. Maybe by the time this example is encountered the course has not covered functions yet, and the authors want to provide a code fragment that compiles and runs correctly.
 
I think self-learning for Python is actually much better than formal courses. I've spent the last 2 years focused heavily on Python and a lot of it is just trial and error, reading StackExchange, and reading others' code. Happy to take a look at more of these questions though.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K