Python variables and simple data types, python Variables

Source: Internet
Author: User

Python variables and simple data types, python Variables
1. events that occur when hello_world.py is run

What did Python do when running hello_world.py? In fact, Python does a lot of work even if it runs a simple program:

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

When running the above Code, you will see the following output:

Hello Python world!

When running the file hello_world.py, The. py at the end 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 and determines the meaning of each word. For example, when the word print is displayed, the interpreter prints the content in the brackets to the screen without worrying about the content in the brackets.

2. Variables

Next we try 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 shown below:

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

Run this program and output the same as before:

Hello Python world!

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

Adding variables causes the python interpreter to need more work. When processing the first line of code, it will text "Hello Python world! "Associates with the variable message. when processing the second line of code, it prints the value associated with the variable message to the screen.

Next we will further expand this program: Modify hello_world.py to print another message. To this end, add an empty line in hello_world.py and then add the following two lines of code:

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

Run this program now and you will see two rows of output:

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

You can modify the value of a variable at any time in the program, while python always records the latest value of the variable.

 

2.1 naming and use of Variables

When using variables in python, you need to follow some rules and guidelines. Violation of these rules will lead to errors, and the Guide aims to make the code you write easier to read and understand. Remember the following variables.

To create a good variable name, some practices are required, especially when the program is complex and interesting. As more and more programs are written and the code written by others are started, more and more good at creating meaningful variable names.

Note:For the moment, lower-case python variable names should be used. Using uppercase letters in variable names does not cause errors, but it is a good habit to avoid using uppercase letters.

 

2.2 avoid naming errors when using variable names

When writing a program, make sure that the variable name is incorrect when defining variables and variables. The error example is as follows:

#!/usr/bin/env python# -*- coding:utf-8 -*-message = "Hello Python World!"print(mesage)
View Code

The variable name defined in the Code above is message, but it is mesage when print references it. When running this program, the following error will be reported:

Traceback (most recent call last):  File "D:/JetBrains/WorkPlace/Python3/0902/error.py", line 7, in <module>    print(mesage)NameError: name 'mesage' is not defined
View Code

When a program has an error, the python interpreter will do its best to help you identify the problem. When the program fails to run, the interpreter provides a Traceback. Traceback is a record that indicates where the interpreter encountered an error while trying to run the code. The following is a Traceback with the variable name reference error:

Traceback (most recent call last ):
File "D:/JetBrains/WorkPlace/Python3/0902/error. py", line 7, in <module>
Print (mesage)
NameError: name 'mesage' is not defined

 

The computer is meticulous but does not care whether the spelling is correct. Therefore, when creating variable names and writing code, you do not need to consider spelling and syntax rules in English.

Many programming errors are simple, but a character is output in a certain line of the program. It takes a long time to identify such errors.

 Note:To understand new programming concepts, the best way is to try to use them in programs.

 

 

To be continued...

 

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.