Basic Operators. What is an operator? using expression 4 + 5 is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

Types and Arithmetic Operators
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz.
Chapter 4: Basic C Operators
4. Python - Basic Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Chapter 6. Dataflow Modeling. Continuous Assignments The left hand side always be a scalar or vector net or a concatenation of scalar and vector nets.
Programmable Logic Architecture Verilog HDL FPGA Design Jason Tseng Week 5.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Programming Principles Operators and Expressions.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CSE 220 – C Programming Expressions.
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Arithmetic operations & assignment statement
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Arithmetic operations, decisions and looping
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
Lecture 2 Python Programming & Data Types
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Lecture 2 Python Programming & Data Types
Homework Homework Continue Reading K&R Chapter 2 Questions?
Introduction to Programming – 4 Operators
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
OPERATORS in C Programming
Introduction to Python
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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 the following types of operators. 1.Arithmetic Operators 2.Comparison (i.e., Relational) Operators 3.Assignment Operators 4.Logical Operators 5.Bitwise Operators 6.Membership Operators 7.Identity Operators

Arithmetic Operators Assume variable a holds 10 and variable b holds 20 OperatorDescriptionExample +Addition - Adds values on either side of the operator a + b will give 30 -Subtraction - Subtracts right hand operand from left hand operand a - b will give -10 *Multiplication - Multiplies values on either side of the operator a * b will give 200 /Division - Divides left hand operand by right hand operand b / a will give 2 %Modulus - Divides left hand operand by right hand operand and returns remainder b % a will give 0 **Exponent - Performs exponential (power) calculation on operators a**b will give 10 to the power 20 //Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. 9//2 is equal to 4 and 9.0//2.0 is equal to 4.0

Example - Arithmetic Operators a = 21; b = 10; c = 0 c = a + b print "Line 1 - Value of c is ", c c = a - b print "Line 2 - Value of c is ", c c = a * b print "Line 3 - Value of c is ", c c = a / b print "Line 4 - Value of c is ", c c = a % b print "Line 5 - Value of c is ", c a = 2 b = 3 c = a**b print "Line 6 - Value of c is ", c a = 10; b = 5; c = a//b print "Line 7 - Value of c is ", c Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 8 Line 7 - Value of c is 2

Comparison Operators Assume variable a holds 10 and variable b holds 20 OperatorDescriptionExample ==Checks if the value of two operands are equal or not, if yes then condition becomes true. (a == b) is not true. !=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true. <>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a <> b) is true. This is similar to != operator. >Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true. <Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true. >=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is not true. <=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a <= b) is true.

Example 1 - Comparison Operators a = 21; b = 10; c = 0 if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b" else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b" else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b"

Example 2 - Comparison Operators a = 21; b = 10; c = 0 if ( a > b ): print "Line 5 - a is greater than b" else: print "Line 5 - a is not greater than b" a = 5; b = 20; if ( a <= b ): print "Line 6 - a is either less than or equal to b" else: print "Line 6 - a is neither less than nor equal to b" if ( b >= a ): print "Line 7 - b is either greater than or equal to b" else: print "Line 7 - b is neither greater than nor equal to b"

output Line 1 - a is not equal to b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b

Assignment Operators: Assume variable a holds 10 and variable b holds 20 OperatorDescriptionExample =Simple assignment operator, Assigns values from right side operands to left side operand c = a + b will assigne value of a + b into c +=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a -=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a *=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a %=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a **=Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //=Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a

Example Assignment a = 21; b = 10; c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2 c %= a print "Line 5 - Value of c is ", c c **= a print "Line 6 - Value of c is ", c c //= a print "Line 7 - Value of c is ", c Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52 Line 5 - Value of c is 2 Line 6 - Value of c is Line 7 - Value of c is 99864

Bitwise Operator Bitwise operator works on bits and perform bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows: a = b = a&b = a|b = a^b = ~a =

Bitwise Operator OperatorDescriptionExample &Binary AND Operator copies a bit to the result if it exists in both operands. (a & b) will give 12 which is |Binary OR Operator copies a bit if it exists in eather operand. (a | b) will give 61 which is ^Binary XOR Operator copies the bit if it is set in one operand but not both. (a ^ b) will give 49 which is ~Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. (~a ) will give -61 which is in 2's complement form due to a signed binary number. <<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. a << 2 will give 240 which is >>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a >> 2 will give 15 which is

