Variable in Python

Tram Ho

Related posts:

Instructions for installing Python on Windows MacOS and Linux

What happens when you run the file hello_world.py

We will take a closer look at how Python works when you run the file hello_world.py .

Python does a fairly large amount of work, even when it runs simple programs.

File name: hello_world.py

When you run this code, you will see the following result:

When you run the file hello_world.py, the .py extension indicates that the file is Python.

The text editor will run the file using the Python interpreter, and will read what each word means.

For example, the interpreter sees the word print followed by () , the program prints anything in () .

As you write the program, your editor will highlight different parts of your program.

For example, the editor will recognize that print is the name of a function and it will display a color for it. The phrase “Hello Python world!” is not a Python code and should display a different color. This function is called syntax highlighting , which is quite useful when writing programs.

syntax highlight

Viable (Viriable)

We will start using variables in the file hello_world.py.

Add a line to the beginning of the file, and edit line 2

Running the program you will see the results appear as you see the previous section

We add a variable named message . Each variable will be connected to a value, which is information related to that variable. In this case the value is Hello Python world! .

Adding 1 variable will increase the workload of the Python interpreter.

Once the first line has been processed, the message variable will connect to the Hello Python world! .

When it comes to the second line, it will print the value connected to the message variable to the screen.

We will extend this program by printing a second result. Add a blank line to hello_world.py, and then add the following two lines.

Hello code

Now, when you run the file hello_world.py, the output will look like this:

You can change the value of the variable at any time, and Python will always store the current value.

Name and use variables

When you use variables in Python, you need to follow a few rules and instructions.

Breaking the rule will cause an error; The tutorial will help you write code that is easier to read and understand.

You must memorize the following rules and guidelines:

Rule:

  • Variable names can only contain letters, numbers and underscores _ . Can start with a letter or an underscore, but cannot start with a number. For example, you can name the variable message_1 but not 1_message .
  • Space is not allowed in variable names, but you can use underscores to separate words. For example, the greeting_message variable will run, but the greeting message variable will fail.
  • When naming variables, don’t use Python keywords and function names. That is, do not use words that are reserved for programming by Python, such as print .

Tutorial:

  • Variable names should be short, but full of meaning. For example, name will be better than n , student_name will be better than s_n , and name_length will be better than length_of_persons_name .
  • Be careful when using lowercase l or capital O because it will be confusing with numbers 1 and 0 .

You have to think carefully to create good variable names, which will make your code easy to read, understand, and maintain, especially as your program gets more complex. Or you can learn by reading someone else’s code, checking what you’ve learned and if the variable name is easy to understand, should it be changed to another name?

Note: The Python variables you use must be lowercase. You won’t get an error if you use capital letters, but capital letters in variable names will have special meaning (discussed in the next section).

Common mistakes when using variables

Every programmer makes mistakes, and most things make mistakes every day.

Although good programmers can make mistakes, they know how to solve bugs most effectively.

Consider a common mistake, and learn to fix it.

We will write the code with the purpose of making an error. Enter the code below, including mesage spelling errors

When the error appears in Python, the Python interpreter will help you find where the error is.

The interpreter provides a traceback (trace) when the program fails to run. A traceback is a record when the interpreter runs faulty.

Below is the traceback example that Python provides after you enter the wrong variable name:

traceback

Traceback said that: error at line 2 at print(mesage) and variable name mesage not been defined. Python does not recognize the variable name. The NameError error is often because you forgot to set the value for the variable before using it or you entered the name of the variable with misspelling. In this case, the variable name is misspelled.

In this example we have dropped a letter s in the message variable in line 2. Python does not check the spelling of the letter, but it ensures that the variable name is used correctly.

For example, you change the message from line 1 to a misspelled mesage .

In this case the program is still running successfully

The programming language checks strictly, but does not check spelling. Therefore, you do not need proper spelling or grammar when you create a variable name and write code.

Many errors are simple, like missing a letter. If you take a long time to understand these errors, it will be very helpful for you. Many experienced programmers but it takes hours to find such simple errors.

Variables are labels

Variables are described as your value storage box. This idea may help you when you first use variables, but it is not a properly described way of variables in Python.

It is better if you consider the variable as a label to assign the value to. You might consider this: the parameter refers to a certain value.

This may not seem like a difference when you first create your program, but it will help you later.

In the future, you will see how the variable doesn’t work as you expect it to, and a proper understanding of how the variable will work will help you realize what’s going on with the code.

Note: The best way to understand new programming concepts is to try using them in the interpreter. If you have difficulty doing exercises, you often take a break, and do something for a while. If it still does not work, please review the theory above.

Exercise: Perform the following 2 exercises with 2 different programs, and the naming must follow the conventions of Python, use lowercase letters under underscores, for example: simple_program

Sentence 1: Assign a value to a variable, and output that value. Sentence 2. Assign a value to a variable, and output that value. Then change the value of the variable and output the changed value.

summary

In this section, you learned how to declare variables, how to name variables meaningfully, and how to resolve problems when you encounter errors.

If you have problems installing, please comment below, I will support you as soon as possible!

Thank you for your interest in this article.

Refer

PYTHON CRASH COURSE – A Hands On Project Based Introduction To Programming (Eric Matthes).

Share the news now

Source : Viblo