[Python] Getting Started tutorial (2): some basic concepts in Python

Source: Internet
Author: User
Tags natural string processing text

Recently I am busy working on unity3d. I have put a lot of Python study notes, but now I am going to make it up.

Speaking of basic concepts, programmers should be familiar with programming, but as part of the Notes, let's take a look at it.

String

Let's talk about it first: String string
String, which is a string of characters.

Strictly speaking, a string is a string of characters.
There are roughly three situations:
1. Use single quotes (')
You can use single quotes to indicate strings, just like 'quote me on this. All spaces, that is, spaces and tabs are retained as they are.

2. Use double quotation marks (")
The strings in double quotes are exactly the same as those in single quotes. For example, "What's your name? ".

3. Use three quotation marks (''' or """)
You can use three quotation marks to indicate a multi-line string. You can use single quotes and double quotes freely in three quotes. For example:

'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name ?, "I asked.
He said "Bond, James Bond ."
'''

Escape Character

Let's talk about the concept of escape characters.
Suppose you want to include a single quotation mark (') in a string, how do you indicate this string?

For example, the string is: What's your name? You certainly won't use what's your name? 'To indicate it, Because python will not understand where the string starts and ends. Therefore, you must specify single quotes instead of the end of the string. You can use escape characters to complete this task. You use \ 'to indicate single quotes -- pay attention to this backslash. Now you can represent the string as 'What \'s your name? '.

The other method to indicate this special string is "What's your name? ", That is, double quotation marks. Similarly, to use double quotation marks in a double quotation mark string, you can also use escape characters. In addition, you can use the Escape Character \ to indicate the backslash itself.

One thing worth noting is that in a string, a separate backslash at the end of the line indicates that the string continues in the next line, rather than starting a new row. For example:

"This is the first sentence .\
This is the second sentence ."

It is equivalent to "This is the first sentence. This is the second sentence ."


Natural string

Can I skip the escape character? Of course, let's take a look: Natural string
If you want to indicate certain strings that do not require special processing such as escape characters, you need to specify a natural string. A natural string is specified by adding the prefix R or R to the string. For example, R "newlines are indicated by \ n ".

Unicode string
Unicode is a standard method for writing international text. If you want to write text in your native language, such as Hindi or Arabic, you need an editor that supports Unicode. For example, editplus is my dish.

Similarly, Python allows you to process Unicode text-you only need to add the prefix U or u before the string.

For example, U "this is a unicode string ."
Use Unicode strings when processing text files, especially when you know that the file contains text written in non-English languages.

Note the following special points in Python:

1. the string is unchangeable.

This means that once you create a string, you cannot change it any more. Although it looks like a bad thing, it is actually not.

2. literally connected strings
If you put two strings adjacent to each other literally, they will be automatically connected by python. For example, 'What \ s' your name? 'Will be automatically converted to "What's your name? ".

3. Comments to Java/C ++ programmers
There is no special char data type in Python.

4. Comments to Perl/PHP programmers
Remember, single quotes and double quotation marks are exactly the same-they are not in any way different.

5. annotate the regular expression user
Be sure to use natural strings to process regular expressions. Otherwise, a lot of backslash is required. For example, the back reference can be written as '\ 1' or R' \ 1 '.

Next, let's take a look at the problem of identifier naming.

An identifier is the name used to identify something. Follow these rules when naming identifiers:
The first character of an identifier must be an uppercase or lowercase letter or an underscore ('_').
Other parts of an identifier name can be uppercase or lowercase letters, underscores ('_'), or numbers (0-9.
The identifier name is case sensitive. For example, myname and myname are not an identifier. Note: the lower case n in the former and the upper CASE n in the latter.
Examples of valid identifiers include I, _ my_name, name_23, and a1b2_c3.
Examples of invalid identifiers include 2 things, this is spaced out, and my-name.

Object

Python calls everything used in a program an object.

Annotations for Object-Oriented Programming users
Python is an object that contains numbers, strings, and even functions.

To further explain the concept of objects, Let's first look at how to write Python programs.
The following is a standard process for saving and running Python programs.
1. Open your favorite editor.
2. Enter the program code in the example.
3. Save it as a file with the file name given in the comment. By convention, I save all Python programs with the extension. py.
4. Run the interpreter command Python program. py or run the program using idle. You can also use the previously introduced executable methods.


We will take a look at how to use constants in the variable and Word Sense. Save the following example and run the program.
Example 4.1 Use constants in the variable and surface sense (source file: 02. var_test.py)

# Filename : var.pyi = 5print(i)i = i + 1print(i)s = '''This is a multi-line string.This is the second line.'''print(s) 

Output
5
6
This is a multi-line string.
This is the second line.

Unlike C ++ or Java, variables only need to be assigned a value. You do not need to declare or define data types. This is a bit like PHP and JS, but it does not have the concept of var.


Again, it is different from Java and C ++: indentation.

Blank space is important in Python. In fact, the blank at the beginning of the line is important. It is called indentation. The blank spaces (spaces and tabs) at the beginning of the logical line are used to determine the indentation level of the logical line and determine the grouping of statements.

This means that the statements at the same level must have the same indentation. Each group of such statements is called a block. We will see examples of block usage in later sections.

One thing you need to remember is that wrong indentation will cause errors. For example:

i = 5 print('Value is', i # Error! Notice a single space at the start of the line)print( 'I repeat, the value is', i )

When you run this program, you will get the following error:

File "whitespace. py", line 4
Print 'value is ', I # error! Notice a single space at the start of the line
^
Syntaxerror: Invalid Syntax

Note that there is a space at the beginning of the second line. The error indicated by Python indicates that the program syntax is invalid, that is, the program is not correctly written. It tells you that you cannot start a new statement block at Will (except for the main block you have been using ). When you can use the new block, it will be detailed in the following chapter, such as the control flow.

How to indent
Do not mix tabs and spaces to indent, because it cannot work normally when crossing different platforms. I strongly recommend that you use a single tab or two or four spaces at each indent level.
Select one of the three indent styles. More importantly, select a style and use it consistently, that is, only this style is used.


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.