Variables

In this tutorial, we will learn about Python variables. We will cover the following topics:

  • What is a variable
  • How to create a variable
  • Casting
  • Using print to output variables
  • Getting the type of a variable
  • Using single or double quotes for strings
  • Case sensitivity
  • Naming rules and conventions
  • Assigning multiple values
  • One value to multiple variables
  • Unpacking a collection
  • The global keyword and global variables

What is a Variable?

A variable in Python is a container for storing data values. Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

Creating a Variable

To create a variable in Python, you simply need to assign a value to a variable name using the assignment operator (=).

x = 5
y = "Hello, World!"

Here, we've created two variables: x with a value of 5 and y with a value of "Hello, World!".

Casting

If you want to specify the data type of a variable, you can use casting. Python is an object-oriented language, and data types are classes. You can use the constructor functions to create a specific data type:

x = int(5)       # x will be 5 (integer)
y = float(5)     # y will be 5.0 (float)
z = str("5")     # z will be '5' (string)

Using Print to Output Variables

To output the value of a variable, you can use the print() function:

x = "Hello, World!"
print(x)  # Output: Hello, World!

You can also use formatted strings (f-strings) to include variable values in your output:

x = 5
y = 10
print(f"The value of x is {x} and the value of y is {y}")
# Output: The value of x is 5 and the value of y is 10

Getting the Type

You can use the type() function to get the data type of a variable:

x = 5
y = "Hello, World!"
print(type(x))  # Output: <class 'int'>
print(type(y))  # Output: <class 'str'>

Using Single or Double Quotes

In Python, you can use either single quotes (') or double quotes (") to represent strings.

x = 'Python'
y = "Python"

Both x and y have the same value, "Python". You can choose either single or double quotes based on your preference, but it's important to be consistent in your code.

Case Sensitivity

Variable names in Python are case-sensitive. This means that x and X are considered different variables.

x = 5
X = "Hello, World!"
print(x)  # Output: 5
print(X)  # Output: Hello, World!

Naming Rules and Conventions

  • Variable names must start with a letter or the underscore character (_).
  • Variable names cannot start with a number.
  • Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
  • Variable names are case-sensitive (x, X, and x_1 are three different variables).

In addition to these rules, it's a good practice to use descriptive variable names and follow the conventions for naming:

  • Use lowercase letters and separate words with underscores (e.g., first_name, last_name).
  • Avoid using single-character variable names, except for common cases like loop indices (e.g., i, j).

Assigning Multiple Values

You can assign multiple values to multiple variables in a single line of code:

x, y, z = 1, 2, 3
print(x)  # Output: 1
print(y)  # Output: 2
print(z)  # Output: 3

One Value to Multiple Variables

You can assign one value to multiple variables in a single line of code:

x = y = z = 5
print(x)  # Output: 5
print(y)  # Output: 5
print(z)  # Output: 5

Unpacking a Collection

If you have a collection of values (e.g., a list, tuple, or set), you can use the unpacking technique to assign its elements to separate variables:

coordinates = (1, 2, 3)
x, y, z = coordinates
print(x)  # Output: 1
print(y)  # Output: 2
print(z)  # Output: 3

Using Print to Output Variables

To output the value of a variable, you can use the print() function:

x = "Hello, World!"
print(x)  # Output: Hello, World!

You can also use formatted strings (f-strings) to include variable values in your output:

x = 5
y = 10
print(f"The value of x is {x} and the value of y is {y}")
# Output: The value of x is 5 and the value of y is 10

The Global Keyword and Global Variables

In Python, variables created inside a function are considered local to that function and can't be accessed from outside the function. If you want to create a variable that can be accessed from anywhere in your code, you need to create a global variable.

To create a global variable, use the global keyword:

def set_global_variable():
    global x
    x = "Hello, World!"

set_global_variable()
print(x)  # Output: Hello, World!

In this example, we created a global variable x inside the set_global_variable() function. Now, we can access x from outside the function as well.

If you need to access a global variable inside a function and modify its value, you should also use the global keyword:

x = "Hello, World!"

def modify_global_variable():
    global x
    x = "Hello, Python!"

print(x)  # Output: Hello, World!
modify_global_variable()
print(x)  # Output: Hello, Python!

In this example, we first defined a global variable x with the value "Hello, World!". Then, we created a function modify_global_variable() to modify the value of x. After calling the function, the value of x is updated to "Hello, Python!".

Conclusion

In this tutorial, we have covered the basics of Python variables, including their creation, naming rules and conventions, assigning and unpacking values, outputting variables using the print() function, and understanding local and global variables. With this knowledge, you are well-equipped to work with variables in your Python programs and develop a deeper understanding of Python programming concepts.