473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

If Statement1 is true and Statement2 is true

176 100+
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 1598
bvdet
2,851 Expert Mod 2GB
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
Kid Programmer
176 100+
Thanks I tried that and it worked. And you could do it with 3, 4 etc right?
Mar 30 '08 #3
Smygis
126 100+
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
Kid Programmer
176 100+
I was just giving an example.
Mar 30 '08 #5

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

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
3
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
36
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
30
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.