Python Basics – Print, Data types and comments
- Subject: Python Tutorials
- Learning Time: 2 hour
Boolean in python programming
Booleans are really simple they only contain one of two value true or false. You are already wondering why on earth would you use something like this. Well I thought the same when learning to program however very soon you will realise how much this little variable can help.
So lets imagine you are working on a game. Now in order for your character to progress through the game he or she has to open a secret door to the next level. Now there are far too many ways to do this some complicated and some simple. To do it in the long way we can check if the character has got the key and if the character is by the door and so on so forth or we can simply have a Boolean that changes from has key false to has key true. Boom you are on the next level now. This is just a simple example of rather complex issue. Just remember this one is important to know.
Booleans are used in variety of ways one of the most common places you will find them is in a IF statement. We will cover IF in great detail on later tutorials. To basically glance at Booleans do the following:
isRaining = False
This will show False on the console screen. Now we can change it to true and check the value again it will show true. This is an example of a Boolean variable. See below for more examples.
Lets try a different way to use Booleans. Python is very smart and it knows which values are true and which are false. Lets say we want to compare two numbers together and see what it returns for us.
print (2==2)#this will return true
This line of code means is two equals to two. We are using the double = sign because thats the way to compare two numbers together.
print(3==2) #this will return false
We can do the same with text or strings
print(“nice”==”nice”) # returns true
print(“nice”==”not nice”) # returns false
Let’s try if something is not equals to something in python.
print(“nice”!=”not nice”) # returns true
print(3!=2) # returns true
Also here are some more example
print(3>2)#this means is 3 greater than 2 its true
print(2<3)#this means is 2 less than 3 its true
Try some examples of your own and see how they work out. Check back with the tutorial if you get errors somewhere.
In basic method these two functions return true because none of these values are equal to each other and python knows that. Thank you python.