When writing if statement conditions, you may want to return a true value if the condition is false. In PHP, you would simply throw an exclamation point before your condition to make it return the opposite value. Here is a simple example to help visualize how this might be used.
$var1 = false;
if( !( $var1 ) ){
echo 'var1 is false';
}
Since the value of var1 was not true, the if statement would execute. To accomplish this in Python, you would include the word "not" before your condition. Here is an example of how the previous example would look in Python.
var1 = False
if not var1:
print('var1 is false')
If you have any further questions, feel free to ask in the comments below.
you don't need parentheses in python print statement. so instead of print('var1 is false') you can just say
ReplyDeleteprint 'var is false'
In Python 2.x the parentheses are optional, in Python 3.x they are required, so why not make it a habit to always use them?
ReplyDeleteBecause when your're using parentheses in 2.x it makes a list, then prints out information. But of course in 99% it doesn't matter
ReplyDelete@Shane: Since this blog is about Django and Django doesn't support Python 3 yet, it would make sense to not use the parenthesis.
ReplyDeleteAlso, you don't need parenthesis around the variable name in PHP. if (!$var1) is enough.