Python Learning Journey------First day: Basic Concepts (1)

Source: Internet
Author: User
Tags natural string

Note: Most of the content of this article comes from python concise , personal preferences, if there is a copyright issue, please contact me promptly

Environment: Python 3.5


Single quotation marks

You can use single quotes to indicate a string, just like ' quoteme on this '. All whitespace, that is, spaces and tabs are preserved as is .

Use double quotation marks (")

the string in double quotes is exactly the same as the string used in single quotes, such as "What ' syour name?"

Use three quotation marks ("' or" ")

Using the three quotes, you can indicate a multiline string. You can use single and double quotation marks freely in three quotes. For example:

"This is a multi-line string. The first line.

The second line.

"What ' s your name?," I asked.

He said "Bond, James bond."

‘‘‘

Escape character

Suppose you want to include a single in a string quotation marks ('), then how do you indicate this string? For example, this string is what ' Your name?   escape character   \ '

another way to represent this particular string is to "What ' s your name?" , that is, double quotation marks. Similarly, to use the double quotation mark itself in a double-quoted string, you can also help with the escape character. Alternatively, you can use the escape character \ \ to indicate the backslash itself .

One notable thing is that in a string , a single backslash at the end of the line indicates that the string continues on the next line instead of starting a new row. For example:

"This isthe first sentence.\

This is thesecond sentence. "

equivalent to "This is the first sentence. The secondsentence. "

  • natural string
              or to specify. For example

  • unicode string
              unicode is the standard method of writing international texts. If you want to write text in your native language, such as Hindi or Arabic, then you need to have a Unicode-enabled editor. Similarly, Python allows you to work with Unicode text-you only need to prefix the string with u u "This is a     Unicode string."           Remember to use Unicode strings when you work with text files, especially if you know that the file contains text written in a language other than English.

  • The string is immutable
    This means that once you create a string, you can no longer change it. Although this may seem like a bad thing, it's not actually. We will see later in the program why we say it is not a disadvantage.

  • String literals by literal meaning
    If you put two strings next 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?" .

Comments to C/A + + programmers

in Python, there is no specific Char The data type. There is no need for this type, I am sure you will not worry about it.

Comments to the perl/php programmer

remember, single and double quote strings are exactly the same --they are not different in any way.

Comments to the regular expression user

Be sure to use a natural string to process the regular expression. Otherwise, you will need to use a lot of back slashes. For example, a forward reference can be written as '\\1 ' or R ' \1'.

Name of the identifier

Variables are examples of identifiers. An identifier is a name used to identify something. When naming identifiers, you should follow these rules:

  • the first character of the identifier must be Letters in the alphabet (uppercase or lowercase) or an underscore (' _ ') .

  • Other parts of the identifier name can consist of a letter (uppercase or lowercase), an underscore (' _ '), or a number (0-9).

  • identifier name is case sensitive the. For example, and is not an identifier. Notice the lowercase n in the former and the uppercase N in the latter.

  • i , __my_name name_23 and A1B2_C3

  • Invalid Examples of identifier names are 2things,This isspaced out, and my-name.

Object

Remember that Python calls anything that is used in the program as an object. This is broadly speaking. So we don't say "something", we say "some object".

Annotations to object-oriented programming users

For every thing including numbers, strings, and even functions that are objects, Python is extremely completely object-oriented.

We'll look at how to use variables and literal constants. Save the following example, and then run the program.

How to write a python program

The following are standard procedures 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. I routinely put all the python .py save.

4. Run the interpreter command python program.py or use the IDLE Run program. You can also use the executable method described earlier .

Example 4.1 using variables and literal constants

# Filename:var.py

i = 5

Print I

i = i + 1

Print I

s = "This is a multi-line string.

This was Thesecond line.

Print s

(source file: code/var.py)

Output

$ python var.py

5

6

This is Amulti-line string.

This was Thesecond line.

How it works

= 5 assigning variables i i 5 join together. Next, we use the print statement printing i

then we have I the value stored in the 1 and save it back I . When we print it, we get the expected value of 6.

Similarly, we assign a literal string to a variable s then print it.

Comments to C/A + + programmers

You only need to assign a value to a variable when you use it. 650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8E/5E/wKiom1i-iN-g88USAAD9mthMLJs315.png-wh_500x0-wm_ 3-wmp_4-s_2728457242.png "title=" First.png "alt=" wkiom1i-in-g88usaad9mthmljs315.png-wh_50 "/> Do not need to declare or define data types.





Python Learning Journey------First day: Basic Concepts (1)

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.