Sitemap

Variables in Python

3 min readDec 15, 2024

--

Variables are fundamental to any programming language, including Python. They act as containers for storing data values, which can then be manipulated throughout your code. Today, we’ll explore the basics of variables in Python, how to use them effectively, and some advanced concepts like scope.

1. Creating Variables

In Python, creating a variable is straightforward. You simply assign a value to a name using the = operator. Python is dynamically typed, so you don’t need to specify the type of the variable explicitly.

# Creating variables
x = 10 # integer value
name = 'product name' # string value
price = 10.20 # float value

Here, x, name, and price are variables holding an integer, a string, and a float value, respectively.

2. Using Quotes (Single and Double)

Python allows you to use both single (') and double (") quotes to create string variables. Both are interchangeable, but you can choose one over the other depending on the need to include quotes within the string.

# Using single quotes
greeting = 'Hello, World!'

# Using double quotes
quote = "Python is fun!"

# Mixing quotes for embedded quotes
message = 'She said, "Python is amazing!"'
alternative = "It's a great day!"

By combining single and double quotes, you can avoid escape sequences, making your code cleaner.

3. Case-Sensitive

Variable names in Python are case-sensitive. This means age, Age, and AGE are treated as three distinct variables.

# Case-sensitive variables
name = "Alice"
Name = "Bob"
NAME = "Charlie"

print(name) # Outputs: Alice
print(Name) # Outputs: Bob
print(NAME) # Outputs: Charlie

To avoid confusion, stick to a consistent naming convention like snake_case or camelCase.

  • snake_case: Words are written in lowercase and separated by underscores (e.g., my_variable_name). This is commonly used in Python for variable names, functions, and file names.
  • camelCase: Words are written without spaces, and each word after the first starts with an uppercase letter (e.g., myVariableName). This is popular in languages like JavaScript for variable and function names.

4. Display Variables : print() function

Get Krishna Bhuva’s stories in your inbox

Join Medium for free to get updates from this writer.

You can display the value of a variable using the print() function. This is especially useful for debugging or displaying output to users.

# printing variables
x = 42
greeting = "Hello, Python!"

print(x) # Outputs: 42
print(greeting) # Outputs: Hello, Python!

You can also combine strings and variables using string formatting methods:

name = "Alice"
age = 25

# Using f-strings
print(f"My name is {name} and I am {age} years old.")

# Using the format() method
print("My name is {} and I am {} years old.".format(name, age))

5. Assigning Multiple Values

Python allows you to assign multiple values to multiple variables in a single line, making your code more streamlined and efficient.

# Assigning multiple values
x, y, z = 1, 2, 3

print(x) # Outputs: 1
print(y) # Outputs: 2
print(z) # Outputs: 3

# Assigning the same value to multiple variables
a = b = c = 100
print(a, b, c) # Outputs: 100 100 100

6. Global and Local Variables

Variables in Python can be either local or global, depending on their scope.

  • Local Variables: Defined inside a function and accessible only within that function.
  • Global Variables: Defined outside any function and accessible throughout the script.
# Global variable
global_var = "I am global"

def my_function():
# Local variable
local_var = "I am local"
print(local_var) # Accessible here
print(global_var) # Accessible here too

my_function()

# Accessing global variable outside function
print(global_var) # Outputs: I am global

# Accessing local variable outside function
# print(local_var) # Error: NameError: name 'local_var' is not defined

Modifying a global variable inside a function, using the global keyword.

x = 10

def modify_global():
global x
x = 20

modify_global()
print(x) # Outputs: 20
  • Without the global keyword, Python treats variables inside a function as local by default, meaning they create a new variable within the function's scope.
  • Changes made to such local variables do not impact variables defined in the global scope, keeping them unaffected.

Conclusion

Mastering the creation and use of variables in Python, along with understanding scope and case sensitivity, is fundamental for tackling more advanced programming tasks. Variables play a crucial role in coding as they provide a way to store, manipulate, and reuse data, making programs more dynamic and efficient. By practicing these examples, you can deepen your comprehension and gain insight into how variables behave in various contexts.

--

--