Conditionals- booleans, Python Programming




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


>>> -6 <= 9



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]


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


>>> a = [1, 2]

>>> b = [1, 2]

>>> c = a

>>> a == b


>>> a is b


>>> a == c


>>> a is c



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


>>> not 7 > 8


>>> 7 == 7 and 8 > 7





Basic conditional instructions have the form:5

if :




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


return -x



We could also have written

def abs(x):

if x >= 0:

result = x


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
protocol rdt2.2 considers there is a bit errors between sender to receiver and also from receiver to sender. So now we have to consider checking bit errors introduced in reply from

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

Synthetic models One  goal  of various  people in a variety of sub-disciplines of  electrical  engineering and  computer science is automatic synthesis of machine from  formal

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

Task (Anagrams) Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters. Fore example, silent and listen are anagrams.

List mutation and shared structure Lists are mutable data  structures, which defines  that  we can actually modifies  the values  stored in their components. We do this by usi

inputWithinRange (prompt, lowValue, highValue) This function will ask for a number using the prompt parameter. The function will only return a value that is within the inclusive ra

Bank transfer What  if we  have  two  values,  representing bank  accounts, and  need  to transfer an  amount of money  amt between them?  Consider that a bank account is show

Programming models Just  as there  are several methods for organizing entire  software systems, there  are  different strategies for formally expressing computational processe

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