Facebook   Tumblr   LinkedIn

Naming Variables in Python


Walden Systems Geeks Corner Tutorial Naming Variables in Python Rutherford NJ New Jersey NYC New York City North Bergen County
Python is a programming language that lets you work quickly and integrate systems more effectively.

In Python, like other programming languages, there is a way to store information to be referenced and manipulated called a variable. They provide a way to label data with a descriptive name, so our programs can be understood by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their purpose is to label and store data in memory. This data can then be used throughout our program.

In Python, variables are created when they are first assigned. Variables must be assigned before being used. The value stored in a variable can be accessed or updated later. A variable can hold strings, numbers, or any other type of data.In Python, no declaration is required which means that a data type doesn't need to be declared. The Python interpreter allocates memory depending on what type of data is stored in the variable.



We can't just use any name for a variable in Python. Variables must start with a letter or an underscore. After that, you can use any combination of letters, numbers and underscores. Just because variable names are case sensitive doesn't mean that we can use the same variable name with different case since it will make our code harder to read and the variable harder to recognize. Variable names are case sensitive so variable names SOME_VAR and some_var are two different variables. There is no limit on how long the variable name is but it should be short enough so we can recognize the variable and not too long that it will make our code unreadable. There is one more restriction with variables names, we can't use keywords. Keywords are some reserved words which we cannot use as a variable name because Python uses them for other things.

When creating variables, it's not just a matter of keeping the rules in mind. We must also consider in order to make the code easier to read and follow. Readability is very important, for example, lastname, lastName, and last_name are all valid variables but one is more readable than the others. Also be descriptive about what the variable holds, for example, x, variable_to_hold_the_first_name, and first_name are all valid variables but x isn't very descriptive, variable_to_hold_the_first_name is too much typing and has a lot of unnecessary words. Also avoid using lower case L, uppercase O and uppercase I since they can be confused with the numbers 1 and 0.

If you keep these simple things in mind, we can save ourselves time, effort and energy. We can make our code much easier to follow and read. This will make it easier to debug.