CMSC201 Computer Science I for Majors

Similar documents
9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CMSC201 Computer Science I for Majors

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

JAVA OPERATORS GENERAL

CMSC 201 Computer Science I for Majors

Prof. Navrati Saxena TA: Rochak Sachan

Variables and Operators 2/20/01 Lecture #

Information Science 1

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

Here n is a variable name. The value of that variable is 176.

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

BASIC ELEMENTS OF A COMPUTER PROGRAM

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Decisions, Decisions. Testing, testing C H A P T E R 7

CMSC201 Computer Science I for Majors

COMP Primitive and Class Types. Yi Hong May 14, 2015

Operators. Java operators are classified into three categories:

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Arithmetic and Bitwise Operations on Binary Data

CMSC201 Computer Science I for Majors

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Learning the Language - V

CS 320: Concepts of Programming Languages

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

QUIZ: What value is stored in a after this

Chapter 17. Fundamental Concepts Expressed in JavaScript

CS Kangmei Yang. Page 1

Information Science 1

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Lesson 38: Conditionals #2 (W11D3)

Department of Computer Science

CSE 115. Introduction to Computer Science I

Operators & Expressions

Mathematical Data Operators

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS101 Lecture 04: Binary Arithmetic

Building Java Programs

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Lecture 3. Input, Output and Data Types

Flow Control. So Far: Writing simple statements that get executed one after another.

Building Java Programs

Fundamentals of Programming CS-110. Lecture 2

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Expressions and Precedence. Last updated 12/10/18

Expressions and Control Statements

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CMSC201 Computer Science I for Majors

Lecture 3 Operators MIT AITI

Full file at

CIS 110: Introduction to Computer Programming

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Basics of Java Programming

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

CSI Lab 02. Tuesday, January 21st

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Advanced Algorithms and Computational Models (module A)

Computer Programming : C++

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Chapter 4: Basic C Operators

ECEN 468 Advanced Logic Design

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CT 229. Java Syntax 26/09/2006 CT229

Chapter 2: Using Data

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Fundamentals of Programming

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Ex: If you use a program to record sales, you will want to remember data:

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

CS 135 Lab Assignments Week 1

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Fundamentals: Expressions and Assignment

Maciej Sobieraj. Lecture 1

REVIEW. The C++ Programming Language. CS 151 Review #2

Computer Programming CS F111

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Language Reference Manual

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Building Java Programs

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Lecture 1. Types, Expressions, & Variables

Chapter 4 : Informatics Practices. Data Handling. Class XI ( As per CBSE Board) Visit : python.mykvs.in for regular updates

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

UNIT 3 OPERATORS. [Marks- 12]

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Introduction to the C++ Programming Language

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Transcription:

CMSC201 Computer Science I for Majors Lecture 03 Operators All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted

Variables Last Class We Covered Rules for naming Different types How to use them Printing output to the screen Getting input from the user Mad Libs 2

3 Any Questions from Last Time?

Today s Objectives To learn Python s operators Arithmetic operators Including mod and integer division Assignment operators Comparison operators Boolean operators To understand the order of operations 4

Pop Quiz! Which of the following examples are correct? 1. 500 = numstudents 2. numstudents = 500 3. numcookies * cookieprice = total 4. mpg = miles_driven / gallons_used 5. "Hello World!" = message 6. _CMSC201_doge_ = "Very learning" 7. 60 * hours = days * 24 * 60 5

6 Pop Quiz! Which of the following examples are correct? 1. 500 = numstudents 2. numstudents = 500 3. numcookies * cookieprice = total 4. mpg = miles_driven / gallons_used 5. "Hello World!" = message 6. _CMSC201_doge_ = "Very learning" 7. 60 * hours = days * 24 * 60

7 Python s Operators

8 Python Basic Operators Operators are the constructs which can manipulate and evaluate our data Consider the expression: num = 4 + 5 operator

Types of Operators in Python Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Membership Operators Bitwise Operators Identity Operators focus of today s lecture 9

Operators Addition & Subtraction Lowest priority in the order of operations Can only change this with parentheses Function as they normally do Examples: 1. cash = cash - bills 2. (5 + 7) / 2 3. ( ((2 + 4) * 5) / (9-6) ) 10

Operators Multiplication & Division Higher priority in the order of operations than addition and subtraction Function as they normally do Examples: 1. tax = subtotal * 0.06 2. area = PI * (radius * radius) 3. totaldays = hours / 24 11

Operators Integer Division Reminder: integers (or ints) are whole numbers What do you think integer division is? Remember division in grade school? Integer division is Division done without decimals And the remainder is discarded 12

