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:

--------------------------------------------------------------------------


Let us look at a few solutions from the exercise based on Numeric data type.


Answers have been shared in RED:
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
            b) what will be the output of the  statements that follow:
              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: 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)
The output of the above three expressions will be:
True
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:
  1. >>> val1=10  
  2. >>> val2=None  
  3. >>> val1  
  4. 10  
  5. >>> val2  
  6. >>> print val2  
  7. None  

-----------------------------------------------------------------------------------------------------------

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:



Comments

  1. Good morning ma’am!! Arjav Jain of class 9D here

    ReplyDelete
  2. Good Morning ma'am.I am Gracious Benny of class9-D

    ReplyDelete
  3. Good morning mam I am Brijin of class 9D

    ReplyDelete
  4. Good morning mam I am Brijin of class 9D

    ReplyDelete
  5. Good Morning Ma'am I am Hrishit Deb of class 9-D

    ReplyDelete
  6. Good morning ma'am .
    My name is Ojas Khanna 9d

    ReplyDelete
  7. Good morning ma’am this is Aditya Narayan Padhy 9D

    ReplyDelete
  8. Good morning ma'am I am Abhishek dial

    ReplyDelete
  9. good morning ma'am i am Alvin johnson

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Good morning ma'am
    My name is Ryan Rahuel Valentine from class 9-D

    ReplyDelete
  12. Good Morning Ma'am
    Kavyansh Bagotra 9D

    ReplyDelete
  13. Good morning ma'am
    I am soumil arora
    Roll no. 5

    ReplyDelete
  14. Good Morning Mam I am Derick Joseph of 9D

    ReplyDelete
  15. Good morning ma'am I am samaksh Goswami of class9-D

    ReplyDelete
  16. Good Morning ma'am . My name is Advait Mohanty of class 9 D

    ReplyDelete
  17. Good morning mam I am Kritik Kuraria

    ReplyDelete
  18. Good Morning Ma'am
    This is Joshua Walter of Class 9-D.

    ReplyDelete
  19. Good morning mam I am Sandarbh of class 9-D

    ReplyDelete
  20. Good morning mam I am Gunamay prasad of class 9 D

    ReplyDelete
  21. Good Morning ma'am
    I am Mohnish Kansal

    ReplyDelete
  22. Good morning Ma'am I am Piyush Sharma of class 9-D

    ReplyDelete
  23. Good morning ma'am
    This is arnav trivedy

    ReplyDelete
  24. Good morning teacher I am Rishit KUMAR

    ReplyDelete
  25. Good morning maam I am Satwik Pruthi of class 9D

    ReplyDelete
  26. Good morning ma'am I am Riyan Gabriel of 9-D

    ReplyDelete
  27. Good morning ma'am this is Vincent zacharias

    ReplyDelete

Post a Comment

Popular posts from this blog

HOLIDA'S HW

Introduction

python-PROGRAM(1)