Python logical operator 'and'

0 votes

Please help understand the working of and

a=10 
b=50 
print(a and b) # 50
print(b and a) # 10

Why different results?

Jan 28 in Python by Lakheer
59 views

1 answer to this question.

0 votes

If you use and on two or more non-zero numbers then the last number would be printed as output. If one of those numbers is 0 then 0 will be the output.

print (a and b)

Here is the last number and because a is not zero, b is printed.

print (b and a)

And in this case, is the last number and because b is not zero, a is printed.

answered Jan 28 by Omkar
• 67,660 points

Related Questions In Python

0 votes
0 answers

What is the difference between == and is operator in python?

What is the basic use of is ...READ MORE

Sep 9 in Python by Waseem
• 4,470 points
45 views
+4 votes
6 answers

Does Python have a ternary conditional operator like C does?

Yes, the syntax is: a if condition else ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
154 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
98 views
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,080 points
51 views
0 votes
0 answers

what is a bitwise operator in python?

can you show some operations using bitwise ...READ MORE

Apr 8 in Python by Waseem
• 4,470 points
35 views
0 votes
0 answers

what is the logical AND operator in python?

how do i use it in a ...READ MORE

Apr 12 in Python by Waseem
• 4,470 points
36 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 346 views
+6 votes
4 answers

Python and Pygame: Difference

Python is a programming language. PyGame is a ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 56,940 points
151 views
+3 votes
2 answers

What does Python _init_ and self do?

Hey @Anirudh!  Self The self keyword is used to access ...READ MORE

answered Oct 31, 2018 in Python by Omkar
• 67,660 points
286 views