top of page

Python Variables

In Python, a variable is a named location in memory used to store a value and can be reassigned to different values. Variables make our code more readable, maintainable, and reusable to store temporary data or intermediate results during the execution of the program.


Declaring a variable in Python is very simple; you assign a value to a name using the assignment operator `=`. For example:

x = 5
y = "Hello World"
z = 3.1415

In the above example, x is assigned the integer 5, y is assigned the string "Hello World," and z is assigned a floating point of 3.1415


Once a variable is assigned a value, it can be used throughout the program, and its value can be modified if needed. For example, you can change the value of x to another integer:

x=10

You can also use the value of one variable to define another variable:

a=x


Naming Conventions

In Python, variable names must follow certain naming conventions to ensure they are valid and easy to understand. Here are some guidelines for defining variable names in Python:

  • Variable names can only contain underscores, numbers, and letters

  • Variable names must start with a letter or an underscore

  • Variable names should not contain spaces or special characters, except for the underscore.

  • Variable names should be in lowercase letters and using underscores to separate words, for example, `my_variable`

  • Python has a list of reserved keywords that cannot be used as variable names. For example, 'and,' 'or,' and 'not' are some keywords that cannot be used as variable names.

  • Variable names should be meaningful and self-explanatory. Use descriptive names, such as `user_name` or `is_active`

For example, the following are valid variable names in Python:

  • my_variable

  • user_name

  • is_active

  • x_coordinate

  • _private_variable

While the following examples are not valid variable names:

  • 2nd_variable (starts with a number)

  • my-variable (contains a special character)

  • user name (contains a space)

  • and (a Python keyword)

Although Python does not impose any particular naming standards, it is a good idea to adhere to some form of naming convention throughout your program to improve the readability of your code. You can choose one specific naming convention, such as camelCase or snake case. Snake case is the preferred naming pattern when writing python programs, as many developers widely use it.



A Little More Information

In Python, variables are dynamically typed, meaning the data type associated with a variable can change depending on the value assigned. Generally, you won't see a data type such as int, string, or bool declared for a variable because the specified type provided will not be enforced. Before looking at the following example in the Python Console, ensure you understand what data types are. Check out this article if you're unsure.


We start by creating a variable `x` and assigning it to a string value of "Python."

x= "Python"

If you run the following command:

type(x) # <class 'str'>

You'll see that the data type associated with the variable is 'str' or string. We can then assign the value of `x` to a different data type, an integer value of 5.

x=5

Running the following command:

type(x) # <class 'int'>

Looking at the output given by the `type` function, you can see the data type associated with the variable `x` is an 'int' (integer). Assigning one data type to a variable and then assigning a completely different data type later in the program is not typical of other popular programming languages, such as Java or C. Using Python's type hints module, you're able to specify the data type which should be stored in the variable. Here's an example of how you can specify the variable `x` should store an integer value:

x: int = 5

Remember, though, the variable is not guaranteed to be an integer because we can just as easily store a string value in the variable `x` even after noting the variable stores an integer. This becomes very helpful when using a decent IDE such as PyCharm. The IDE will warn you if you're assigning a string value to the variable `x,` which might help you avoid some headaches.




3 views0 comments
bottom of page