Python Variables
$count++; if($count == 1) { include "../mobilemenu.php"; } ?> if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Variables are fundamental in Python, as they allow you to store and manipulate data. A variable is a symbolic name representing a value. In Python, variables are dynamically typed, meaning you don’t need to declare the type explicitly. Below, we’ll cover variable naming conventions, assignment, common data types, and working with variables in various operations.
1. Assigning Variables
In Python, assigning a value to a variable is as simple as using the `=` operator. Python will automatically detect the data type.# Assigning variables
name = "Alice" # String
age = 30 # Integer
height = 5.9 # Float
is_student = True # Boolean
print(name)
print(age)
print(height)
print(is_student)
Output:
Alice
30
5.9
True
2. Naming Variables
Python variables follow specific naming rules:- Must begin with a letter (a–z, A–Z) or an underscore (`_`).
- Cannot start with a number.
- Can only contain alphanumeric characters and underscores.
- Are case-sensitive (e.g., `age` and `Age` are different).
Examples of Valid and Invalid Variable Names:
# Valid names
user_name = "John"
_age = 25
height_cm = 170
# Invalid names (these would raise syntax errors)
# 2name = "Alice" # Cannot start with a number
# user-name = "Bob" # Hyphens are not allowed
# class = "Python" # 'class' is a reserved keyword
3. Common Data Types in Variables
Python supports several built-in data types:Strings (`str`)
Strings are sequences of characters, enclosed in single (`'`) or double (`"`) quotes.greeting = "Hello, World!"
print(greeting)
Output:
Hello, World!
Integers (`int`)
Integers are whole numbers, positive or negative, without a decimal point.count = 10
print(count)
Output:
10
Floats (`float`)
Floats are numbers that have a decimal point.temperature = 36.6
print(temperature)
Output:
36.6
Booleans (`bool`)
Booleans represent truth values: `True` or `False`.is_open = True
print(is_open)
Output:
True
4. Assigning Multiple Variables
Python allows you to assign multiple variables at once.x, y, z = 5, 10, 15
print(x)
print(y)
print(z)
Output:
5
10
15
You can also assign the same value to multiple variables in one line:
a = b = c = 20
print(a)
print(b)
print(c)
Output:
20
20
20
5. Changing Variable Types
Variables in Python are dynamically typed, so you can change their type by assigning a new value.my_var = "Python" # Initially a string
print(my_var)
my_var = 3.14 # Now a float
print(my_var)
my_var = 42 # Now an integer
print(my_var)
Output:
Python
3.14
42
6. Working with Variables in Operations
Variables can be used in arithmetic operations, string concatenation, and more.Arithmetic Operations
a = 10
b = 5
sum_result = a + b
diff_result = a - b
prod_result = a * b
div_result = a / b
print("Sum:", sum_result)
print("Difference:", diff_result)
print("Product:", prod_result)
print("Division:", div_result)
Output:
Sum: 15
Difference: 5
Product: 50
Division: 2.0
String Concatenation
first_name = "Alice"
last_name = "Johnson"
full_name = first_name + " " + last_name
print(full_name)
Output:
Alice Johnson
7. Variable Scope
In Python, variables have a scope that determines where they can be accessed. Variables declared inside a function are local to that function, while variables declared outside are global.def my_function():
local_var = "I'm local" # Local scope
print(local_var)
global_var = "I'm global" # Global scope
my_function()
print(global_var)
Output:
I'm local
I'm global
8. Deleting Variables
You can delete a variable using the `del` keyword. Once deleted, accessing the variable will raise an error.x = 10
print(x)
del x
# print(x) # Uncommenting this will raise a NameError
Output:
10
NameError: name 'x' is not defined (if attempting to access `x` after deletion)
Summary
Variables in Python are flexible and support various data types and operations. Understanding variables and their scope, naming conventions, and how to manage them is essential for effective Python programming.