Python User Input Processing and Operator Evaluation
Capturing User Input
The input() function pauses execution to accept data from the console. It always evaluates to a string, regardless of what the user types.
user_data = input("Enter your preferred programming language: ")
print(user_data, type(user_data))
To perform mathematical operations on user input, convert the string using int() or float().
num1 = int(input("Provide the first integer: "))
num2 = int(input("Provide the second integer: "))
print(f"Sum: {num1 + num2}")
Operator Categories
Arithmetic Operators
Standard math operations include addition, subtraction, multiplication, true division, floor division, modulo, and exponentiation.
print(15 + 5) # 20
print(15 - 5) # 10
print(4 * 5) # 20
print(15 / 4) # 3.75
print(15 // 4) # 3 (Floor division)
print(15 % 4) # 3 (Modulo/Remainder)
print(3 ** 4) # 81 (3 to the power of 4)
When mixing positive and negative numbers, floor division rounds down towards negative infinity. The modulo result takes the sign of the divisor.
print(-15 // 4) # -4
print(-15 % 4) # 1 (Because -15 = 4 * (-4) + 1)
Assignment Operators
Values are assigned using =. Multiple variables can share the same value via chained assignment.
x = y = z = 100
Augmented assignments combine arithmetic with assignment, modifying the variable in place.
val = 50
val += 20 # val is now 70
val -= 10 # val is now 60
val *= 2 # val is now 120
val /= 4 # val is now 30.0 (division results in float)
val //= 3 # val is now 10.0
val %= 6 # val is now 4.0
Unpacking assignment allows mapping multiple values to multiple variables in one line. This also enables swapping values without a temporary variable.
p, q, r = 10, 20, 30
p, q = q, p # Swaps the values of p and q
Comparison Operators
These evaluate to boolean values (True or False). The == operator checks for value equality, while is checks for memory identity (whether two references point to the exact same object).
m, n = 50, 100
print(m > n) # False
print(m < n) # True
print(m == n) # False
print(m != n) # True
list_a = [1, 2, 3]
list_b = [1, 2, 3]
print(list_a == list_b) # True (values are identical)
print(list_a is list_b) # False (different objects in memory)
Boolean Operators
Logical operators and, or, and not evaluate complex conditions. Membership operators in and not in check for existence within sequences.
a, b = 10, 20
print(a == 10 and b == 20) # True
print(a < 5 or b > 15) # True
print(not False) # True
chars = "python"
print('t' in chars) # True
print('z' not in chars) # True
Bitwise Operators
Bitwise operators work on the binary representations of integers.
print(6 & 3) # 2 (Bitwise AND: 110 & 011 = 010)
print(6 | 3) # 7 (Bitwise OR: 110 | 011 = 111)
print(6 << 1) # 12 (Left shift: multiply by 2^1)
print(6 >> 1) # 3 (Right shift: divide by 2^1)
Operator Precedence
Evaluation follows a strict hierarchy: Parentheses take the highest priority, followed by Exponentiation, Unary operators, Multiplication/Division, Addition/Subtraction, Bitwise shifts, Comparison operators, Logical NOT/AND/OR, and finally Assignment operators.