Numeric Data and Operators

Numeric Data Type

  • Python has three numeric data types: int, float, and complex
  • int (integer)
    1. Whole numbers (+ve, -ve or zero), but without decimal point
    2. Example: 10, -5, 0
    float
    1. Numbers with a decimal point (+ve, -ve or zero)
    2. Example: 3.14, -2.5, 0.1, 3.0
    complex
    1. Numbers with real and imaginary parts
    2. Example: 3 + 4j, -2.5 + 3.14j, 0 + 2j

Operators

  • Special symbols that perform operations on variables and values
  • Types: Assignment, Arithmetic, Comparison, Logical, Bitwise, Membership, Identity
  • Examples
    1. a = 10 b = 3 calc_1 = 3 + 2 print(calc_1) my_calc = a + 3 * (4 - 1) print(my_calc) calculation = (a + b) * (a - b) / 4 print(calculation)

Arithmetic Operators

Performs mathematical operations on numeric data
OperatorNameDescriptionExpressionResult
+AdditionAdd two numbers3 + 25
-SubtractionSubtract one number from another5 - 14
*MultiplicationMultiply two numbers4 * 28
/DivisionDivide one number by another7 / 41.75
//Floor DivisionDivide and round down to nearest integer10 // 33
%ModulusRemainder after division10 % 31
** ExponentRaise a number to the power of another2 ** 38
Arithmetic operators in Python

Assignment Operators

Assigns values to variables. Can also perform an operation and assignment in one step
OperatorNameDescriptionExpressionResult
=Simple AssignmentAssigns value on right to variable on leftx = 5Equivalent to x = 5
=Multiple AssignmentAssigns same value to multiple variablesx = y = z = 10Equivalent to x = 10; y = 10; z = 10
=Positional AssignmentAssigns multiple values to multiple variablesa, b, c = 1, 2, 3Equivalent to a = 1; b = 2; c = 3
+=Add and AssignmentAdds value on right to variable on leftx += 5Equivalent to x = x + 5
-=Subtract and AssignmentSubtracts value on right from variable on leftx -= 5Equivalent to x = x - 5
*=Multiply and AssignmentMultiplies value on right to variable on leftx *= 5Equivalent to x = x * 5
/=Divide and AssignmentDivides variable on left by value on rightx /= 5Equivalent to x = x / 5
//=Floor Divide and AssignmentFloor divides variable on left by value on rightx //= 5Equivalent to x = x // 5
%=Modulus and AssignmentTakes modulus of variable on left by value on rightx %= 5Equivalent to x = x % 5
**=Exponent and AssignmentRaises variable on left to power of value on rightx **= 5Equivalent to x = x ** 5
Assignment operators in Python

Comparison Operators

Compares two values and return a boolean result (True or False)
OperatorNameDescriptionExpressionResult
==Equal toReturns True if both operands are equal5 == 5True
!=Not equal toReturns True if both operands are not equal5 != 5False
<Less thanReturns True if left operand is less than right operand5 < 10True
>Greater thanReturns True if left operand is greater than right operand10 > 5True
<=Less than or equalReturns True if left operand is less than or equal to right one5 <= 5True
>=Greater than or equalReturns True if left operand is greater than or equal to right one10 >= 5True
Comparison operators in Python

Logical Operators

Combines multiple conditional statements and return a boolean result
OperatorNameDescriptionExpressionResult
andLogical ANDReturns True if both operands are True(5 < 10) and (4 > 5)True
orLogical ORReturns True if at least one operand is True(5 < 10) or (4 > 5)True
notLogical NOTReverses the result of the operandnot (5 < 10)False
Logical operators in Python

Membership Operators

Checks if a value is a member of a sequence (string, list, tuple, set, dictionary)
OperatorNameDescriptionExpressionResult
inMembership TestReturns True if value is found in the sequence"a" in "cat"True
not inMembership TestReturns True if value is not found in the sequence"cat" not in ["cat", "rat"]False
Membership operators in Python