Example - Bitwise a = 60 # 60 = b = 13 # 13 = c = 0 c = a & b; # 12 = print "Line 1 - Value of c is ", c c = a | b; # 61 = print "Line 2 - Value of c is ", c c = a ^ b; # 49 = print "Line 3 - Value of c is ", c c = ~a; # -61 = print "Line 4 - Value of c is ", c c = a << 2; # 240 = print "Line 5 - Value of c is ", c c = a >> 2; # 15 = print "Line 6 - Value of c is ", c Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15

Logical Operators Assume variable a holds 10 and variable b holds 20 Opera tor DescriptionExample andCalled Logical AND operator. If both the operands are true then then condition becomes true. (a and b) is true. orCalled Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (a or b) is true. notCalled Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. not(a and b) is false.

Example 1 - Logical a = 10; b = 20; c = 0 if ( a and b ): print "Line 1 - a and b are true" else: print "Line 1 - Either a is not true or b is not true" if ( a or b ): print "Line 2 - Either a is true or b is true or both are true" else: print "Line 2 - Neither a is true nor b is true" Line 1 - a and b are true Line 2 - Either a is true or b is true or both are true

Example 2 - Logical a = 0; b = 20; c = 0 if ( a and b ): print "Line 3 - a and b are true" else: print "Line 3 - Either a is not true or b is not true" if ( a or b ): print "Line 4 - Either a is true or b is true or both are true" else: print "Line 4 - Neither a is true nor b is true" if not( a and b ): print "Line 5 - Either a is not true or b is not true" else: print "Line 5 - a and b are true“ Line 3 - Either a is not true or b is not true Line 4 - Either a is true or b is true or both are true Line 5 - Either a is not true or b is not true

Membership Operators Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators explained below: OperatorDescriptionExample inEvaluates to true if it finds a variable in the specified sequence and false otherwise. x in y, here in results in a 1 if x is a member of sequence y. not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is not a member of sequence y.

Example Membership a = 10; b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is in the given list" else: print "Line 1 - a is not in the given list" if ( b not in list ): print "Line 2 - b is not in the given list" else: print "Line 2 - b is in the given list" a = 2 if ( a in list ): print "Line 3 - a is in the given list" else: print "Line 3 - a is not in the given list“ Line 1 - a is not in the given list Line 2 - b is not in the given list Line 3 - a is in the given list

Identity Operators Identity operators compare the memory locations of two objects. There are two Identity operators explained below: OperatorDescriptionExample isEvaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y). is notEvaluates to false if the variables on either side of the operator point to the same object and true otherwise. x is not y, here is not results in 1 if id(x) is not equal to id(y).

Identity – Example a = 20; b = 20 if ( a is b ): print "Line 1 - a and b same " else: print "Line 1 - a and b not same " if ( id(a) == id(b) ): print "Line 2 - a and b same " else: print "Line 2 - a and b not same " b = 30 if ( a is b ): print "Line 3 - a and b same " else: print "Line 3 - a and b not same " if ( a is not b ): print "Line 4 - a and b not same " else: print "Line 4 - a and b same “ Line 1 - a and b same Line 2 - a and b same Line 3 - a and b not same Line 4 - a and b not same

Operators Precedence 1 The following table lists all operators from highest precedence to lowest. OperatorDescription **Exponentiation (raise to the power) ~ + -Ccomplement, unary plus and minus (method names for the last two are and * / % //Multiply, divide, modulo and floor division + -Addition and subtraction >> <<Right and left bitwise shift &Bitwise 'AND'

Operators Precedence 2 The following table lists all operators from highest precedence to lowest. OperatorDescription &Bitwise 'AND' ^ |Bitwise exclusive `OR' and regular `OR' >=Comparison operators <> == !=Equality operators = %= /= //= -= += *= **=Assignment operators is is notIdentity operators in not inMembership operators not or andLogical operators

Example - Operators Precedence a = 20; b = 10; c = 15; d = 5; e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5 print "Value of ((a + b) * c) / d is ", e e = (a + b) * (c / d); # (30) * (15/5) print "Value of (a + b) * (c / d) is ", e e = a + (b * c) / d; # 20 + (150/5) print "Value of a + (b * c) / d is ", e

output Value of (a + b) * c / d is 90 Value of ((a + b) * c) / d is 90 Value of (a + b) * (c / d) is 90 Value of a + (b * c) / d is 50