Examples: Integer Division Integer division uses double slashes (//) Examples: 1. 7 / 5 = 2. 7 // 5 = 3. 2 / 8 = 4. 2 // 8 = 1.4 1 5. 4 // 17 // 5 = 0.25 0 0 evaluate from left to right 13

Operators Mod Also called modulo or modulus Example: 17 % 5 = 2 What do you think mod does? Remember division in grade school? Modulo gives you the remainder The opposite of integer division 14

Examples: Mod Mod uses the percent sign (%) Examples: 1. 7 % 5 = 2. 5 % 9 = 3. 17 % 6 = 4. 22 % 4 = 2 5 5 2 5. 48692451673 % 2 = 1 15

Modulo Answers Result of a modulo operation will always be: Positive No less than 0 No more than the divisor minus 1 Examples: 1. 8 % 3 = 2. 21 % 3 = 3. 13 % 3 = 2 0 no more than the divisor minus 1 1 no less than zero 16

Operators Exponentiation Exponentiation is just another word for raising one number to the power of another Examples: 1. binary8 = 2 ** 8 2. squarearea = length ** 2 3. cubevolume = length ** 3 4. squareroot = num ** 0.5 17

Arithmetic Operators in Python Operator Meaning + Addition - Subtraction * Multiplication / Division // Integer division % Modulo (remainder) ** Exponentiation 18

Order of Operations (Arithmetic) Expressions are evaluated in from what left direction? to right Operator(s) ** * / // % + - Priority highest lowest What can change this ordering? Parentheses! 19

20 Assignment Operators

Basic Assignment All assignment operators Contain a single equals sign Must have a variable on the left side Examples: 1. numdogs = 18 2. totaltax = income * taxbracket 3. numpizzas = (people // 4) + 1 21

Combining with Arithmetic You can simplify statements like these count = count + 1 amountleft = amountleft // 2 By combining the arithmetic and assignment count += 1 amountleft //= 2 You can do this with any arithmetic operator 22

Combined Assignments These operators work only if the variable is the first thing to the right of the assignment percent = int(input("enter percent: ")) # convert the percentage to a decimal percent /= 100 The last line is the same as this line percent = percent / 100 23

24 Comparison Operators

Vocabulary Comparison operators Relational operators Equality operators Are all the same thing Include things like >, >=, <, <=, ==,!= 25

26 Comparison Operators Always return a Boolean result True or False Indicates whether a relationship holds between their operands a >= b comparison operator operands

Comparison Examples What are the following comparisons asking? a >= b Is a greater than or equal to b? a == b Is a equivalent to b? 27

Comparison Operators in Python Operator Meaning < Less than (exclusive) <= Less than or equal to (inclusive) > Greater than (exclusive) >= Greater than or equal to (inclusive) == Equivalent to!= Not equivalent to 28

Comparison Examples (Continued) What do these evaluate to if a = 10 and b = 20? a >= b Is a greater than or equal to b? Is 10 greater than or equal to 20? FALSE 29

Comparison Examples (Continued) What do these evaluate to if a = 10 and b = 20? a == b Is a equivalent to b? Is 10 equivalent to 20? FALSE 30

31 Comparison vs Assignment A common mistake is to use the assignment operator (=) in place of the relational (==) This is a very common mistake to make! This type of mistake will trigger an error in Python, but you may still make it on paper!

Equals vs Equivalence What does a = b do? Sets a equal to b Changes a s value to the value of b What does a == b do? Checks if a is equivalent to b Does not change the value of a or b 32

33 Evaluating to Boolean Values

Comparison Operators and Simple Data Types Examples: 8 < 15 evaluates to 6!= 6 evaluates to 2.5 > 5.8 evaluates to 5.9 <= 7.5 evaluates to True False False True 34

Value of Boolean Variables When we discuss Boolean outputs, we think True and False We can also think of it in terms of 1 and 0 True = 1 False = 0 35

Value of Boolean Variables Other data types can also be seen as True or False in Python Anything empty or zero is False "" (empty string), 0, 0.0 Everything else is True 81.3, 77, -5, "zero", 0.01 Even "0" and "False" evaluate to True 36

37 Logical Operators

Logical operators Boolean operators Are the same thing Vocabulary Include and, or, and not 38

Logical Operators There are three logical operators: and or not They allow us to build more complex Boolean expressions By combining simpler Boolean expressions 39

40 Logical Operators and Let s evaluate this expression bool1 = a and b Value of a Value of b Value of bool1 True True True True False False False True False False False False

41 Logical Operators and Let s evaluate this expression bool1 = a and b Value of a Value of b Value of bool1 True True True True False False False True False False False False For a and b to be True, both a and b must be true

42 a = 10 b = 20 c = 30 Examples of and Prints: True True True ex1 = a < b ex2 = a < b and b < c ex3 = a + b == c and b 10 == a and c / 3 == a print (ex1, ex2, ex3)

43 Logical Operators or Let s evaluate this expression bool2 = a or b Value of a Value of b Value of bool2 True True True True False True False True True False False False

44 Logical Operators or Let s evaluate this expression bool2 = a or b Value of a Value of b Value of bool2 True True True True False True False True True False False False For a or b to be True, either a or b must be true

45 Logical Operators not Let s evaluate this expression bool3 = not a Value of a True False Value of bool3 False True

46 Logical Operators not Let s evaluate this expression bool3 = not a Value of a True False Value of bool3 False True not a calculates the Boolean value of a and returns the opposite of that

Complex Expressions We can put multiple operators together! bool4 = a and (b or c) What does Python do first? Computes (b or c) Computes a and the result 47

Order of Operations (All) Operator(s) ** * / // % + - < <= > >=!= == not and or Priority highest lowest 48

Announcements Your discussions start this week! Go to your scheduled location and time HW 0 is due by Wednesday at 8:59:59 PM HW 1 is out on Blackboard now You must complete a Quiz to see it Due by Friday (Feb 10th) at 8:59:59 PM 49