Python variables and simple data types

Source: Internet
Author: User

1. What happens when you run hello_world.py

What did Python do when it ran hello_world.py? In fact, even if you run a simple program, Python does quite a lot of work:

# !/usr/bin/env python # -*-coding:utf-8-*- Print ("Hello Python world! ")
View Code

When you run the above code, you will see the following output:

Hello Python world!

When you run the file hello_world.py, the end of the. PY indicates that this is a Python program, so the compiler will use the Python interpreter to run it. The Python interpreter reads the entire program to determine the meaning of each of these words. For example, when you see the word print, the interpreter prints the contents of the parentheses to the screen, not the contents of the parentheses.

2. Variables

The following attempts to use a variable in hello_world.py. Add a line of code at the beginning of the file and modify the second line of code as follows:

# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jie.fang  "Hello Python world! " Print (message)
View Code

Run the program with the same output as before:

Hello Python world!

We have added a message variable. Each variable stores a value-the information associated with the variable. Here, the stored value is the text "Hello Python world!".

Adding a variable causes the Python interpreter to do more work. When the first line of code is processed, it will text "Hello Python world!" Associated with a variable message; When the second line of code is processed, it prints the value associated with the variable message to the screen.

The following is a further extension of this program: modify the hello_world.py so that it prints a message again. To do this, add a blank line to the hello_world.py and add the following two lines of code:

# !/usr/bin/env python # -*-coding:utf-8-*-  "Hello Python world! " Print  "Hello Python Crash Course world! " Print (message)
View Code

Now run this program and you will see two lines of output:

Hello Python world! Hello Python Crash Course world!
View Code

You can modify the value of a variable at any time in your program, and Python will always record the latest value of the variable.

2.1 Naming and use of variables

There are a few rules and guidelines to follow when using variables in Python. Violating these rules throws an error, and the guide is designed to make the code you write easier to read and understand. Be sure to keep the following rules about variables in mind.

    1. Variable names can contain only letters, numbers, and underscores. Variable names can begin with a letter or underscore, but cannot begin with a number, for example, you can message_1 a variable command, but you cannot command it as 1_message.
    2. Variable names cannot contain spaces, but you can use underscores to separate words in them. For example, the variable name greeting_message possible, but the variable greeting message throws an error.
    3. Do not use Python keywords and function names as variable names, that is, do not reserve words for special purposes, such as print, using Python.
    4. Variable names should be both brief and descriptive. For example, name is better than N, Student_name is better than S_n, Name_length is better than length_of_person_name.
    5. Use the lowercase l and the capital Letter o sparingly, as they may be seen by people as numbers 1 and 0.

Creating good variable names requires a certain amount of practice, especially when the program is complex and interesting. As more and more programs are written and you begin to read code written by others, you are becoming more adept at creating meaningful variable names.

note For now, you should use the lowercase python variable name. Using uppercase letters in variable names does not result in errors, but it is a good practice to avoid using capitals.

2.2 Avoid naming errors when using variable names

When writing a program, you should be aware of the incorrect variable name when defining references to variables and variables. The error example is as follows:

# !/usr/bin/env python # -*-coding:utf-8-*-  "Hello Python world! " Print (mesage)
View Code

The variable name defined in the code above is a message, but when the print reference is mesage, running the program will have the following error:

Traceback (most recent):   " d:/jetbrains/workplace/python3/0902/error.py "  in <module>    print'mesage' is  Not defined
View Code

The Python interpreter will do whatever it takes to help you figure out where the problem is, when there is an error in the program. When the program fails to run successfully, the interpreter provides a traceback. Traceback is a record that indicates where the interpreter encountered an error while trying to run the code. Here is a traceback that has just referred to the wrong variable name:

Traceback (most recent):
File "d:/jetbrains/workplace/python3/0902/error.py", line 7, <module>
Print (mesage)
Nameerror:name ' mesage ' is not defined

The computer is meticulous, but does not care about spelling correctly. So when creating variable names and writing code, you don't have to consider spelling and grammar rules in English.

Many programming errors are simple, except that one character is printed on one line of the program. It took a long time to find out the mistake.

Note   The best way to understand the new programming concepts is to try to use them in your program.

Not to be continued ...

Python variables and simple data types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.