By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,408 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
4 Replies


bvdet
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
  1. >>> a = 3
  2. >>> b = 4
  3. >>> if a < 5 and b < 5:
  4. ...     print 'Both conditions are True'
  5. ...     
  6. Both conditions are True
  7. >>> if a < 5:
  8. ...     if b > 5:
  9. ...         print 'b is greater than 5'
  10. ...     elif b < 5:
  11. ...         print 'b is less than 5'
  12. ...     else:
  13. ...         print 'b equals 5'
  14. ...         
  15. b is less than 5
  16. >>> 
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

Smygis
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
  1. >>> 3>4
  2. False
  3. >>> 3<4
  4. True
  5. >>> 5>4 and 3>4
  6. False
  7. >>> 5>4 and 3<4
  8. True
  9. >>> 
  10.  
Mar 30 '08 #4

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

Post your reply

Sign in to post your reply or Sign up for a free account.