Unit 1:
Python Variables, Operators, and Expressions
1. Introduction to Variables
• Definition: A variable is a container for storing data values. In Python,
variables do not need explicit declaration.
• Characteristics:
o Variables are created when a value is assigned to them.
o Python is dynamically typed, so you don’t need to specify the
type.
Declaring and Assigning Variables
x = 10 # Integer
name = "John" # String
is_valid = True # Boolean
• Variables can store different types of data (e.g., numbers, strings,
lists).
Variable Naming Rules
1. Must begin with a letter or underscore (_).
2. Cannot start with a number.
3. Can only contain alphanumeric characters and underscores.
4. Case-sensitive (e.g., age and Age are different).
Best Practices
• Use descriptive names: age instead of a.
• Follow conventions like snake_case (e.g., total_amount).
2. Python Operators
Operators are symbols used to perform operations on variables and values.
2.1 Arithmetic Operators
Operator Description Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
// Floor Division x // y
% Modulus (Remainder) x%y
** Exponentiation x ** y
2.2 Comparison Operators
Operator Description Example
== Equal to x == y
!= Not equal to x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
2.3 Logical Operators
Operator Description Example
and Logical AND x > 5 and x < 10
or Logical OR x > 5 or y < 10
not Logical NOT not(x > 5)
2.4 Assignment Operators
Operator Description Example
= Assign x=5
+= Add and assign x += 3
-= Subtract and assign x -= 3
*= Multiply and assign x *= 3
/= Divide and assign x /= 3
2.5 Bitwise Operators
Operator Description Example
& Bitwise AND x&y
| Bitwise OR x|y
^ Bitwise XOR x^y
~ Bitwise NOT ~x
<< Left Shift x << 2
>> Right Shift x >> 2
Bitwise Operator (Examples)
x=5 # 0101 in binary
y=3 # 0011 in binary
# Bitwise AND
print(x & y) # 0001 -> 1
# Bitwise OR
print(x | y) # 0111 -> 7
# Bitwise XOR
print(x ^ y) # 0110 -> 6
# Bitwise NOT
print(~x) # -6 (In two’s complement, it’s -(x+1))
# Left Shift (Shifts bits left by 2 places)
print(x << 2) # 10100 -> 20
# Right Shift (Shifts bits right by 2 places)
print(x >> 2) # 0001 -> 1
2.6 Membership Operators
Operator Description Example
in Returns True if a value exists 'a' in 'apple'
not in Returns True if a value does not exist 'z' not in 'apple'
3. Expressions
• Definition: An expression is a combination of values, variables, and
operators that evaluates to a value.
• Example:
x = 10
y=5
result = (x + y) * 2 # Expression
• Key Points:
o Expressions can be simple (e.g., 5 + 3) or complex (e.g., (x * y) +
z).
o The evaluation of expressions follows the operator precedence.
Operator Precedence
1. Parentheses ()
2. Exponentiation **
3. Multiplication, Division, Floor Division, Modulus * / // %
4. Addition, Subtraction + -
5. Bitwise Operators & | ^ << >>
6. Comparison Operators ==, !=, >, <, >=, <=
7. Logical NOT not
8. Logical AND and
9. Logical OR or
Example
x=3+4*2 # Multiplication happens first
print(x) # Output: 11
y = (3 + 4) * 2 # Parentheses alter precedence
print(y) # Output: 14
4. Practice Exercises
1. Declare a variable to store your name and print it.
2. Write a program to calculate the area of a rectangle using variables.
3. Evaluate the following expression and explain the output:
result = 10 - 4 * 2 / 2 + 1
print(result)
4. Use and, or, and not to write conditions for a simple login system.
5. Demonstrate the use of bitwise AND, OR, and XOR operations with
integer values.