Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.

Slides:



Advertisements
Similar presentations
Variables and Expressions
Advertisements

1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Types and Arithmetic Operators
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Section R3: Polynomials
D ATA T YPE. NUMBERS A number is an immutable type – means that changing or updating its value result in a newly allocated object. There are several types.
Numeric Types, Expressions, and Output ROBERT REAVES.
Addition, Subtraction, Multiplication, and Division of Integers
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Unit 3 Lesson 2: Rational Expressions
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Arithmetic in Pascal A Short Glance We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Data Types Declarations Expressions Data storage C++ Basics.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Division. Just as multiplication can be expressed as a series of additions, division can be seen as a series of subtractions. 21 ÷ 7 asks how many times.
Doing math In java.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Day Problems Simplify each expression. 1. (c 5 ) 2 2. (t 2 ) -2 (t 2 ) (2xy) 3x 2 4. (2p 6 ) 0.
DIVISION. Standards G4.1M.C2.PO4A. Use multiple strategies to divide whole numbers using 4-digit dividends and divisors from 1 to 12 with remainders.
INTEGER RULES. Adding Integers Adding IntegersRule # 1: Same Signs When adding integers with same signs, you add the numbers and write the common sign.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.
$100 $200 $300 $400 $100 $200 $300 $400 $300 $200 $100 Writing variable equations Find variables in addition equations Find variables in subtraction.
Ch 3.1 Add and Subtract Signed Numbers Vocabulary Op posites :2 numbers the same distance from 0 but in opposite directions
Logarithms Common Logarithms Integer Logarithms Negative Logarithms Log of a Product Log of a Quotient Log of an Exponential Natural Logarithms.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Interesting Integers – Part Dos
Thinking Mathematically
Lecture 4: Expressions and Variables
Algebra 1 Notes: Lesson 2-2 Rational Numbers
Multiplying and Dividing Integers
Negative Numbers.
Arithmetic Operator Operation Example + addition x + y
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Algebraic Expressions, Equations, and Symbols
Lecture 3: Expressions and Variables
Writing expression basic
Algebraic expression to Verbal statement Basic
More Maths Programming Guides.
Data Types and Expressions
Lecture 4: Expressions and Variables
Dividing a decimal by a whole number
Algebraic expression to verbal statement two terms
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
7-4 Division Properties of Exponents
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Operators in Python

Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work with numbers and produce numbers

For any operator you need to know These are questions you should ask every time you see a new operator – not just arithmetical but any operator! How many arguments does it have and what do they mean What data type are the arguments? (int, float, string, etc.) The semantics of the operator, which is what does it do? What does it mean? What data type does the operator return? NOT always the same as the type of the arguments What is its precedence or priority with other operators?

Example: addition + It has two arguments (we are talking about the addition operator here, not the unary plus sign) The arguments can be ints or floats It produces the sum of the values of the two arguments It produces an integer if the arguments are both integers, otherwise it produces a floating point number It is relatively low precedence, comes after * and /

Other familiar operators Multiplication * works on ints and floats, produces an int if 2 int arguments, float otherwise Subtraction works similarly to multiplication Division / works on ints and floats, but always returns a float Precedence is as in algebra, multiplication and division higher than addition and subtraction If there are two operators in an expression of the same level of precedence, they are executed left to right in the expression a + b + c is done as (a + b) + c

New Operators // and % // is similar to division but always gives the whole number part of the quotient (result of dividing) If give two ints, will give an int, otherwise a float 5 // 4 = 1, 5 // 4.0 = 1.0 (not 1.25) % is the modulus operator, gives the remainder of a division operation If give two ints, yields an int, otherwise a float 13 % 4 = 1 (the remainder when 13 is divided by 4), 8 % 4 = 0 These two operators have the same precedence as multiplication and division

New Operator ** ** means exponentiation Has two arguments a ** b a is the base, b is the power to raise a to 5 **3 = 5 * 5 * 5, 7 ** 2 = 7 * 7 = 49 The precedence is higher than the other arithmetic operators The base or power can be integer or float As usual, if two int arguments, gives int result, otherwise float Is right associative (different from all of the other operators) 4 ** 3 ** 2 = 4 ** (3 ** 2) = 4 ** 9 =