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

AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
3K
Back
Top