Continuing a Statement on One or More Lines

The backslash character \  is the line continuation character, and allows you to continue a Python statement on the next line.

Example:
>>> sum = 2.35 + 8 \
... + 13.6 + \
...  25
>>> sum
48.95000000000000003
>>> print "\t\nHello, my name is", \
...      "Jarvis"

Hello, my name is Jarvis




Formatting Output


The string formatting operator %

We use the formatting operator % to indicate how a number should be displayed.

General form: print format_string % number

Example:
>>> import math # this package contains a constant for the value of pi
>>> math.pi  # display the value of pi
3.1415926535897931
>>> # Now we display pi with 4 digits after the decimal point
>>>print "Pi with 4 digits after the decimal point: %.4f" % math.pi
Pi with 4 digits after the decimal point: 3.1416


Notes:
1. the f in the format string means that we are printing a float.
2. The number (in this example, math.pi) appears in the output wherever the format string occurs.
3. The number is rounded to the specified number of digits.


We can also specify a minimum field width for the display of the number. The minimum width comes before the decimal point in the format string.


Example:
>>> print "Pi = %7.3f" % math.pi
Pi =   3.142

The width of the field is 7, and the numbers and decimal point have width 5, so there are 2 blank spaces to the left of the number.

Here are the characters that you use with % in the format string to indicate different types:

s -  string
e -  exponential format for a floating point number (with lowercase e)
E - exponential format (with uppercase E)
d - integer


Example:
>>> print "%.3E" % math.pi
3.142E+00
>>> print "%.3e" % math.pi
3.142e+00
>>> print "%15s" % "hello"
               hello
>>> print "My favorite number %5d is cool!" % 13
My favorite number    13 is cool!




Using format strings to display multiple values

When you are formatting two or more values in a print statement, enclose the values in  parentheses, and separate the values with commas.

Example:
>>> print "My first name is %10s and my last name is %12s" % ("Elvis", "Presley")
My first name is      Elvis and my last name is      Presley





Decision Structures


"Once you make a decision, the universe conspires to make it happen."
   --- Ralph Waldo Emerson


The if statement

The if statement executes its block (one or more statements) only when its condition is true.

Format:
if condition:
    statement
    statement
    etc.


Example:
>>> number = 25
>>> if number > 10:
. . .         print number, "is greater than 10!"
. . .
25 is greater than 10!



Relational Operators

Operator  What it means
 >  greater than
<  less than
>=  greater or equal
<=  less than or equal
==  equal
!= not equal


Note: In Python, we can compare strings with these operators:
"elvis" < "jasper" since elvis comes before "jasper" in alphabetical order. More on this later...


Example:
num1 = 10
num2 = 5
if num2 < num1:
    print num2, "is less than", num1

if num1 != num2:
    print num1, "is not equal to", num2

if num1 == num2:
    print num1, "is equal to", num2


Output:
5 is less than 10
10 is not equal to 5



The if-else statement

An if-else statement executes one of two blocks of statements. One block is executed if the if statement's condition is true, and the other is executed if the condition is false.


Format:
if condition:
    statement
    statement
    etc.
else:
    statement
    statement
    etc.


Exercise: Write a program that asks the user to enter a number. If the number is 3, print a message indicating that they entered your favorite number, and otherwise, indicate that you don't like the chosen number.



The if-elif-else statement

The if-elif-else statement executes exactly one of several blocks, depending on which of several conditions is true.

Format:
if condition1:
    statement
    statement
    etc.
elif condition2:
    statement
    statement
    etc.
elif condition3:
    statement
    statement
    etc.
else:
    statement
    statement
    etc.

Note: You may use as many elif clauses as you like. The conditions are executed in order - condition1, condition2, etc. - until a true condition is found, and the block for that true condition is executed. If none of the conditions are true, the else block is executed.


Example:
number = input("Please enter your number: ")

if number < 10:
    print number, "is small"
elif number < 100:
   print number, "is pretty big"
elif number < 500:
   print number, "is big"
else:
   print "Wow, a really big number!"

Sample Run:
Please enter your number: 355
355 is big



Exercise: Write a program that asks the user for the current month and the current day, and then prints the month and day in mm/dd format.