I want to set a variable in Python to true or false. But the words true
and false
are interpreted as undefined variables:
#!/usr/bin/python
a = true;
b = true;
if a == b:
print("same");
The error I get:
a = true
NameError: global name 'true' is not defined
What is the python syntax to set a variable true or false?
Python 2.7.3
This question is related to
python-2.7
boolean
constants
you have to use capital True and False not true and false
Python boolean keywords are True
and False
, notice the capital letters. So like this:
a = True;
b = True;
match_var = True if a == b else False
print match_var;
When compiled and run, this prints:
True
match_var = a==b
that should more than suffice
you cant use a - in a variable name as it thinks that is match
(minus) var
match=1
var=2
print match-var #prints -1
Source: Stackoverflow.com