how do you do an if statement with python
how do you do an if statement with python
TimothyAnderso1 (127)
Just to make it more simple:
if cool == true:
print("I am cool")
else:
print(" :( ")
Also it dont matter if you use "These" for a string or 'These' Its the same
ArchieMaclean (915)
@TimothyAnderso1 That's simpler probably
but it should have a lowercase if
.
TimothyAnderso1 (127)
@ArchieMaclean uh i am?
ArchieMaclean (915)
@TimothyAnderso1 You weren't originally?
ArchieMaclean (915)
Like this:
if something:
# this will execute if "something" is true
print("something is true")
elif somethingelse:
# this will execute if "something" is false, but "somethingelse" is true
print("somethingelse is true")
else:
# this will execute if neither "something" nor "somethingelse" is true
print("nothing is true")
You can add as many elif
statements as you want.
Please mark this as the answer if it solved your problem :)
ArchieMaclean (915)
@ArchieMaclean you can replace something
and somethingelse
with any comparison, e.g.
if x>10:
# blah
elif x>5:
# blah
else:
# blah
To elaborate, let's try an example.
That's not all you can do though.
Here's all the comparisons that you can do in example form:
Those are at least all that I know, someone let me know if I missed any.
You also have modifiers you can put before and inbetween conditions, those are
and
,not
, andor
To show how to use these and what they do, here's an example:
We also have one more thing we can do with
if-else
. We also haveif-elif-else
in python as well.Python sees if the if statement is true, and if it is, executes it. Otherwise, it executes the elif statement, then if that's not true, it executes the else statement.
To make some more sense out of it,
Is the same as;
You can also have mutliple
elif
s following anif
.I hope this made some sense and helped you out!