Concise Python tutorial-4. Basic Concepts

Source: Internet
Author: User
Tags natural string

Concise Python tutorial --- 4. Basic Concepts

 

Literal constant

A literal constant is a literal constant like 1, 2, 1.5, and "Hello World. You can use their values in the literal sense.

Quantity

Python has four types of numbers: integer, long integer, floating point, and plural.

2 is an integer example.

A long integer is a larger integer.

1.5 is a floating point number.

2 + 1.5j is the plural number.

String

In python, strings must be enclosed by single quotation marks (') or double quotation marks. For example:

'String1', "string2"

In addition, multiple-line strings can be declared in Python. Such strings must be enclosed by three single quotes (''') or three double quotes. For example:

"She is a good girl.

Say "hello" to her.

"""

Escape characters

In python, backslash (\) is used as the leading escape character. In this example, C/C ++, Java, and C # are the same.

The difference is that in Python, The backslash can also be used to continue a string. For example:

"ABCD \

Efgh"

It is equivalent to "abcdefgh ".

Natural string

A natural string can be specified by prefix R or R. For example, R "ABC \ n ". In a natural string, the leading character of the escape character is processed as a normal character.

Unicode string

If you want to use Unicode characters in a string, you must add the letter U or U prefix before the string. For example, U "Haha ".

The string is unchangeable.

In python, once a string is created, you can no longer change it, such as length and content. This is the same as the string in Java. This is not a defect, but it has some advantages, which will be introduced later.

Identifier name

The first character of the identifier must be a combination of uppercase and lowercase letters or underscores.

The subsequent characters of an identifier can be uppercase/lowercase letters, numbers, or underscores.

The identifier is case sensitive.

Data Type

The most basic data types in Python are numbers and strings.

Use numbers and strings in a program:

I = 5;

Print I;

S = "ABC ";

Print S;

As you can see, in Python, you only need to declare the variable without specifying its data type, which is very different from C/C ++, Java, and C!

Object

Python calls everything in the program an object.

Logical and physical rows

The line you see in the source program is a physical line. For ease of reading, a physical row is generally a logical row that only writes one statement.

Example: print 100;

However, in some cases, a physical row contains multiple statements, and a statement is a logical row. In this way, a physical row contains multiple logical rows.

Example: I = 100; print I;

A semicolon indicates the end of a logical row.

Indent

Blank space is important in Python. The blank at the beginning of the line is even more important. The blank at the beginning of the line is also indented. The blank at the beginning of the logical line is used to determine the indentation level of the logical line and to determine the grouping of statements.

This means that statements at the same level must have the same indentation level. Each group of such statements is called a statement block.

Incorrect indentation may cause an error.

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.