Line continuation output formatting and decision structures
This presentation is the property of its rightful owner.
Sponsored Links
1 / 27

Line Continuation, Output Formatting, and Decision Structures PowerPoint PPT Presentation


  • 72 Views
  • Uploaded on
  • Presentation posted in: General

Line Continuation, Output Formatting, and Decision Structures. CS303E: Elements of Computers and Programming. Line Continuation. What do you do if your line of Python code is too long? Use the line continuation character! the backslash character \ Place at the very end of the line

Download Presentation

Line Continuation, Output Formatting, and Decision Structures

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Line continuation output formatting and decision structures

Line Continuation, Output Formatting, and Decision Structures

CS303E: Elements of Computers and Programming


Line continuation

Line Continuation

  • What do you do if your line of Python code is too long?

  • Use the line continuation character!

    • the backslash character \

    • Place at the very end of the line

    • Python interpreter will assume the next line is part of the same line


Line continuation example

Line Continuation: Example

>>> sum = 2.35 + 8 \

+ 13.6 + \

25

>>> sum

48.950000000000003

>>> print “\t\nHello, my name is”, \

“Jarvis”

Hello, my name is Jarvis


Output formatting

Output Formatting

  • We’ve seen this:

    print “The temperature is”,temp,”degrees”

    • Now, we’ll see another way

  • Better able to control how print displays values

    • 88.33333333 -> 88.33

    • 4 / 15 / 2010 -> 4/15/2010


Output formatting format operators

Output Formatting:Format Operators

print “The temperature is %d degrees” % temp

  • String formatting operator: %

    • NOT modulus: modulus operates on numbers

    • Appears in strings

  • Indicates how and where a value should be printed in a string

  • Also indicates end of print string and beginning of the values to be printed


Output formatting format operators1

Output Formatting:Format Operators

print “The temperature is %d degrees” % temp

Indicates the type and format of the value

Indicates the end of the string to be printed and the beginning of the values specified in the string


Output formatting format specifiers

Output Formatting:Format Specifiers

  • For each, you can also specify width and precision:

    %<width>.<precision><type character>

    print “The average is %3.2f” % avg


Output formatting embedded operations

Output Formatting:Embedded Operations

You can calculate values in your print statement:

print “2+3 = %d” % (2+3)

print “x/y = %.2f” % (x/y)


Formatting example

Formatting Example

>>> import math # package containing pi

>>> math.pi #display the value of pi

3.1415926535897931

>>> #Now display with 4 digits after decimal point

>>> print “Pi: %.4f” % math.pi

Pi: 3.1416

Notes:

  • The f in the format string stands for float.

  • The number appears in the output wherever the format string occurs.

  • The number is rounded to the specified number of digits.


Formatting example1

Formatting Example

  • Can specify a minimum field width for the display of a value

  • Minimum width comes before the decimal point in the format string

  • >>> print “Pi: %7.3f” % math.pi

  • Pi = 3.142

  • The field width is 7:

    • digits and decimal point: width 5

    • 2 blank spaces to left of number


Formatting two or more values

Formatting: Two or More Values

  • Enclose multiple values in parentheses

  • Separate the values with commas

    print “First name: %10s, Last Name: %12s” % (“Elvis”, “Presley”)

    First name: Elvis, Last Name: Presley


Output formatting examples

Output Formatting:Examples

  • Modify average.py to print 2 decimal places of the average

  • Practice printing strings from raw_input()

  • Print using multiple values

    • The values must be enclosed in parentheses


Question for you output formatting

Question for you:Output Formatting

What is the expected output?

x = 5.7

y = 2.18

print “x+y=%.1f” % (x+y)

A. 7C. 7.9

B. 7.8D. 8


Comparisons

Comparisons

  • Allows you to compare two values

  • Result in a True or False value

    • type Boolean (bool)

  • You can compare numbers or strings, literals, variables, or expressions


How do you specify a comparison

How do you specify a comparison?

  • Specify the condition using a relational operator


Comparisons examples

Comparisons:Examples

  • test = 13 < 15

  • test = 101 >= 99

  • test = “a” < “b”

  • test = 4 == 2+2

  • test = 15 != 16

  • test = 12 == 3*5


Lexicographic order

Lexicographic Order

  • Strings are rated according to lexicographic order

  • Orders words A-Za-z

    • Capital letters first in alphabetical order

    • Lower-case letters second in alphabetical order

  • NOT in dictionary order


Decisions once you make a decision the universe conspires to make it happen ralph waldo emerson

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

  • Gives you the ability to specify different instructions based on a condition

  • The condition is typically a comparison

    if some comparison is true:

    do something


Decisions if statement

Decisions: if Statement

def main():

command

if(<condition>):

command

command

command

command

main()

Commands not dependent on the condition

Commands only executed if condition is true

Commands not dependent on the condition

Indentation matters! (Again)


If examples

if examples

number = 25

if number > 10:

print number, “is greater than 10!”

Output:

25 is greater than 10!


Decisions if else statement

Decisions:if-else Statement

if(<condition>):

command

command

else:

command

command

Commands only executed if condition is True

Commands only executed if condition is False


If else exercise

if-else 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.


Decisions if elif else statement

Decisions:if-elif-else Statement

if(<condition>):

command

command

elif(<condition>):

command

command

else:

command

command

Commands only executed if condition is True

Commands only executed if earlier conditions are False and this condition is True

You can used as many of these as you like

Commands only executed if EVERY condition is False


If elif else example

if-elif-else 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


Decisions nested if s

Decisions:Nested ifs

You can put if statements inside the body of the if (or elif or else) statement:

if(<condition>):

if(<some other condition>):

command

else:

command

elif(<condition>):


Decisions gotchas

Decisions:Gotchas

  • Exactly ONE of the bodies in if-elif-else will be executed

    • Only the first True condition

    • THINK about the construction of your if statements before coding

  • Comparison of floats

    if(.3 == .1+.2) is False


Question for you decisions

Question for you:Decisions

What is the expected output?

if(125<140):

print “first one”

elif(156>=140):

print “second one”

else:

print “third one”

A. first oneC. third one

B. second oneD. first one

second one


  • Login