Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set python variables to true or false?

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

like image 590
user1050619 Avatar asked Sep 28 '12 16:09

user1050619


People also ask

How do you assign a variable truth to a Boolean?

To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.

How do you define true in Python?

The True keyword is a Boolean value, and result of a comparison operation. The True keyword is the same as 1 ( False is the same as 0).


1 Answers

First to answer your question, you set a variable to true or false by assigning True or False to it:

myFirstVar = True myOtherVar = False 

If you have a condition that is basically like this though:

if <condition>:     var = True else:     var = False 

then it is much easier to simply assign the result of the condition directly:

var = <condition> 

In your case:

match_var = a == b 
like image 70
poke Avatar answered Sep 22 '22 17:09

poke