Sunday, September 16, 2018

Python Ninja bootcamp 12-Boolean

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Boolean.


" Boolean values are the two constant objects False and True ".

We can use built-in bool() function or comparison operators to get the boolean result.


Let's look at the Boolean examples-

a = 1 
b = 2 

print( a < b ) 
>>True

print(bool( a > b))
>> False


print(bool( a == b))
>> False

We can also use bool() function
print(bool( a != b)) # Returns True when the argument is true, False otherwise
>> True


Check The Type

We have built-in function type() to check the type of object.

type(True)
>> bool

type(false)
>> bool

type(0)
>> int

type('value')
.>> str


Cast Into Boolean

We can use built-in bool() method to cast the object in the boolean type

1.Integer to boolean

bool(1)
>> True

bool(0) # Notice O is casted as false in boolean
>> False

2.String to Boolean

bool('value')
>> True

bool(' ')  # Notice Space is casted as false in boolean
>> False


Boolean to Integer


int(True) # Notice True is casted as 1 in integer type
>> 1


int(False) # Notice False is casted as 0 in integer type
>> 0

Also, try to cast boolean to string



EDITS ARE WELCOMED!!

In the next Blog, we will discuss Q&A first. 

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-13-q-first.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/


View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment