Few More Math Operators

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Python November 14, Unit 7. Python Hello world, in class.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Computer Science 101 Introduction to Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 101 Introduction to Programming.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Bell Ringer = – 5 = = ÷ -2 = =6. -7 – (-7) = After you have completed the bell ringer, take out your homework!
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
Computer Science 101 Introduction to Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.
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.
Floating Point Numbers
Few More Math Operators
Math operations 9/19/16.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Topics Designing a Program Input, Processing, and Output
Chapter 2 More on Math More on Input
Chapter 2 Introduction to C++ Programming
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Formatting Output.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Variables, Expressions, and IO
Formatting Output.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
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.
Introduction to C++ Programming
Math and Data Types Practice Problems
Introduction to Java, and DrJava part 1
Learning Outcomes –Lesson 4
Arithmetic Expressions & Data Conversions
Reading Input from the Keyboard
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Lecture3.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Introduction to Java, and DrJava
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
CHAPTER 3: String And Numeric Data In Python
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Beginning Python Programming
Unit 3: Variables in Java
Output Manipulation.
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Data Types and Maths Programming Guides.
Introduction to C Programming
Formatting Output.
Arithmetic Expressions & Data Conversions
Presentation transcript:

Few More Math Operators Just a couple of more …

Practice – the Metro Card Write a program that asks the value of their current Metro Card If each ride costs $3.75, compute: The number of rides they have left The amount of money they have “left over” after previously stated given rides The amount of money they need to add to round out an even number of rides

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

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

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

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 There’s actually an operator symbol in Python for what we just did. Realize, that this will happen a lot. Python has functions and commands that condense the process of a common algorithm. Let’s take a look …

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)

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

format( ) Function This is a bit premature, but for the sake of your homework, we can use the format( ) function. This function allows us to format numbers to as many decimal places as we’d like. It also allows us to insert a comma every three digits, as there are in the real number system (i.e. 12,345,678) The format function must receive two arguments: The number it is formatting (for now we’ll always pass floats) The instructions for formatting

format( ) Function format ( 100000/7 , “,.2f” ) The result: 14,285.71 Instructions: format ( number , “ , . 2 f ” ) Examples: format ( 100000/7 , “,.2f” ) The result: 14,285.71

format( ) Function print ( format ( 100000/7 , “,.2f” ) ) >> 14,285.71 x = format( 100000/7 , “.2f ” ) print( “$” + x ) >> $14,285.71

I Woke Up in a New Bugatti

Compounded Interest 𝑨= 𝑨 𝒐 𝟏+ 𝒓 𝒏 𝒏𝒕 𝑨= 𝑨 𝒐 𝟏+ 𝒓 𝒏 𝒏𝒕 𝑨=𝒇𝒊𝒏𝒂𝒍 𝒑𝒓𝒊𝒄𝒆, 𝒂𝒇𝒕𝒆𝒓 𝒂𝒄𝒄𝒓𝒖𝒆𝒅 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕 𝑨 𝒐 =𝒊𝒏𝒊𝒕𝒊𝒂𝒍 𝒑𝒓𝒊𝒄𝒆, 𝒃𝒆𝒇𝒐𝒓𝒆 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕 𝒔𝒕𝒊𝒄𝒌𝒆𝒓 𝒑𝒓𝒊𝒄𝒆 𝒓=𝒓𝒂𝒕𝒆 𝒐𝒇 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕, % 𝑨𝑷𝑹 (𝒎𝒖𝒔𝒕 𝒃𝒆 𝒊𝒏 𝒅𝒆𝒄𝒊𝒎𝒂𝒍 𝒇𝒐𝒓𝒎) 𝒏=𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒕𝒊𝒎𝒆𝒔 𝒄𝒐𝒎𝒑𝒐𝒖𝒏𝒅𝒆𝒅/ 𝒑𝒂𝒊𝒅 𝒑𝒆𝒓 𝒚𝒆𝒂𝒓 (𝒇𝒐𝒓 𝒐𝒖𝒓 𝒑𝒖𝒓𝒑𝒐𝒔𝒆𝒔, 𝒏 𝒘𝒊𝒍𝒍 𝒂𝒍𝒘𝒂𝒚𝒔 𝒆𝒒𝒖𝒂𝒍 𝟏𝟐) 𝒕=𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒚𝒆𝒂𝒓𝒔 𝒇𝒐𝒓 𝒕𝒉𝒆 𝒍𝒐𝒂𝒏