I’m learning Python and I’m right now on the chapter “Control Flow”. I’ve just learned that you can see what type of class a data have by for example write: print(type(is_true)) it will output: <class ‘bool’> if it’s a boolean.
I just tried to do the exact same thing but with a if Statement. The result of that: print(type(if)) was this error: File “script.py”, line 15
SyntaxError: invalid syntax
So my question is why can’t I class the if Statement?
Thanks in advance!
This is the whole script:
statement_one = False
statement_two = True
credits = 115
gpa = 1.9
if (credits >= 120) and (gpa >= 2.0):
print(“You meet the requirements to graduate!”)
if(credits < 120) and (gpa < 2.0):
print(“You don’t meet the requirements to graduate.”)
When you type:
You are actually saying “Print the type of variable that the word or command “if” is” which will turn up as an error because if is not a variable, it is a built in body of code which indicates a statement or a condition.
There is actually no variable for you to assign what is returned of each if, so you can’t check its type. If you type Return True o Return False, it will return a boolean outcome but you are not assigning it to a variable, so there is nothing you can apply type to…
I hope this helped!!
Thanks that really helped me understand!
I just wanna add some information. There is special termin for such a part of any program language - operator. “if”, such as “for”, “while”, “class”, “break”, and so on - are all operators, means it’s a special word in language, which start some sort of “constraction” like control flow. Also this words are resrved by language, so you cannot create a variable with name “if” and with name of other operators.
Hope this will help too.
Yes thank you! The description of operators made it very clear.