Python multi-line statement
In a python statement, a new line is generally used as the Terminator of a statement, but we can use a slash (\) to divide a line of statements into multiple lines, as follows:
Total = item_one+\
Item_two + \
Item_three
The statement contains [],{}, () there is no need to use a multiline connector, as follows:
Days =[' Monday ', ' Tuesday ', ' Wednesday ', '
Thursday ', ' Friday ']
Python Quotes
Python receives single quotation marks ('), double quotation marks ("), three quotation marks (" ', "" ") to represent a string, where three quotation marks can consist of multiple lines, a shortcut syntax for writing multiple lines of text, often used in a document string, and taken as a comment at a specific location in a file.
word= ' word '
Sentence= "This is a sentence"
Paragraph= "" is a paragraph. It is
Made U of multiple lines and sentences. "" "
Python Empty Line
Separated by a blank line between functions or methods of a class, identifies the beginning of a new piece of code. The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.
Remember: Blank lines are also part of your program code.
Wait for user input
The following program will wait for user input after hitting enter:
#!/usr/bin/python
Raw_input ("\n\npress the Enter key to exit.")
In the above code, ' \ n ' will output two new blank lines before the result output, and the program will exit once the user presses the key.
Show multiple statements on the same line
Python can use more than one statement in the same row, separated by semicolons, as follows:
Importsys; x= ' foo '; Sys.stdout.write (x+ ' \ n ')
Multiple statements form a code group
Indenting the same set of statements constitutes a block of code that we call code groups.
Compound statements like If,while,def,class, where the first line begins with a keyword, ends with a colon (:), and one or more lines of code after that line form the code group.
We refer to the first line and the following code group as a clause (clause).
As follows:
If expression:
Suite
Elif expression:
Suite
Else
Suite
Python variable Assignment
Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.
Each variable is created in memory and includes information about the identity, name, and data of the variable.
Each variable must be assigned a value before it is used, and the variable is created after it is assigned.
The equals sign (=) is used to assign a value to a variable, the left side of the equals operator is a variable name, and the right is the value stored in the variable, as follows:
counter= #an Integer Assignment
miles= ' 1000.0 #a floating point
Name= "Woody" #a string
Printcounter
Printmiles
Printname
Multi-variable assignment
Python allows assigning values to multiple variables at the same time, as follows:
A=b=c=1
The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.
You can also specify multiple variables for multiple objects, such as:
a,b,c=1,2, ' Woody '
In the above example, two Shaping objects 1, 2 are assigned to a, B, the string object ' Woody ' is assigned to the variable C.
Python has five standard data types:
Numbers (number), string (string), list (listing), tuple (tuple), Dictionary (dictionary)
Python numbers
Numeric data types are immutable data types, which means that changing the numeric data type assigns a new object, and when you specify a value, the number object is created, such as:
Var1= 1
You can delete a single live multiple objects through the DEL statement, such as:
Delvar
Delvar1,var2
Python string
The list of strings in Python has 2 order of values:
1. The index from left to right starts from 0, and the maximum range is 1 less than the length of the string.
2, right-to-left index default starting from 1, the maximum range is the beginning of the string
If you want to intercept a substring, you can use the variable name [header subscript: tail subscript], subscript starting from 0, can be positive negative, subscript can be empty to take the head or tail, such as:
s= ' ilove python '
S[1:5] The result is Love
When using a colon-delimited string, Python returns a new object, and the left-hand side contains the lower boundary, the result of which contains the value of s[1], and the maximum range that is taken does not include the value of the upper boundary, or s[5].
The plus sign is the string join operator, and the asterisk is a repeating operator, such as:
Python list
The list is the most frequently used data type in Python.
A list can accomplish the data structure implementation of most collection classes, which supports characters, numbers, and even strings that can include lists (that is, nested), such as:
Python tuples
Tuples are identified with () and the inner elements are separated by commas, but the element cannot be assigned two times (the element cannot be updated), which is equivalent to a read-only list.
Python dictionary
The dictionary is the most flexible data structure type except for the list, and the list is an ordered collection of objects, and the dictionary is an unordered collection of objects.
The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
The dictionary is identified with the {}, which consists of the index (key) and its corresponding value.
Woody's Python Learning Note 2