Why use python? The development efficiency is high and can be transplanted.
String:
1,Use single quotation marks (') = use double quotation marks (") = Java double quotation marks (")
2. Use three quotation marks (''' or ") to merge multiple rows.
3. Escape, similar to '\' in Java. Note (in a string, a separate backslash at the end of the line indicates that the string continues in the next line, rather than starting a new line .)
4. A natural string is prefixed by adding a character stringR
OrR
.
5. the string is variable, similar to Java.
6. the string is cascaded literally.
Variable: (similar to Java)
Object: everything in python is an object.
It is strongly recommended that:PersistenceWrite only one logical row in each physical row
Python requires strict indentation
Operator: similar to Java.
Process control:
If:
If
Guess = Number:
Print
'Congratulations, you guessed it .'
# New block starts here
Print
"(But you do not win any prizes !) "
# New block ends here
Elif
Guess <Number:
Print
'No, it is a little her than that'
# Another block
# You can do whatever you want in a block...
Else
:
Print
'No, it is a little lower than that'
# You must have guess> number to reach here
While:
While
Running:
Guess =
Int
(
Raw_input
(
'Enter an integer :'
))
For:
For
I
In
Range
(
1
,
5
):
Print
I
Break:
Break
The statement is usedTerminationLoop statement, that is, even if the loop condition is not calledFalse
Or the sequence has not been completely recursive, and the execution of loop statements is also stopped.
An important note is that if youFor
OrWhile
CirculatingTermination, Any corresponding LoopElse
BlockNoRun.
Continue:
Continue
The statement is used to tell python to skip the remaining statements in the current loop block, and thenContinueFor the next round.
Function:
#! /Usr/bin/Python
# Filename: func_doc.py
Def
Printmax
(X, y ):
'''Prints the maximum of two numbers.
The two values must be integers .'''
X =
Int
(X)
# Convert to integers, if possible
Y =
Int
(Y)
If
X> Y:
Print
X,
'Is maximum'
Else
:
Print
Y,
'Is maximum'
Printmax (
3
,
5
)
Print
Printmax. _ Doc __
Module:
Import _ name __= = '_ main _' indicates the masterProgram
You can use the built-inDir
Function to list the identifier defined by the module. Identifiers include functions, classes, and variables.
Data structure:
Array: zoo = (
'Wolf'
,
'Elephant'
,
'Penguin'
), Zoo [1], zoo [0]
Map: note that you can only use immutable objects (such as strings) as Dictionary keys, but you can use immutable or variable objects as Dictionary values. Basically, you should only use simple objects as keys.
AB = {
'Swaroop'
:
'Swaroopch @ byteofpython.info'
,
'Larry'
:
'Larry @ wall.org'
,
'Matsumoto'
:
'Matz @ ruby-lang.org'
,
'Spammer'
:
'Spammer @ hotmail.com'
}
AB ['Larry '], Len (AB)
List:
Shoplist = [
'Apple'
,
'Mango'
,
'Carrot'
,
'Bana'
]
Shoplist. Sort (),Shoplist. append (
'Rice'
)
List
AndArray
Very similar,Array
Like a stringImmutableYou cannot modifyArray.
Sequences: Lists, arrays, and strings are all sequences. But what are sequences? Why are they so special? The two main features of the sequence are:IndexOperator andSliceOperator. The index operator allows us to capture a specific item from the sequence. The Slice operator allows us to obtain a slice of a sequence, that is, a part of the sequence.
Shoplist [
1
:
3
],Shoplist [
2
:], Shoplist [:],Shoplist [
-1
],Shoplist [
0
.