444,035 Members | 1,408 Online
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

# If Statement1 is true and Statement2 is true

 100+ P: 176 Hello guys. I was wondering if it is possible to execute certain commands within a if statement if two conditions are true. I know that in C++ and JavaScript you can do something like if (5 > 4 && 3>4) { cout << "Hello, world!" } or in JavaScript if (5>4 && 3>4) { document.write("Hello, world!") So can you do the same thing in Python? Thanks :-) Mar 30 '08 #1
4 Replies

 Expert Mod 2.5K+ P: 2,851 Hello guys. I was wondering if it is possible to execute certain commands within a if statement if two conditions are true. I know that in C++ and JavaScript you can do something like if (5 > 4 && 3>4) { cout << "Hello, world!" } or in JavaScript if (5>4 && 3>4) { document.write("Hello, world!") So can you do the same thing in Python? Thanks :-) Yes. Expand|Select|Wrap|Line Numbers >>> a = 3 >>> b = 4 >>> if a < 5 and b < 5: ...     print 'Both conditions are True' ...      Both conditions are True >>> if a < 5: ...     if b > 5: ...         print 'b is greater than 5' ...     elif b < 5: ...         print 'b is less than 5' ...     else: ...         print 'b equals 5' ...          b is less than 5 >>>  Mar 30 '08 #2

 100+ P: 176 Thanks I tried that and it worked. And you could do it with 3, 4 etc right? Mar 30 '08 #3

 100+ P: 126 Hello guys. I was wondering if it is possible to execute certain commands within a if statement if two conditions are true. I know that in C++ and JavaScript you can do something like if (5 > 4 && 3>4) { cout << "Hello, world!" } or in JavaScript if (5>4 && 3>4) { document.write("Hello, world!") So can you do the same thing in Python? Thanks :-) Last time i did any math 3 whas in fact smaler than 4. Wich makes that statement false. And the hello, world part wont be executed. So if you did the same thing in python im not suprised it didnt work. Expand|Select|Wrap|Line Numbers >>> 3>4 False >>> 3<4 True >>> 5>4 and 3>4 False >>> 5>4 and 3<4 True >>>    Mar 30 '08 #4

 100+ P: 176 I was just giving an example. Mar 30 '08 #5