Python-Boolean Data Type
Good Morning Boys.
- I hope you are able to understand the concepts being shared.
- Feel free to share doubts.
- Don't forget to Check the solutions for the Exercise given in the last session.
---------------------------------------------------------------------
Google Meet class link for today is:
1. In the
following statements,
a=10.0
b=57
c="a"
d="SCS"
a) what is the data type of
the variables a,b,c and d ?
Ans.
- First of all we may check the data type by
using the type() function.
- Second,
The result should be as follows
- a
will be FLOAT
- b
will be INTEGER
- c will be STRING
- d will be STRING
print(a)
print(b)
print(c)
print(d)
Ans. The output of the statements
that follow:
print(a) will
be 10.0
print(b) will
be 57
print(c) will be a
print(d) will be SCS
7. Give the output of the following set of statements
a= 27.325
b=int(a)
print("b=",b)
Ans. The output of the statements will be:b=27
--------------------------------------------------------------------------------------------------
By the end of this session, you will be able to:
- Understand the purpose and application of Boolean literals.
- Understand and apply the concept of boolean literals in real life problems.
- Understand the purpose and application of bool( ) function.
- Understand the purpose and application of the Special literal None
Boolean Literals:
Booleans represent one of two values:
-----------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------
Q2 Predict the output for the following code:
Booleans represent one of two values:
True
or False
.Boolean Values
In programming you often need to know if an expression is
True
or False
.
You can evaluate any expression in Python, and get one of two answers,
True
or False
.
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
print(10 > 9)
print(10 == 9)
print(10 < 9)
print(10 == 9)
print(10 < 9)
The output of the above three expressions will be:
True
False
False
False
False
Explanation:
print(10 > 9)
The output will be true because, 10 is greater than 9, so TRUE will be printed
print(10 == 9)
The output will be false because, 10 is NOT EQUAL TO 9, so FALSE will be printed
print(10 < 9)
The output will be false because, 10 is NOT LESS THAN 9, so FALSE will be printed
Most Values are True
Almost any value is evaluated to
True
if it has some sort of content.
Any string is
True
, except empty strings.
Any number is
True
, except 0
.
Evaluate Values and Variables
The
bool()
function allows you to evaluate any value, and give you True
or False
in return,
Some Values are False
In fact, there are not many
values that evaluates to False, except
empty values, such as (), [], {}, "", the number 0, and the
value None. And of course the
value False evaluates to False.
The following will return False:
bool(False)
bool(None)
bool(0)
bool("")
Example : How to use boolean literals in Python?
x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
When you run the program, the output will be:
x is True
y is False
a: 5
b: 10
- In the above program, we use boolean literal
True
and False
.
- In Python,
True
represents the value as 1 and False
as 0.
- The value of x is
True
because 1 is equal to True
.
- And, the value of y is
False
because 1 is not equal to False
.
Similarly, we can use the True
and False
in numeric expressions as the value.
- The value of a is 5 because we add
True
which has value of 1 with 4.
- Similarly, b is 10 because we add the
False
having value of 0 with 10.
------------------------------------------------------------------------
Special literals
- Python contains one special literal i.e., None.
- None is used to specify to that field that is not created.
- It is same as NULL in other programing languages.
Eg:
x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
When you run the program, the output will be:
x is True
y is False
a: 5
b: 10
- In the above program, we use boolean literal
True
andFalse
. - In Python,
True
represents the value as 1 andFalse
as 0. - The value of x is
True
because 1 is equal toTrue
. - And, the value of y is
False
because 1 is not equal toFalse
.
Similarly, we can use the
True
and False
in numeric expressions as the value.- The value of a is 5 because we add
True
which has value of 1 with 4. - Similarly, b is 10 because we add the
False
having value of 0 with 10.
------------------------------------------------------------------------
Special literals
- Python contains one special literal i.e., None.
- None is used to specify to that field that is not created.
- It is same as NULL in other programing languages.
Eg:
EXERCISE
Q1. Consider the following statements and predict the output:
x = (1 == True)
y = (10<9)
a = True + 5
b = False + 2
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
---------------------------------------------------------------------------------
Q2 Predict the output for the following code:
Good morning ma’am!! Arjav Jain of class 9D here
ReplyDeleteGood Morning ma'am.I am Gracious Benny of class9-D
ReplyDeletegood morning mam.samarth raj 9D
ReplyDeleteGood morning mam I am Brijin of class 9D
ReplyDeleteGood morning mam I am Brijin of class 9D
ReplyDeleteGood Morning Ma'am I am Hrishit Deb of class 9-D
ReplyDeleteGood morning ma'am .
ReplyDeleteMy name is Ojas Khanna 9d
Good morning ma’am this is Aditya Narayan Padhy 9D
ReplyDeleteGood morning ma'am I am Abhishek dial
ReplyDeletegood morning ma'am i am Alvin johnson
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGood morning ma'am
ReplyDeleteMy name is Ryan Rahuel Valentine from class 9-D
Good Morning Ma'am
ReplyDeleteKavyansh Bagotra 9D
Good morning Ma'am I am Kenn of9d
ReplyDeleteGood morning ma'am
ReplyDeleteI am soumil arora
Roll no. 5
Good Morning Mam I am Derick Joseph of 9D
ReplyDeleteGood morning ma'am I am samaksh Goswami of class9-D
ReplyDeleteGood Morning ma'am . My name is Advait Mohanty of class 9 D
ReplyDeleteGood morning mam I am Kritik Kuraria
ReplyDeleteGood Morning Ma'am
ReplyDeleteThis is Joshua Walter of Class 9-D.
Good morning mam I am Sandarbh of class 9-D
ReplyDeleteGood morning mam I am Gunamay prasad of class 9 D
ReplyDeleteGood Morning ma'am
ReplyDeleteI am Mohnish Kansal
Good Morning Maam
ReplyDeleteI am Aryan Joseph
Good morning Ma'am I am Piyush Sharma of class 9-D
ReplyDeleteGood morning ma'am
ReplyDeleteThis is arnav trivedy
Good Morning ma'am
ReplyDeleteAaron John
Good morning teacher I am Rishit KUMAR
ReplyDeleteGood morning maam I am Satwik Pruthi of class 9D
ReplyDeleteGood morning ma'am I am Riyan Gabriel of 9-D
ReplyDeleteGood morning ma'am this is Vincent zacharias
ReplyDelete