Python programming is profound, the knowledge point is numerous, needs to understand the basic usage of Python on the whole before going to study each knowledge point carefully, so that learning speed will be much faster. So let's look at some of the basics that Python needs to know beforehand.
Line and indent:
The first point to note when learning Python is that you do not use parentheses to represent the class and function definition blocks or process controls for your code. Code blocks are indented by lines, which are strictly performed representations. The number of indents is variable, but all statements in the block must be indented the same amount. In this example, two function blocks are well-used:
If True:
print "True"
Else
print "False"
In this case, the second block will produce an error:
If True:
print "Answer" print "True"
Else
print "Answer"print "False"
As a result, the number of spaces indented in Python for all successive lines will also be formed into blocks.
Multi-line statements:
Python statements usually end with a new line. However, Python allows you to use the continuation character () to indicate that the line should continue (across rows).
For example:
Total = (Item_one +
item_two + item_three)
Statements contained within [],{} or () brackets do not need to use a continuation character. For example:
days = [' Monday ', ' Tuesday ', ' Wednesday ',
‘Thursday‘, ‘Friday‘]
You can use a three-quote "" "or" "to print multiple lines of statements, and what format to print. Like what:
Print ("' Hello ' this
Is
A
Test Multiline statement ")
The printing results are as follows:
Hello this
Is
A
Test multi-line statement
continuation character (\):
Python statements, which are generally delimited by line breaks, that is, one statement at a line. A line that is too long can be broken up into lines using a backslash (\), such as:
if (weather_is_hot = = 1) and \
(shark_warnings = = 0):
Send_goto_beach_mesg_to_pager ()
Quotes:
Python can use single quotation marks ('), double quotation marks ("), and Three (" "or" "") quotation marks to represent string constants, as long as they are the same type of quotation marks that start and end a string. Sanchong quotes can be used for strings that span multiple lines. For example:
Word = ' word '
Sentence = "This is a sentence."
Paragraph = "" "This is a paragraph. It is
Made up of multiple lines and sentences. "" "
Comments:
Single-line comment: a pound sign (#), after the "#" character to the end of the bank is a comment, the Python interpreter ignores them. Like what:
#!/usr/bin/python
First comment
Print "Hello, python!" # second comment
Operation Result:
Hello, python!.
Use three quotation marks to complete multiple lines of comment:
‘‘‘
Comment1
Comment2
‘‘‘‘
Waiting for the User:
Way 1:raw_input ()
The following line of the program shows the prompt, press ENTER to exit, waiting for the user to press ENTER:
#!/usr/bin/python
Raw_input ("Press the ENTER key to exit.")
The program ends as soon as the user presses the key. This is a good technique to keep an interactive conversation until the user finishes running the application.
Way 2:input ()
The differences between the above two ways to wait for user input are:
Raw_input does not explain user input
Input will interpret the user's inputs
Multiple statements in a row:
a semicolon (;) allows multiple statements to be written in a single line, regardless of whether the statement starts a new block of code. The following is an example of using semicolons:
Import SYS; x = ' Foo '; Sys.stdout.write (x + ")
Multiple statement groups as suites:
A separate set of statements, in Python, a single code block is called a sequence. Complex statements, such as if, while, Def, and class, require a header row and a suite. The header line begins with the declaration (keyword), and terminates with a colon (:), followed by one or more lines that make up the package.
For example:
If expression:
suite
Elif expression:
suite
else:
suite
Command-line arguments:
Many programs can run, and they provide some basic information about how to run them. You can do this in Python using-H:
$ python-h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...Options and arguments (and corresponding environment variables):-c cmd : program passed in as string (terminates option list)-d : debug output from parser (also PYTHONDEBUG=x)-E : ignore environment variables (such as PYTHONPATH)-h : print this help message and exit[ etc. ]
Cloud computing Learning notes, cloud computing Python automation basic usage