[ Pobierz całość w formacie PDF ]

The following table, taken from the Python reference manual, is provided for
the sake of completeness. It is far better to use parentheses to group operators
and operands appropriately in order to explicitly specify the precedence. This
makes the program more readable. See Changing the Order of Evaluation below
for details.
lambda Lambda Expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in Membership tests
is, is not Identity tests
38
=, !=, == Comparisons
| Bitwise OR
Æ Bitwise XOR
& Bitwise AND
Shifts
+, - Addition and subtraction
*, /, //, % Multiplication, Division, Floor Division and Remainder
+x, -x Positive, Negative
~x Bitwise NOT
** Exponentiation
x.attribute Attribute reference
x[index] Subscription
x[index1:index2] Slicing
f(arguments ...) Function call
(expressions, ...) Binding or tuple display
[expressions, ...] List display
{key:datum, ...} Dictionary display
The operators which we have not already come across will be explained in later
chapters.
Operators with the same precedence are listed in the same row in the above
table. For example,+and-have the same precedence.
7.3 Changing the Order Of Evaluation
To make the expressions more readable, we can use parentheses. For example,
2 + (3 * 4)is definitely easier to understand than2 + 3 * 4which requires
knowledge of the operator precedences. As with everything else, the parentheses
should be used reasonably (do not overdo it) and should not be redundant, as in
(2 + (3 * 4)).
There is an additional advantage to using parentheses - it helps us to change the
order of evaluation. For example, if you want addition to be evaluated before
multiplication in an expression, then you can write something like(2 + 3) * 4.
39
7.4 Associativity
Operators are usually associated from left to right. This means that operators
with the same precedence are evaluated in a left to right manner. For example,2
+ 3 + 4is evaluated as(2 + 3) + 4. Some operators like assignment operators
have right to left associativity i.e.a = b = cis treated asa = (b = c).
7.5 Expressions
Example (save asexpression.py):
length = 5
breadth = 2
area = length * breadth
print( Area is , area)
print( Perimeter is , 2 * (length + breadth))
Output:
$ python3 expression.py
Area is 10
Perimeter is 14
How It Works:
The length and breadth of the rectangle are stored in variables by the same
name. We use these to calculate the area and perimeter of the rectangle with the
help of expressions. We store the result of the expressionlength * breadth
in the variableareaand then print it using theprintfunction. In the second
case, we directly use the value of the expression2 * (length + breadth)in
the print function.
Also, notice how Python  pretty-prints the output. Even though we have not
specified a space between Area is and the variablearea, Python puts it for
us so that we get a clean nice output and the program is much more readable
this way (since we don t need to worry about spacing in the strings we use for
output). This is an example of how Python makes life easy for the programmer.
7.6 Summary
We have seen how to use operators, operands and expressions - these are the
basic building blocks of any program. Next, we will see how to make use of these
in our programs using statements.
40
8 Control Flow
In the programs we have seen till now, there has always been a series of statements
faithfully executed by Python in exact top-down order. What if you wanted to
change the flow of how it works? For example, you want the program to take
some decisions and do different things depending on different situations, such as
printing  Good Morning or  Good Evening depending on the time of the day?
As you might have guessed, this is achieved using control flow statements. There
are three control flow statements in Python -if,forandwhile.
8.1 The if statement
Theifstatement is used to check a condition: if the condition is true, we
run a block of statements (called the if-block), else we process another block of
statements (called the else-block). The else clause is optional.
Example (save asif.py):
number = 23
guess = int(input( Enter an integer : ))
if guess == number:
print( Congratulations, you guessed it. ) # New block starts here
print( (but you do not win any prizes!) ) # New block ends here
elif guess
print( No, it is a little higher than that ) # Another block
# You can do whatever you want in a block ...
else:
print( No, it is a little lower than that )
# you must have guessed > number to reach here
print( Done )
# This last statement is always executed, after the if statement is executed
Output:
$ python3 if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python3 if.py
Enter an integer : 22
No, it is a little higher than that
41
Done
$ python3 if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
How It Works:
In this program, we take guesses from the user and check if it is the number that
we have. We set the variablenumberto any integer we want, say23. Then, we
take the user s guess using theinput()function. Functions are just reusable
pieces of programs. We ll read more about them in the next chapter.
We supply a string to the built-ininputfunction which prints it to the screen [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • policzgwiazdy.htw.pl