First article on Python basics,
1. The first python code
1. python Execution Process: 1. Load memory-lexical analysis-syntax analysis-compilation-execution
2. Create the hello. py file and enter the content.
1 #!/usr/bin/env python2 print "Hello"
Run the hello. py file: python hello. py
3. Interpreter: The hello. py script is executed with the python interpreter. to execute it like a shell script, you must specify the interpreter at the beginning.
Ii. Encoding
1. the python interpreter is loading. the content is encoded (ascill by default) when the code in the py file is used. If you do not tell the python interpreter what encoding is used to execute the source code, an error is reported.
2. Tell the python interpreter what encoding is used for execution.
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 print "Hello"
3. Notes:
When watching :#
Multi-line comment: "content """
Iii. Variables
1. Declare Variables
name = "zhangsan"
Variable name: name value: "zhangsan"
2. variable name definition Cabinet
(1) variable names can only be any combination of letters, numbers, or underscores
(2) The first character of the variable name cannot be a number.
(3) KEYWORDS cannot be declared as variables
(4) The outer variable can be used by the inner and inner variables and cannot be used by the outer variables.
Iv. Input
1. Assign the value entered by the user to name
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 import getpass 4 name = raw_input ("Enter the User name :") 5 print name 6 7 # when entering the password, if you want to be invisible, you can import the getpass Method 8 9 pwd = getpass in the getpass module. getpass ("Enter Password:") 10 print pwd
2. Process Control
1 #! /Usr/bin/env python 2 #-*-coding: encoding-*-3 name = raw_input ('enter your Username: ') 4 5 if name = "zhangsan ": 6 print "zhangsan" 7 elif name = "lisi": 8 print "lisi" 9 elif name = "wangwu": 10 print "wangwu" 11 else: 12 print "null"
V. Data Types
1. Number
(1) int (integer)
(2) long (long integer)
(3) float)
(4) complex (plural)
2. boolean value (true or false 1 or 0) 3. String (1). python is written in C language. C language does not have a string and has characters.
The string hello is saved in a character array.
Character array ['h', 'E', 'l', 'l', 'O']
(2). character string feature: re-create once modified (so use less concatenation "+ ")
Print "Hello" + "a" + "B" # Three memories will be created # 'hello' + 'W' +' e'
(3). String formatting
1.%
name = "zhangsan"print "my name is %s " % name
2. Index
Name = "aaa, {0}, {1}" print name. format ("bbb", 20) # output: aaa, bbb, 20
(4) common functions of strings
1. Remove white space
Name = "aadd" print name. strip () print name. lstrip () # left space print name. rstrip () # Right Space
2. Segmentation
Name = "aa, bb, cc, dd" print name. split (',') # obtain the list ['A', 'bb ', 'cc', 'dd'] # The list is converted back to the string :",". join (name)
3. Length
Name = "zhangsan" print len (name) # output: 8
4. Index
Name = "zhangsan" print name [1] # output: h
5. Slice
Name = "zhangsan" print name [0: 2] # output: zhprint name [-1] # output: n
4. List
(1) create a list
namelist=['aa','bb','cc']
(2) list common functions
1. Index (same as string)
2. Slice (same string)
3. append
namelist=['aa','bb','cc']namelist.append('dd')
4. Delete
namelist=['aa','bb','cc']del namelist[0]
5. Length (same as string)
6. Include
Namelist = ['A', 'bb ', 'cc'] print "aa" in namelist # Return True or False
7. Loop
Continue # This cycle does not continue to run down, continue next cycle break # Jump out of the loop
5. tuples
(1) create a tuples
tuplename=('aa','bb','cc')
(2) Basic operations on tuples (same as above)
1. Index
2. Slice
3. Loop
4. Length
5. Include
6. Dictionary (unordered dictionary)
(1) create a dictionary
dic ={"name": "aaa", 'age': 18}
(2) common dictionary operations
1. Index
Dic = {"name": "aaa", 'age': 18} print dic ["name"] # output: aaa
2. Add
dic ={"name": "aaa", 'age': 18}dic["iphone"]=123456
3. Delete
dic ={"name": "aaa", 'age': 18}del dic["age"]
4. Loop
Dic. items () # All elements (for loop only)
Dic = {"name": "aaa", 'age': 18} for k, v in dic. items (): print k # dictionary key print v # dictionary value
Dic = {"name": "aaa", 'age': 18} print dic. keys () # output a list containing all keyprint dic. values () # output a list containing all values
5. Length (same as string)
6. Keys and values (one key corresponds to one value)