Variables are essential elements in Python and virtually every programming language. They serve as containers for storing data values that can be manipulated and referenced throughout a program. In Python, variables come with dynamic typing, meaning you don’t need to declare their types explicitly. This flexibility simplifies coding but requires careful attention to variable naming and usage.
What are Variables?
In Python, a variable is essentially a named reference to a memory location where data is stored. You can think of variables as labels attached to values. When you assign a value to a variable, you’re instructing Python to reserve memory space for that value and associate it with the variable name.
Variable Naming Rules
Python enforces specific rules for variable naming:
- Valid Characters: Variable names can consist of letters (both lowercase and uppercase), numbers, and underscores.
- Start with a Letter or Underscore: The first character of a variable name must be a letter (a-z, A-Z) or an underscore (_).
- Case-Sensitive: Python distinguishes between lowercase and uppercase letters, so
myVar
,MyVar
, andmyvar
are considered different variables.
Variable Assignment
Assigning a value to a variable is straightforward in Python. You use the assignment operator (=
) to bind a value to a variable name.
x = 5
In this example, we assign the integer value 5
to the variable x
.
Data Types in Python
Python supports various data types, including:
- int: Integer values (e.g.,
5
,100
,-10
). - float: Floating-point values (e.g.,
3.14
,2.718
). - str: String values (e.g.,
'hello'
,"world"
). - bool: Boolean values (
True
orFalse
). - list: Ordered collection of items (e.g.,
[1, 2, 3]
). - tuple: Immutable ordered collection of items (e.g.,
(1, 2, 3)
). - dict: Collection of key-value pairs (e.g.,
{'name': 'John', 'age': 30}
).
Basic Variable Operations
Once you’ve assigned values to variables, you can perform various operations on them.
Arithmetic Operations
Python supports standard arithmetic operations such as addition, subtraction, multiplication, division, and modulus.
a = 10
b = 5
# Addition
sum = a + b
# Subtraction
difference = a - b
# Multiplication
product = a * b
# Division
quotient = a / b
# Modulus (remainder)
remainder = a % b
String Operations
Strings in Python support concatenation and repetition operations.
first_name = "John"
last_name = "Doe"
# Concatenation
full_name = first_name + " " + last_name
# Repetition
greeting = "Hello " * 3
Variable Reassignment
In Python, you can reassign a variable to a new value at any time.
x = 5
print(x) # Output: 5
x = 10
print(x) # Output: 10
Variable Scope
Variable scope determines where in your code a variable is accessible. Python distinguishes between global and local variables.
- Global Variables: Declared outside of any function, global variables are accessible throughout the entire program.
- Local Variables: Declared inside a function, local variables are only accessible within that function.
global_var = 10
def my_function():
local_var = 20
print(global_var) # Accessing global variable
print(local_var) # Accessing local variable
my_function()
print(global_var) # Output: 10
print(local_var) # Error: NameError: name 'local_var' is not defined
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.