Math operations 9/19/16.

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Advertisements

 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
PERFORMING CALCULATIONS IN SCIENTIFIC NOTATION ADDITION AND SUBTRACTION.
Chapter 5, Section 9 Complex Numbers. i, the imaginary number By definition, the square root of -1 is defined as i, i.e. We can now write the square root.
Introductory Algebra Glossary The Language of Math.
Few More Math Operators
CS 106A, Lecture 4 Introduction to Java
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Topics Designing a Program Input, Processing, and Output
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
A Playful Introduction to Programming by Jason R. Briggs
Solving Two step equations
Lecture 4: Expressions and Variables
Topic: Python’s building blocks -> Statements
Formatting Output.
WARM-UP Write in Scientific Form Write in Standard Form
Imaginary & Complex Numbers
Building Java Programs
Dividing Polynomials.
Assignment statement:
Arithmetic operations & assignment statement
Primitive Data, Variables, Loops (Maybe)
Variables, Expressions, and IO
Computer Programming Methodology Introduction to Java
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Math in C The math blocks you've used in Scratch can all be recreated in C!
Introduction to Equations Cronnelly
Building Java Programs Chapter 2
Number and String Operations
Fractions Review.
Ch 3.3 – Solving Multi-Step Equations
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
Fractions Review.
Learning Outcomes –Lesson 4
Lecture 3: Expressions and Variables
Building Java Programs
Coding Concepts (Sub- Programs)
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Arithmetic Expressions & Data Conversions
1.3 Solving Linear Equations
Solving Two-Step Equations Lesson 2-2 Learning goal.
More Maths Programming Guides.
Chapter 3 – Introduction to C# Programming
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Fractions Review.
SSEA Computer Science: Track A
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Lecture 4: Expressions and Variables
Equations 1 Click mouse.
Topics Designing a Program Input, Processing, and Output
Fractions Review.
Fractions Review.
Fractions Review.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Building Java Programs
Building Java Programs
Fractions Review.
Fractions Review InfoTech Math October 12, 2017
Algebra 1B Chapter 3 Notes.
Number Theory: Prime & Composite Numbers
Building Java Programs
COMPUTING.
Arithmetic Expressions & Data Conversions
0.4 Nth Roots and Real Exponents
Presentation transcript:

Math operations 9/19/16

Math operations You can add, subtract, multiply, divide numbers like this: addition = 72 + 23 subtraction = 108 – 204 multiplication = 108 * 1.5 division = 108 / 9   You try it: Set the variable count_to equal to the sum of two big numbers Then print count_to

Exponentiation All that math can be done on a calculator, so why use Python? Because you can combine math with other data types (e.g. variables, strings, and booleans…which we will get to) and commands to create useful programs. Calculators just stick to numbers.   Now let’s work with exponents. eight = 2 ** 3 In the above example, we create a new variable called eight, and set it to 8, or the result of 2 to the power to 3 (2^3) Notice that we use ** instead of * or the multiplication operator. You try it: Create a new variable and use exponents to set that variable equation to 100. Try raising 10 to the power of 2.

Modulo Our final operator is modulo. Modulo returns the remainder from a division. So, if you type 3 % 2, it will return 1, because 2 goes into 3 evenly once, with 1 left over. You try it: use modulo to set a variable equal to 1. You can use any two numbers that will leave a remainder of 1 to do this.

Useful math operators Operator Description Example Evaluates to + Addition 7+3 10 - Subtraction 7-3 4 * Multiplication 7*3 21 / Division (True) 7/3 2.3333333333333333335 // Division (Integer) 7//3 2 % Modulus 7%3 1

More about variables Recap: a variable is a way to label and access information. Instead of having to know exactly where in the computers memory something is store, you use a variable to get at it. name = “Larry” This line is called an assignment statement. Technically, an assignment statement stores the value on the right side of the equal sign into your computers memory while the variable on the left side only refers to the value

Using Variables Once the variable has been created, it refers to some value. The convenience of the variable is that it can be used just like the value to which it refers. print (name) Prints the string “Larry” just like the statement print (“Larry”) does. And the line print (“Hi, “, name) Prints the string “Hi” followed by a space, followed by “Larry”

You try it Write a single-line comment on line 1. It can be anything! (Make sure it starts with a #) Set a variable python equal to 1.234 Set a second variable to monty_python equal to python squared Print monty_python

The Meal Now let's apply the concepts from the previous section to a real world example. You've finished eating at a restaurant, and received this bill: Cost of meal: $44.50 Restaurant tax: 6.75% Tip: 15% You'll apply the tip to the overall cost of the meal (including tax).

Instructions First lets declare the variable meal and assign it to 44.50

The Tax Now let's create a variable for the tax percentage. The tax on your receipt is 6.75%. You'll have to divide 6.75 by 100 in order to get the decimal form of the percentage.

Instructions Create the variable tax and set it equal to the decimal value of 6.75%

The Tip You received good service, so you'd like to leave a 15% tip on top of the cost of the meal, including tax. Before we compute the tip for your bill, let's set a variable for the tip. Again, we need to get the decimal form of the tip, so we divide 15.0 by 100.

Instructions On the next line, set the variable tip to the decimal value of 15%

Reassign in a Single Line We've got the three variables we need to perform our calculation, and we know some arithmetic operators that can help us out. We saw in last week’s lesson that we can reassign variables. For example, we could say spam = 7 and then later change out minds and say spam = 3

Instructions On the next line, reassign meal to the value of itself + itself * tax. And yes, your allowed to reassign a variable in terms of itself. We’re only calculating the cost of the meal and tax here. We will get to the tip next.

The Total Instructions Now that meal has the cost of the food plus tax, let’s introduce a new variable on the next line, total, equal to meal +meal *tip Instructions Assign the variable total to the sum of meal + meal * tip on the next line. Now you have the total cost of the meal. Then add: print("%.2f" % total) The code on the end formats and prints to the console the value of total with exactly two numbers after the decimal. (We'll learn about string formatting, the console, and print in the next unit!)