Line Continuation, Output Formatting, and Decision Structures

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

16-May-15 Sudden Python Drinking from the Fire Hose.
Chapter 4 Making Decisions
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 - Algorithms and Design
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Variables and Expressions CMSC 201 Chang (rev )
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Decision Structures, String Comparison, Nested Structures
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
 Type Called bool  Bool has only two possible values: True and False.
Python Basics.
Basic concepts of C++ Presented by Prof. Satyajit De
© by Pearson Education, Inc. All Rights Reserved.
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
Introduction to Python
Formatting Output.
COMP 170 – Introduction to Object Oriented Programming
Chapter 4: Making Decisions.
Python: Control Structures
IF statements.
Variables and Arithmetic Operators in JavaScript
The Selection Structure
Variables, Expressions, and IO
Chapter 4: Control Structures
Formatting Output.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Logical Operators and While Loops
Decision Structures, String Comparison, Nested Structures
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
And now for something completely different . . .
Decision Structures, String Comparison, Nested Structures
Types, Truth, and Expressions (Part 2)
Introduction to Programming
If selection construct
3. Decision Structures Rocky K. C. Chang 19 September 2018
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Java Applications
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Logical Operators and While Loops
Topics Designing a Program Input, Processing, and Output
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
CHAPTER 5: Control Flow Tools (if statement)
Unit 3: Variables in Java
Introduction to Programming
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Introduction to C Programming
Class code for pythonroom.com cchsp2cs
Flow Control I Branching and Looping.
Presentation transcript:

Line Continuation, Output Formatting, and Decision Structures COSC 1301

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 Saw in Camus.py Show in interpreter

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 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 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

Output Formatting: Format Specifiers Value Type %d integer %f float %s string %e or %E exponential For each, you can also specify width and precision: %<width>.<precision><type character> print (“The average is %.2f” % avg) What about long? Precision mostly comes into play for floats, where it specifies places after the decimal place---rounds! but can be used for integers, where it pads with 0s Width is the total number of spaces the field should occupy Example: math.pi Modify average.py

Output Formatting: Embedded Operations You can calculate values in your print statement: print (“2+3 = %d” % (2+3)) print (“x/y = %.2f” % (x/y)) Do in IDLE

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 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 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 Modify average.py to print 2 decimal places of the average Practice printing strings from input() Print using multiple values The values must be enclosed in parentheses

Question for you: Output Formatting What is the expected output? x = 5.7 y = 2.18 print (“%.1f” % (x+y)) A. 7 C. 7.9 B. 7.8 D. 8 C

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 Where have you seen this before?

How do you specify a comparison? Specify the condition using a relational operator Operator Meaning < Less than > Greater than <= Less than or equal >= Greater than or equal == Equality != Not equals

Comparisons: Examples test = 13 < 15 test = 101 >= 99 test = “a” < “b” test = 4 == 2+2 test = 15 != 16 test = 12 == 3*5

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 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 Where have you seen this before?

Decisions: if Statement def main(): command if(<condition>): main() Commands not dependent on the condition Commands only executed if condition is true Indentation matters! (Again) Parentheses are optional in Python Do not want to duplicate code---you’ll have to correct mistakes twice The commands executed only if it is true is called the body or block of the if statement. Extend Average to say, “You passed!”

if examples number = 25 if number > 10: print (number, “is greater than 10!”) Output: 25 is greater than 10!

Decisions: if-else Statement if(<condition>): command else: Commands only executed if condition is True Extend average to say “You failed!” Commands only executed if condition is False

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 if(<condition>): command elif(<condition>): else: Commands only executed if condition is True You can used as many of these as you like Commands only executed if earlier conditions are False and this condition is True Elif is a contraction for else-if Extend average to say “You got a perfect score!” You can also nest the conditionals Commands only executed if EVERY condition is False

if-elif-else example number = int(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 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: elif(<condition>): … Grade example: if-elif… : Print "A", "B", …, "F" Then nest if-else in body of first block: if grade >= 97, print "A+", else print "A".

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

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 one C. third one B. second one D. first one second one A