Python's variable usage and string

Source: Internet
Author: User

What happens when you run hello_world.py

Create a hello_world.py file to write a code like this:

# !/usr/bin/env python # -*-Coding:utf8-*- Print ("helloWorld")

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 editor 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.
When you write a program, the editor highlights different parts of the program in various ways. For example, it knows that print is the name of a function and therefore displays it as blue; it knows "Hello Python world!" is not a Python code, so it is shown in orange. This feature is called syntax highlighting and is helpful when you first start writing programs.

Variable

Here's an attempt to use a variable in hello_world.py. Add a line of code at the beginning of the file and modify the 2nd line of code as follows:

# !/usr/bin/env python # -*-Coding:utf8-*-name = ("my name is Yankerp")print(name)

The output results are as follows:

Hello Python world!
We have added a variable named message. 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 1th line of code is processed, it will text "Hello Python world!" is associated with a variable message, and when the 2nd 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:utf8-*-name = ("my name is Yankerp"= ("my name is Z Hangsan")print(name)

For example:

# !/usr/bin/env python # -*-Coding:utf8-*-name = ("my name is Yankerp")print= ("my name is Zhangsan") Print (name)

Output results

Yankerp

Zhangsan

Myself to experience!!!

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.

    • 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 name a variable message_1, but you cannot name it 1_message.
    • 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 name greeting message throws an error.
    • Do not use Python keywords and function names as variable names, that is, do not reserve words for special purposes using Python, such as print
    • 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_persons_name.
    • Use the lowercase l and the capital Letter o sparingly, as they may be mistaken for numbers 1 and 0.

Creating good variable names requires a certain amount of practice, especially when the program is complex and interesting. As you write more and more programs, and start reading code written by others, you'll be 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 cause errors, but it is a good idea to avoid using uppercase letters.

Avoid naming errors when using variables

Programmers make mistakes, and most programmers make mistakes every day. While good programmers make mistakes, they know how to eliminate them effectively. Here's a look at a mistake you might make and learn how to get rid of it.
We will intentionally write some code that raises the error. Please enter the following code, including the word mesage in bold but not spelled correctly:

String

Most programs define and collect some kind of data, and then use them to do something meaningful. In view of this, the classification of data is of great benefit. The first data type we'll cover is a string. Strings, though seemingly simple, can be used in many different ways.
A string is a series of characters. In Python, a string is enclosed in quotation marks, which can be single quotes or double quotes, as follows:

" This is a yankerp " ' This is a yankerp '

Python's variable usage and string

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.