PYTHON- OPERATORS CO1 : Explain environment, data types, operators used in Python . Mr. DEEPAK Kumar Assistant Professor GNA University
OPERATORS O perators are special symbols, combinations of symbols, or keywords that designate some type of computation. You can combine objects and operators to build expressions that perform the actual computation . an operator is usually a symbol or combination of symbols that allows you to perform a specific operation. This operation can act on one or more operands . If the operation involves a single operand, then the operator is unary . If the operator involves two operands, then the operator is binary .
TYPES OF OPERATORS IN PYTHON Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Bitwise Operators Membership Operators Identity Operators
1. Arithmetic Operators: Arithmetic operators are used for performing mathematical operations a nd are those operators that allow you to perform arithmetic operations on numeric values . OPERATORS SYMBOL MEANING + Addition - Subtraction * Multiplication / Division % Remainder / Modulus ** Exponent // Floor Division
Exponentiation is a mathematical operation that involves raising a base number to the power of an exponent . Exponentiation is done using the ** operator Example : 23=2×2×2=8 result = 2 ** 3 print(result) # Output: 8
Floor Division Floor division is a division operation that divides two numbers and rounds down the result to the nearest integer . floor division is done using the // operator Example: 7÷2=3.57 Floor division: 7 // 2=3 result = 7 // 2 print(result) # Output: 3
2. Assignment Operators : The assignment operator is one of the most frequently used operators in Python. The operator consists of a single equal sign (=). it operates on two operands. The left-hand operand is typically a Variable, while the right-hand operand is an expression. number = 42 day = " Friday" digits = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) letters = ["a", "b", "c"]
3 . Relational or Co mparison Operators: Comparison operators are used to compare Numerical values and return True or False . The comparison operators are all binary. This means that they require left and right operands. These operators always return a Boolean value (True or False) that depends on the truth value of the comparison at hand. OPERATORS OPERATIONS SYNTAX == Equal to X == Y != Not Equal to X != Y < Less than X < Y <= Less than or equal to X <= Y > Greater than X >Y >= Greater than or equal to X >= Y
4. Logical Operators: Logical operators are used to perform logical operations on Boolean values. They allow you to combine or manipulate the results of comparisons . OPERATORS SYNTAX DESCRIPTION EXAMPLES And X and Y This returns True if both x and y are true x = 10 x > 5 and x < 15 Or X or Y This returns True if either x or y are true x = 10 x > 5 or x < 15 not not X Reverses a result, so if something is True , not turns it False x = 10 not(x > 5 and x < 15)
5 . Bitwise Operators: Bitwise operators perform bitwise operations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits. OPERATOR NAME DESCRIPTION SYNTAX & Bitwise AND Result bit 1,if both operand bits are 1;otherwise results bit 0. x & y | Bitwise OR Result bit 1,if any of the operand bit is 1; otherwise results bit 0. x | y ~ Bitwise NOT inverts individual bits ~x ^ Bitwise XOR Results bit 1,if any of the operand bit is 1 but not both, otherwise results bit 0. x ^ y >> Bitwise right shift The left operand’s value is moved toward right by the number of bits specified by the right operand. x>> << Bitwise left shift The left operand’s value is moved toward left by the number of bits specified by the right operand. x<<
6. Membership Operator : Membership operators in Python are operators used to test whether a value exists in a sequence, such as a list, tuple, or string . Membership operators are used to test if a sequence is presented in an object OPERATOR DESCRIPTION EXAMPLE in Returns True if a sequence with the specified value is present in the object x in y not in Returns True if a sequence with the specified value is not present in the object x not in y
7. Identity Operator : Identity operators in Python are used to compare the memory locations of two objects. There are two identity operators: is and is not . These operators return True if the specified objects have the same memory address, and False otherwise.
IDENTITY OPERATORS OPERATOR DESCRIPTION EXAMPLE is Returns true if both variables are the same object x is y Return true is not Returns true if both variables are not the same object x is not y Return false