- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python and Keyword
The "and" keyword in Python works as a logical operator. It returns True only if both the expression evaluates to be True. For example:
a = 10 b = 20 c = 30 if a<b and b<c: print(a, "is smallest") else: print(a, "is not smallest")
10 is smallest
From above program, since the left expression, that is a<b or 10<20 evaluates to be True, and the right expression, that is b<c or 20<30 also evaluates to be True. Therefore the whole expression including the and keyword, that is:
a<b and b<c
evaluates to be True. So the if statement including the evaluated condition becomes:
if True:
That indicates the program flow, to goes inside the if block and evaluate the following statement:
print(a, "is smallest")
that prints 10 is smallest on output as shown in the snapshot.
Note - The and keyword can be used to separate any number of expression. If all expressions, separated by and keyword, evaluates to be True, then only the whole expression evaluates to be True. That is, if any expression evaluates to be False, then of course, the whole expression evaluates to be False.
Python and Keyword with More than Two Expressions
This program uses and keyword to separate more than two expressions:
a = 10 b = 20 c = 30 d = 40 e = 50 if a<b and a<c and a<d and a<e: print(a, "is smallest") else: print(a, "is not smallest")
The output is:
10 is smallest
Here is another example of and keyword in Python:
print(True and True and True and True and True) print(True and True and True and False and True)
The output is:
True False
Python and Keyword Truth Table
The truth table of and keyword is:
A | B | A and B |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
« Previous Tutorial Next Tutorial »