Python入门学习
Python文档阅读和Python语法点拾遗
Python文档阅读
这是一个英语阅读生词总结笔记(大雾)
Some prompts :
- (>>>) (three greater-than signs) primary prompt
- (…) secondary prompt
Operators :
- (//) to do floor division and get a integer result (discarding any flactional result)
- (\)** to calculate power
- (_) if you use Python as a calculator you can type _ to get the last printed expression (ps. Don not assign the value to it)
- use round(a, b) to keep a in the b decimal place
- (j or J) use the J suffix to indicate the imaginary part
String operators :
quotes : 单引号
double quotes : 双引号
backslashes : 反斜线
(\) use \‘ to escape the single quote
(\n) \n means newline
String operation :
If you don’t want character prefaced by \ to be interpreted as special characters, you can use raw string by adding an r
before the first quote.
1 |
|
using triple-quotes : '''
or """
so that the strings literals can span multiple lines. and use \at the end of the line to avoid that end of lines are automatically included in the string.
The following example:
1 |
|
1 |
|
Strings can be concatenated (glued together) with the +
operator and repeated with *
.
Two or more string literals next to each other are automatically concatenated.
1 |
|
This feature is only work with two literals, not with variables or expressions.
The string indices alse can be negative number,to counting from the right.
negetive indices start from -1
slicing slicing is used to obtain the substring
There follwing examples :
1 |
|
The start is always included, and end always excluded.This make sure that s[:i] + s[i:] always equal to s.
Note : an omitted first index defaults to zero.
Python strings are immutable, so assigning to an indexed position in the string result in an error.
List :
Lists can contain different types, but usually the items all have the same type.
List also can be indexed and sliced.(same as string)
1 |
|
List also support operations like concatenation.
1 |
|
Unlike string, list is a mutable type, it is possible to change their content.
You can also add new items at the end of the list, by using the append() method (we will see more about methods later):
1 |
|
Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:
1 |
|
build-in function len()
also applies to lists.
nest list is also allowed.
Contains in while
The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false.
Indentation is Python’s way of grouping statements.
The keyword argument end can be used to avoid the newline after the output, or end the output with a different string:
1 |
|
More Control Flow Tools
if Statements
The else part is optional.
The keyword ‘elif’ is short for ‘else if’,and is useful to avoid excessive indentation.An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.
for Statements
Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):
1 |
|
The range() Function
the built-in function range() can make the sequence of numbers.
These following example:
1 |
|
It is possible to let the range start at another number, or to specify a different increment:
1 |
|
A strange thing happens if you just print a range:
1 |
|
Here is the solution:
1 |
|
break and continue statements and else Clauses on Loops:
break and continue are like in C++.
1 |
|
In this code, the else is belongs to the for loop, not the if statement.
The loop’s else clause runs when no break occurs.
pass Statement :
It can be used when a statement is required syntactically but the program requires no action.
pass can be used is as a place-holder for a function or conditional body when you are working on new code
Defining Functions :
Other names can also point to that same function object and can also be used to access the function:
1 |
|
The example for the defining function:
1 |
|
This function can be called in several ways:
giving only the mandatory argument: ask_ok(‘Do you really want to quit?’)
giving one of the optional arguments: ask_ok(‘OK to overwrite the file?’, 2)
or even giving all arguments: ask_ok(‘OK to overwrite the file?’, 2, ‘Come on, only yes or no!’)
Python语法点拾遗
1 |
|
Python入门学习