Few More Math Operators

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Advertisements

Programming Languages: Notes for Class Discussion: V Deena Engel’s class.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
CompSci 230 S Programming Techniques
Math operations 9/19/16.
CS 106A, Lecture 4 Introduction to Java
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Software Development.
Introduction to Computing Science and Programming I
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
A Playful Introduction to Programming by Jason R. Briggs
Repetition Structures
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
Expressions.
Formatting Output.
Completing the Problem-Solving Process
Chapter 2 Assignment and Interactive Input
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Few More Math Operators
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to C++ Programming
Math and Data Types Practice Problems
Learning Outcomes –Lesson 4
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
MSIS 655 Advanced Business Applications Programming
Arithmetic Expressions & Data Conversions
Repetition Structures
Reading Input from the Keyboard
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Core Objects, Variables, Input, and Output
Topics Designing a Program Input, Processing, and Output
Java Programming Loops
Topics Designing a Program Input, Processing, and Output
Beginning Python Programming
Unit 3: Variables in Java
CHAPTER 6 Testing and Debugging.
Arithmetic Expressions & Data Conversions
Introduction to Python
Presentation transcript:

Few More Math Operators Just a couple of more …

Practice – The solution to all our problems We’ve all been there before, we go to the restaurant with some friends and we’re having a good time … all until the check comes out and the nightmare that is figuring out how much you need to pay appears.

Practice – The solution to all our problems Create a program that helps a user figure out exactly how much they need to pay for dinner. You probably want to start off with a design for your code. (What information do you need to start? What parts of the solution are subject to change?) Assume 7% tax.

Practice – The solution to all our problems HINT: You will need to ask the user for a few components: The total amount of the check (before tax) The price of their individual meal The percent tip they want to leave The number of people at the table

Practice – The solution to all our problems Step by step: You will need to take the total amount of the check and add the tax to the total Then you want to find the tip based off the total before tax Then you will add the tax amount and the tip amount as a table You will take that number and divide it by the number of people who ate, this will be the amount people need to put on top of their meal for “tip and tax” Add this value to the amount of the user’s specific meal

The REAL solution … Understand that the actual problem at hand was not the calculations of the bill People are more often frustrated by the time they feel they’re wasting by waiting for the check to be paid … So, what’s the real solution?

The REAL solution …

It’s your birthday! Write a program that asks the user for their name and their age. Figure out what their birth year is from this information and report it back to the user.

Debugging The process of debugging is really to find errors (bugs) and then to resolve them

Types of Errors Syntax errors: the code does not follow the rules for the language; for example, a single quote is used where a double quote is needed, or a colon is missing, or a keyword is used as a variable name print ( “ Hello, world! ’ )

Types of Errors Runtime errors: in this case, your code is fine but the program does not run as expected (it “crashes”). For example, if your program is meant to divide two numbers, but the divisor is zero, a runtime error will occur. var1 = float( input (‘give me a number:’) ) number = var1 + 4 >> give me a number: seventeen thirty eight # runtime error

Types of Errors Logic errors: these can be the hardest to find. In this case, the program is correct from a syntax perspective, and it runs, but the result is not what you were looking for. For example, if your program prints “2 + 2 = 5” the answer is … clearly wrong. var = 3.49 print (var) >> 3.4900000000002

Example Logic Error num_1 = float ( input ( ‘ give me a number:’ ) ) num_2 = float ( input ( ‘ give me another number:’ ) ) Print ( ‘ the sum is: ’, num_1 – num_2 ) >> give a number: 43 give me another number: 32 the sum is: 11

Tips for debugging Set small, incremental goals for your program. Don’t try and write a large program all at once. Stop and test your work often. Use comments to have Python ignore certain lines that are giving you trouble. Ask a friend.

Practice: Debugging # Name: Donald Seok # Title: “I don’t always make mistakes, but when I do …” first_name = ‘Donald” last_name = Seok print ( “Welcome to “Computer Programming” 101,”, first_name last_name, “!” ) print ( ) answer_1 = float ( input ( “What is the answer to 5 plus 2?” ) Print (“Your answer:”, answer_1) print ( )) real_answer = 5 * 2 printt (“If you answered”, real_answer, “you’re right!”)

Order of Operations Python follows the order of operations (PEMDAS) You can use parentheses inside your math expressions to group operations Example: x = ( (5 + 10 + 20) / 60 ) * 100

PEMDAS Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable 3 * 4 / 2 * 5 (3 * 4) / (2 * 5)

Converting Math Formulas into Programming Statements Most math formulas need to be converted into a format that Python can understand Examples: 10 x y 10 * x * y ( 3 ) ( 12 ) 3 * 12 𝑦= 3𝑥 2 y = 3 * x / 2

Line Continuation Sometimes expressions can get to be very long You can use the “ \ ” symbol to indicate to Python that you’d like to continue the expression onto another line ** Example: x = 5 + 2 / 7 \ + 8 – 12 This also works for the print ( ) function

Mixed Type Expressions Python allows you to mix integers and floats when performing calculations The result of a mixed-type expression will evaluate based on the operands used in the expression Operand 1 Operand 2 Result int float

Division Operations Python contains two different division operators The “/” operator is used to calculate the floating-point result of a division operation The “//” operator is used to calculate the integer result of a division operation, it will throw away the remainder. *** This operation will always round DOWN. Examples: print ( 5 // 2 ) # 2 print ( -5 // 2 ) # -3

Remainder Operator (modulo) The modulo operator “ % ” returns the remainder portion of a division operation This is basically the opposite of the “ // ” operator Examples: 5 / 2 # 2.5 5 // 2 # 2 5 % 2 # 1 (remainder from divisor of 2)

Exponents You can raise any number to a power by using the “ ** ” operator Example: 24  2 ** 4

Practice: Time Calculations Ask the user to input a number of seconds as a whole number. Then express the time value inputted as combination of minutes and seconds >> Enter seconds: 110 That’s 1 minute and 50 seconds!

Practice: Time Calculations Now extend this program to include the number of hours >> Enter seconds: 12074 That’s 3 hours, 21 minutes and 14 seconds!

Escape Key “\” The backslash ( “ \ ” ) is known as an escape key in Python It tells Python that the character directly following the backslash will not function in it’s regular nature Example: print (“This class is “Awesome!””) #error! print (“This class is \“Awesome!\””) >> This class is “Awesome!”

Examples of the Escape Key We can use the escape key in various ways: print(“\””) # this will print a quotation mark print(“\n”) # this will print a new line print(“\t”) # this will print an indented tab print(“\\”) # this will print out a back slash

Examples of the Escape Key print (“We saw this \n this will print a new line”) >> We saw this this will print a new line

Examples of the Escape Key print (“We saw this \t this will print a tab”) >> We saw this this will print a tab

Examples of the Escape Key print (“What if we want an actual backslash \\”) >> What if we want an actual back slash \

Practice: O Christmas Tree Using a single print statement, try writing a program that prints out the image of a Christmas Tree We want this: >> tree /\ / \ / \ I I

Examples of the Escape Key print (""" /\\ \n / \\ \n/ \\ \n | | """) >> tree /\ / \ / \ I I