Conditionals- booleans, Python Programming

 

Booleans

 

Before we talk about  conditionals, we require  to clarify the Boolean  data  type.  It has two values False and True. Typical statement that have Boolean values  are numerical comparisons:

 

>>> 7 > 8

False

>>> -6 <= 9

True

 

We can also test  whether data  items  are equal  to each other.  Usually we use  == to test  for equality. It gives True if the two  objects have  same  values.   Sometimes, however, we will be study in knowing whether the two items are the exact same object. In that case we use is:

 

>>> [1, 2] == [1, 2]

True

>>> [1, 2] is [1, 2]

False

>>> a = [1, 2]

>>> b = [1, 2]

>>> c = a

>>> a == b

True

>>> a is b

False

>>> a == c

True

>>> a is c

True

 

Thus, in the examples above, we see that == testing  can be applied to nested loops, and basically gives true  if every one of the individual elements is the similar.  However, is testing,  especially when  applied to nested loops, is more defined, and only returns True if the two objects point to exactly the same instance in memory.

In addition, we can combine Boolean values  conveniently using  and, not, and or:

 

>>> 7 > 8 or 8 > 7

True

>>> not 7 > 8

True

>>> 7 == 7 and 8 > 7

True

 

If

 

Basic conditional instructions have the form:5

if :

else:

 

 

When  the  interpreter encounters a  conditional statement, it  starts  by  evaluating  ,   getting either   True or  False as  a  result.6   If  the given result   is  True,  then   it  will  eval­ uate  ,...,;  if it is not true,  then  it will evaluate ,...,. Crucially, it always calculates only one set of the statements.

 

Now, for example, we can create a functionthat returns the absolute value of its argument.

 

def abs(x):

if x >= 0:

return x

else:

return -x

 

 

We could also have written

def abs(x):

if x >= 0:

result = x

else:

result = -x

return result

 

Python uses the level of indentation of the instructions to decide  which  ones go in the groups of statements governed by the conditionals; so, in the example above, the return result statement is evaluated once the conditional is done,  no matter which  branch  of the conditional is evaluated.

 

Posted Date: 8/9/2012 7:02:10 AM | Location : United States







Related Discussions:- Conditionals- booleans, Assignment Help, Ask Question on Conditionals- booleans, Get Answer, Expert's Help, Conditionals- booleans Discussions

Write discussion on Conditionals- booleans
Your posts are moderated
Related Questions
Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

Your assignment for the semester will involve the development of a system for drawing trees using the Python programming language and the turtle graphics module (turtle.py). Comple

I have a C++ code (10-15 line) which i need to re-write in python with the help of ctypes library. Is it possible i can get some help with it? Thanks & regards Tanmoy

Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and final

Procedure calls When you compute an expression of the form ( ,  ..., )   the Python interpreter treats  this as a procedure call. It will be simpler to talk about

Models It is a new system that is considerably easier than  the system being modelled, but which saves the important points of the original machine. We might create a physical

The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as: It's time that we became uber-effici

For this assignment, you are to: 1.    Modify the output format of your Python script from Assignment 1 to match the input format of your C program from Assignment 2. 2.    W

Java programs are  what  is known as statically and  strongly defined.  Thus,  the  kinds  of all the variables must  be known at the time that the program is written. That seems

Procedures in Firrst-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variabl