Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
The If/Else Statement, Boolean Flags, and Menus Page 180
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
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 and programming Michael Ernst UW CSE 190p Summer 2012.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Introduction to Python
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Decision Structures and Boolean Logic
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
CONTROLLING PROGRAM FLOW
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Variables and Expressions CMSC 201 Chang (rev )
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
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
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
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.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Nested Loops CS303E: Elements of Computers and Programming.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
 Type Called bool  Bool has only two possible values: True and False.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Python: Control Structures
IF statements.
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Types, Truth, and Expressions (Part 2)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
CHAPTER 5: Control Flow Tools (if statement)
Types, Truth, and Expressions
Presentation transcript:

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? What do you do if your line of Python code is too long? Use the line continuation character! 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 >>> sum = \ \ \ >>> sum >>> print “\t\nHello, my name is”, \ “Jarvis” “Jarvis” Hello, my name is Jarvis

Output Formatting We’ve seen this: We’ve seen this: print “The temperature is”,temp,”degrees” –Now, we’ll see another way Better able to control how print displays values Better able to control how print displays values – > –4 / 15 / > 4/15/2010

Output Formatting: Format Operators print “The temperature is %d degrees” % temp String formatting operator: % String formatting operator: % –NOT modulus: modulus operates on numbers –Appears in strings Indicates how and where a value should be printed in a string 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 Also indicates end of print string and beginning of the values to be printed

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 For each, you can also specify width and precision: For each, you can also specify width and precision: %. %. print “The average is %3.2f” % avg

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 >>> import math # package containing pi >>> math.pi #display the value of pi >>> #Now display with 4 digits after decimal point >>> print “Pi: %.4f” % math.pi Pi: Notes: 1. The f in the format string stands for float. 2. The number appears in the output wherever the format string occurs. 3. The number is rounded to the specified number of digits.

Formatting Example Can specify a minimum field width for the display of a value Can specify a minimum field width for the display of a value Minimum width comes before the decimal point in the format string Minimum width comes before the decimal point in the format string >>> print “Pi: %7.3f” % math.pi >>> print “Pi: %7.3f” % math.pi Pi = Pi = The field width is 7: 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 Enclose multiple values in parentheses Separate the values with commas 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 Modify average.py to print 2 decimal places of the average Practice printing strings from raw_input() Practice printing strings from raw_input() Print using multiple values 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 “x+y=%.1f” % (x+y) A. 7C. 7.9 B. 7.8D. 8

Comparisons Allows you to compare two values Allows you to compare two values Result in a True or False value Result in a True or False value –type Boolean ( bool ) You can compare numbers or strings, literals, variables, or expressions You can compare numbers or strings, literals, variables, or expressions

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

Comparisons: Examples 1. test = 13 < test = 101 >= test = “a” < “b” 4. test = 4 == test = 15 != test = 12 == 3*5

Lexicographic Order Strings are rated according to lexicographic order Strings are rated according to lexicographic order Orders words A-Za-z Orders words A-Za-z –Capital letters first in alphabetical order –Lower-case letters second in alphabetical order NOT in dictionary 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 Gives you the ability to specify different instructions based on a condition The condition is typically a comparison The condition is typically a comparison if some comparison is true: do something

Decisions: if Statement def main(): command command if( ): if( ): command command main() Indentation matters! (Again) Commands not dependent on the condition Commands only executed if condition is true Commands not dependent on the condition

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

Decisions: if-else Statement if( ): if( ): command command else: else: command command Commands only executed if condition is True 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. 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( ): if( ): command command elif( ): elif( ): command command else: else: command command Commands only executed if condition is True Commands only executed if EVERY condition is False Commands only executed if earlier conditions are False and this condition is True You can used as many of these as you like

if-elif-else example number = input(“Please enter your number: “) if number < 10: print number, “is small” print number, “is small” elif number < 100: print number, “is pretty big” print number, “is pretty big” elif number < 500: print number, “is big” print number, “is big”else: print “Wow, a really big number!” print “Wow, a really big number!” Sample Run: Please enter your number: is big

Decisions: Nested if s You can put if statements inside the body of the if (or elif or else ) statement: if(<condition>): if( ): if( ):command else: else:commandelif(<condition>):…

Decisions: Gotchas Exactly ONE of the bodies in if- elif-else will be executed 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 Comparison of floats if(.3 ==.1+.2) is False

Question for you: Decisions What is the expected output? if(125<140): print “first one” print “first one”elif(156>=140): print “second one” print “second one”else: print “third one” print “third one” A. first oneC. third one B. second oneD. first one second one second one