Variables and Operators
In Python, variables are used to store data values. These data values can be manipulated using operators.
Variables
A variable is a name that refers to a value stored in memory. In Python, variables are created by assigning a value to a name. Here's an example:
x = 42
In this example, the variable x is assigned the value 42. The equal sign (=) is used to assign a value to a variable.
Variable names can contain letters, numbers, and underscores, but they cannot start with a number. Variable names are case-sensitive, so x and X are two different variables.
Operators
Operators are used to perform operations on values stored in variables. Python supports several types of operators, including:
- Arithmetic operators
- Comparison operators
- Assignment operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numbers. Python supports the following arithmetic operators:
- '+' (addition)
- '-' (subtraction)
- '*' (multiplication)
- '/' (division)
- '%' (modulus)
- '**' (exponentiation)
- '//' (floor division)
Here are some examples of arithmetic operations in Python:
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3
Comparison Operators
Comparison operators are used to compare values. Python supports the following comparison operators:
- == (equal to)
- != (not equal to)
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to))
Here are some examples of comparison operators in Python:
x = 10
y = 3
print(x == y) # Output: False
print(x != y) # Output: True
print(x < y) # Output: False
print(x > y) # Output: True
print(x <= y) # Output: False
print(x >= y) # Output: True
Assignment Operators
Assignment operators are used to assign values to variables. Python supports several assignment operators, including:
- = (simple assignment)
- += (addition assignment)
- -= (subtraction assignment)
- *= (multiplication assignment)
- /= (division assignment)
- %= (modulus assignment)
- **= (exponentiation assignment)
- //= (floor division assignment)
Here are some examples of assignment operators in Python:
x = 10
x += 5
print(x) # Output: 15
x -= 3
print(x) # Output: 12
x *= 2
print(x) # Output: 24
x /= 4
print(x) # Output: 6.0
x %= 3
print(x) # Output: 0.0
Logical Operators
Logical operators are used to combine boolean expressions. Python supports three logical operators:
- and (logical AND)
- or (logical OR)
- not (logical NOT)
Here are some examples of logical operators in Python:
x = True
y = False
print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False
Identity Operators
Identity operators are used to compare the memory locations of two objects. Python supports two identity operators:
- is (true if the operands are identical)
- is not (true if the operands are not identical)
Here are some examples of identity operators in Python:
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is y) # Output: False
print(x is not y) # Output: True
print(x is z) # Output: True
Membership Operators
Membership operators are used to test whether a value is a member of a sequence. Python supports two membership operators:
- in (true if the value is found in the sequence)
- not in (true if the value is not found in the sequence)
Here are some examples of membership operators in Python:
x = [1, 2, 3]
print(2 in x) # Output: True
print(4 not in x) # Output: True
Bitwise Operators
Bitwise operators are used to perform bitwise operations on integers. Python supports the following bitwise operators:
- & (bitwise AND)
- | (bitwise OR)
- ^ (bitwise XOR)
- ~ (bitwise NOT)
- << (left shift)
- >> (right shift)
Here are some examples of bitwise operators in Python:
x = 10
y = 5
print(x & y) # Output: 0
print(x | y) # Output: 15
print(x ^ y) # Output: 15
print(~x) # Output: -11
print(x << 1) # Output: 20
print(y >> 1) # Output: 2
Conclusion
We had just covered the basics of variables and operators in Python. We looked at how variables are created and used, and we covered the different types of operators available in Python, including arithmetic operators, comparison operators, assignment operators, logical operators, identity operators, membership operators, and bitwise operators. By mastering these fundamental concepts, you'll be well on your way to becoming proficient in Python programming.
Basic Topics
- Introduction to Python
- Basic Syntax and Data Types
- Variables and Operators
- Input
- Conditional statements
- Loops
- Functions
- List
- Tuples
- Sets
- Dictionary
- Modules
- Packages
- Exception Handling
- Read/Write Files