Python Operators and Precedence PDF
Document Details
Uploaded by SpiritualGeometry3224
Tags
Summary
This document provides a quick reference guide to Python operators, including arithmetic, comparison, logical, bitwise, and membership operators. It also details operator precedence, demonstrating how these operators are evaluated in expressions.
Full Transcript
input() function a = input(“Enter name”) Note: The output of the input function is always a string even if the number is entered by the user. Example: Area of circle take radius form user. Operators Arithmetic Operators Comparison Operators Logical Operators Bitwise Operators Id...
input() function a = input(“Enter name”) Note: The output of the input function is always a string even if the number is entered by the user. Example: Area of circle take radius form user. Operators Arithmetic Operators Comparison Operators Logical Operators Bitwise Operators Identity Operators Operator Precedence Augmented Assignment Operators Arithmetic Operator + (unary) +a Unary Positive In other words, it doesn’t really do anything. It mostly exists for the sake of completeness, to complement Unary Negation. + (binary) a+b Addition Sum of a and b - (unary) -a Unary Negation Value equal to a but opposite in sign - (binary) a-b Subtraction b subtracted from a * a*b Multiplication Product of a and b / a/b Division Quotient when a is divided by b. The result always has type float. % a%b Modulo Remainder when a is divided by b Arithmetic Operator // a // b Floor Division (also Quotient when a is divided by b, rounded to the floor called Integer Division) number ** a ** b Exponentiation a raised to the power of b Comparison Operator/ Relational Operator == a == b Equal to True if the value of a is equal to the value of b False otherwise != a != b Not equal to True if a is not equal to b, False otherwise < a= a >= b Greater than or equal to True if a is greater than or equal to b, False otherwise == a == b Equal to True if the value of a is equal to the value of b False otherwise Logical Operator True if x is False not not x False if x is True (Logically reverses the sense of x) True if either x or y is True or x or y False otherwise True if both x and y are True and x and y False otherwise Bitwise Operator & a&b bitwise AND Each bit position in the result is the logical AND of the bits in the corresponding position of the operands. (1 if both are 1, otherwise 0.) | a|b bitwise OR Each bit position in the result is the logical OR of the bits in the corresponding position of the operands. (1 if either is 1, otherwise 0.) ~ ~a bitwise negation Each bit position in the result is the logical negation of the bit in the corresponding position of the operand. (1 if 0, 0 if 1.) ^ a^b bitwise XOR Each bit position in the result is the logical XOR of the bits in (exclusive OR) the corresponding position of the operands. (1 if the bits in the operands are different, 0 if they are the same.) >> a >> n Shift right n places Each bit is shifted right n places. > x = 1001 >>> a = 'I am a string' >>> y = 1000 + 1 >>> b = a >>> print(x, y) 1001 1001 >>> a is b >>> id(x) True >>> id(a) >>> x == y 60307920 >>> a == b 55993992 True >>> id(y) True >>> id(b) >>> x is y 60307936 55993992 False Membership Operator Python membership operators are used to check the membership of value inside a Python data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false. In :- It is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary). not in :- It is evaluated to be true if the first operand is not found in the second operand (string, list, tuple, or dictionary). Operator Precedence When we string operators together - Python must know which one to do first. This is called “operator precedence”. Operators Meaning () Parentheses ** Exponent +x, -x, ~x Unary plus, Unary minus, Bitwise NOT *, /, //, % Multiplication, Division, Floor division, Modulus +, - Addition, Subtraction Bitwise shift operators & Bitwise AND ^ Bitwise XOR | Bitwise OR ==, !=, >, >=,