Python Study Notes Chapter 4: Basic Concepts

Source: Internet
Author: User
Tags natural string

This chapter focuses on some basic concepts in python, which are frequently used and must be understood. 1. constants in the literal sense are constants like 1, 2, 3, Hello, such as numbers or strings, you can literally understand it as a constant. 2. There are four types of numbers in python: integer, long integer, floating point, and plural. * 2 is an integer example * 10000 is a long integer example (a long integer is a larger integer) * 3.14 is an example of a floating point number * (-5 + 4j) is a complex number. 3. A string is a sequence of characters. It can also be understood as a group of words. How to Use strings in python? * You can use single quotation marks (') to specify strings. For example, in 'Hello world', * use double quotation marks ("") to use strings in double quotation marks exactly the same as those in single quotation marks, for example, "What's your name? "* With three quotation marks (''' or"), you can specify a multi-line string, for example, "What's your name? My name is Song "* escape character (\) the use and expression of the Escape Character in python and shell are the same, both expressed by \ (backslash, it is worth noting that if a \ (backslash) exists at the end of a row, it indicates that the next row is written, rather than escaping. Pay special attention to this. * If you want to indicate certain special characters that do not require Escape Character Processing, you can specify a natural string. The natural string is specified by prefix r or R before the string. For example, r "My name is Song by \ n" * Unicode string Unicode is a standard writing method for writing international text. python can also process Unicode text, you only need to add u before the string. For example, the u "This is a book" * string cannot be changed. Once you create a string, you cannot change it. I will explain why later. * If you put two strings adjacent to each other in the literal sense, python will automatically cascade them. For example, What's your name? 'Will be automatically converted to "What's your name? "4. A variable refers to the names of various types of values. You can directly reference the variable name when using this value in the future. The naming rules for specific value variables are not as follows: * The first letter of an identifier must be an uppercase or lowercase letter or _ underline. * Other Departments of the identifier name may consist of letters, underscores, and numbers. * The identifier name is case sensitive. * Examples of valid identifiers: I, _ my_name _, and name_123 * invalid identifiers include: 2 things, this is, my-name 5. Data Type variables can process different types of values and become data types. The basic types are numbers and strings. 6. Object python calls everything in the program an "object ". The following example shows how to use constants in variables and words: #/usr/bin/env python # Filename: var. py I = 5 print I ii = I + 1 print I s = ''' This is a book. this is a open. ''' output: $ python var. py 5 6 This is a book. this is a open. explain how this program works: first, assign a literal constant number 5 to variable I using the value assignment operator (=). This line is a statement, the statement is used to declare that something needs to be done. In this case, we connect variable name I and number 5 and print I, that is, the value of the variable is displayed on the screen. Then we add 1 to the value stored in I and save it to I. When we print it, we get the expected value 6. similarly, we assign a string to s and print it. 7. Logical lines and physical lines are what you see in programming. Logical lines are statements that python understands. python assumes that each physical line corresponds to a logical line. For example, in the print "Hello World" statement, it is a line, just as you can see During writing, so it is a physical line, and python will read it into a statement, so it is still a logical line. By default, python requires only one statement per line, which makes the code easier to read. If you want to use multiple logical rows in a physical row, you can use semicolons (;). For example, I = 5 print I is the same as I = 5; print I; you can also write I = 5; print I; or even I = 5; print I suggests that only one logical row be written for each physical row to avoid the appearance of semicolons as much as possible, make the code easier to read. 8. The blank at the beginning of the indent line is very important in python, which is called "indent ". The blank at the beginning of a logical line is used to determine the indentation level of the logical line and 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. Similarly, improper indentation may cause errors. As follows: I = 5 print 'value is ', I print' I repeat, the value', I print the above program, the following error will be thrown: 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 in the first line of the second line. python tells us that this is an invalid syntax. How to indent? Do not use tabs or spaces to indent the program. Because different program running platforms may cause the program to fail to work, we recommend that you use a single tab or two or four spaces for each indentation level. This chapter has a lot of conceptual knowledge. If you don't understand it, read it several times more and learn it slowly.

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: 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.