1. Installation
Address: https://www.python.org/downloads/windows/
After the installation is complete, configure the environment variables, such as: path after the Count ; C:\Python27(may need to be restarted)
Then cmd enters Python, which is shown below to indicate that the installation was successful
2, the basic knowledge (record write special column)
0, empty value is a special value in Python, with the None
expression. None cannot be understood as 0, because 0 is meaningful, and none is a special null value.
1, string: With single or double quotation marks line, the more special is three single quotation marks are multi-line text, the middle can be wrapped
2, if the string contains a lot of transferred characters, you can use R ' 23\wer\2 ' can add r prefix, equivalent to C # in the @
3, if the printing has garbled can be used print u ' Chinese ' plus u prefix, a Unicode-represented string
4, U, r these can be used together, for example:
ur ' python's Unicode string supports ' Chinese ',
"Japanese",
"Korean" and many other languages "
5. py file can fill a line #-*-Coding:utf-8-*- Tell the Python parser to Utf-8 code read the source code
6. If Else syntax
if age >=: print'adult'else: If age >= 6: print'teenager' Else : Print ' Kid '
7. Circulation
L = [ ' adam ", " lisa , " Bart " ] name in L: print namen = 10x = 0 while x < N: print x x = x + 1
sum =1 while True := sum + x = x + 1 if x >: breakprint sum
For x in L: if x <: continue sum = sum + x n = n + 1
forXinch['A','B','C']: forYinch['1','2','3']: PrintX + y
8. Dictionaries
#Dictionary features the first feature of Dict is that the search speed is fast, regardless of whether the dict has 10 elements or 100,000 elements, the search speed is the same. The search speed of the list decreases as the element increases. However, dict search speed is not without cost, dict the disadvantage is that the memory is large, but also waste a lot of content, the list is just the opposite, the memory is small, but the search speed is slow. Because Dict is located by key, in a dict, key cannot be duplicated#Defining DictionariesD = { 'Adam': 95, 'Lisa': 85, 'Bart': 59}#AccessPrintd['Adam']#determine if key existsif 'Paul' inchD:Printd['Paul']#using a Get method provided by Dict itself, returns none when key does not existPrintD.get ('Bart')#Updated['Paul'] = 72#Traverse forKeyinchD:PrintKey
#遍历value
For V in D.values (): Print V
9, Ganso
T = ('Adam','Lisa','Bart') t[0]='Paul'#because () can represent both a tuple and the precedence of an operation as parentheses>>> T = (1,)>>>PrintT (1,)#Guanzuko contains listT = ('a','b', ['A','B']) L= T[2]>>> L[0] ='X'>>> L[1] ='Y'>>>PrintT ('a','b', ['X','Y'])
10. Set
#because set stores unordered collections, we cannot access them through an index. s = Set (['Adam','Lisa','Bart','Paul'])#use the In operator to return true'Bart' inchs#Traverses = Set (['Adam','Lisa','Bart']) forNameinchS:Printname#Adds = Set ([1, 2, 3]) S.add (4)#There is no remove errorS.remove (7)
11. Functions
#returns multiple valuesImportMathdefMove (x, y, step, Angle): NX= x + Step *Math.Cos (angle) NY= Y-step *Math.sin (angle)returnNX, NY#calledX, y = Move (+, +, MATH.PI/6)Printx, y151.961524227 70.0#Recursivedeffact (n):ifN==1: return1returnn * Fact (n-1)#Default ParametersdefPower (x, n=2,c=4): S= 1 whilen >0:n= N-1s= S *xreturns#Skip CallPower (1,c=3)#variable parameter, just think of the variable args as a tuple.defFN (*args):PrintArgs
def
func
(a, b=
5
, c=
10
):
print
‘a is‘
, a,
‘and b is‘
, b,
‘and c is‘
, c
func(
3
,
7
)
func(
25
, c=
24
)
func(c=
50
, a=
100
)
11, docstrings
defPrintmax (x, y):" "Print my document content prints the maximum of the numbers. The values must be integers." "x= Int (x)#Convert to integers, if possibley =Int (y)ifX >y:PrintX'is maximum' Else: PrintY'is maximum'Printmax (3, 5)PrintPrintmax.__doc__
Description: Part of the content from the Web and Python manual content
Step by Step learning python-Basic article