Grouping commands as one

i dont know if this is the right place to put this, and its kinda hard to explain

pretty much ive just downloaded python and ive just made the command

"if 3 > 2:
print(“good”)

if 2 > 3:
print (“bad”)" just to see if i can actually do anything

i want to know if i can group this as something called like " math " and just type print(math) and get it to come up with the answer

i can explain in further detail if this doesnt make sense but i just want help to see if its possible to group things as one command

They are called “functions”.

I suggest you read a Python tutorial. There ae plenty available.

i dont know if this is the right place to put this,

It is.

Side note: please enclose your code in “code fences”, eg:

 ```python
 your python code
 goes here
 ```

There’s a </> button in the compose toolbar for this. It preserves the
code formatting, which is very important.

Anyway…

pretty much ive just downloaded python and ive just made the command

"if 3 > 2:
print(“good”)

if 2 > 3:
print (“bad”)" just to see if i can actually do anything

i want to know if i can group this as something called like " math " and just type print(math) and get it to come up with the answer

i can explain in further detail if this doesnt make sense but i just
want help to see if its possible to group things as one command

You want a function, eg:

 def both():
     if 3 > 2:
         print("good")
     if 2 > 3:
         print ("bad")

which you run like this:

 both()

Normally you’d be more flexible, eg:

 def both(a, b):
     if a > b:
         print("good")
     if b > a:
         print ("bad")

and go:

 both(2, 3)

so that you could ask the same questions with other numbers eg 5 and 7
etc.

Your function calls print() already.

Normally we don’t print from functions very often (unless their purpose
is to do some printing of course). Typically you’d make something
likeyour function return a “Boolean” (a true/false value) indicating
whether the two numbers were good or bad. Then test is when you call it.
That way you could use it for decisions, and decide about printing stuff
separately.

eg:

 def both(a, b):
     if a > b:
         return True
     if b > a:
         return False


 if both(2, 3):
     print("good")
 else:
     print("bad")

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Additionally, there’s also the case of a == b to consider—right now, your manual commands (or the function using print() directly) doesn’t print anything, and the function above returns None (which is implicit if the function exits without a return statement, though it is a good practice to make that explicit if the function uses return elsewhere, i.e.

def both(a, b):
     if a > b:
         return True
     if b > a:
         return False
    return None

However, there’s a good chance that you want to treat a == b as either good, or bad. In that case, you can simplify your function dramatically; if, say, a == b is good, then you could write it:

def both(a, b):
    return a >= b

Or if a == b is bad, you’d write it

def both(a, b):
    return a > b

This returns the boolean result of the comparison directly, with no need for separate branches.

Alternatively, it may be closer to what you are asking for (to be able to do print(math) if you have your function instead return the string "good" or "bad" and print that. For that, you can do (assuming a == b is "bad")

def good_bad(a, b):
     if a > b:
         return "good"
     else:
         return "bad"

Or, more concisely:

def good_bad(a, b):
     return "good" if a > b else "bad"

You can then use it like so:

print(good_bad(2, 3))