Cycle
For
Name_k = [' teng ', ' Cardinals ', ' Rommel ']
For i in name_k:if i = = "Cardinals": print "Super Bowl is%s"% i
Note And if are used together
While try not to use
For is the efficiency of Python optimization is high
Break: Please debug the following code to understand break
For i in name_k:if i = = "Cardinals": print "Super Bowl is%s"% i break if i = = "Rommel": print "This is Romme L
Continue: Please debug the following code to understand continue
For i in name_k:if i = = "Cardinals": print "Super Bowl is%s"% i continue if i = = "Rommel": print "This is RO Mmel "
Dictionary
- Key-value pairs
man={"name": ' Cardinals ', ' age ': "Gender": "Male"}
man[' name ']
Dictionaries and for
For k in Man:print Man[k]
For k,v in Man.items (): Print k,v
The characteristics of a dictionary
Disordered
Unique methods
Man.items ()
Man.keys ()
Man.values ()
Operation of the text
Text: Persisting
Find File
Open File
Read and write operations
File Close
File (path, ' mode ')
Mode
R: Read-only
W: Write only (invalid original file)
A: Append
r+: After reading it again
w+: Delete Content again
A = file (' Test ', ' R ') print A.readlines () a.close ()
Method
READ: Reading a file into memory
ReadLines: Reads the file into memory and takes a newline character as a delimiter, and then gets a list
Write: Finish once
WriteLine: Line Write
A = file (' Test ', ' r+ ') file_list = A.readlines () for i in file_list:line = I.strip () values = Line.split (';') Last_value=int (Values[-1]) last_value+=1 values[-1]= last_value print valuesa.close ()
Code instance
#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' teng ' F = file (' Test ', ' r+ ') fileList = F.readlines () mylist =[]for I in filelist:line = I.strip () valueList = Line.split (';') Lastvalue=int (Valuelist[-1]) Lastvalue + = 1 Valuelist[-1] = str (lastvalue) #print values valuestr = '; '. Join (valueList) mylist.append (valuestr) mystr = ' \ n '. Join (mylist) str= ' \ n ' +mystrf.write (str) f.close ()
The test file was originally
Hello Fdf;3rommel;dfds;5
After running the script:
Hello Fdf;3rommel;dfds;5hello; Fdf;4rommel;dfds;6
For explanations of operation, please search by yourself
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/C3/wKiom1azfV2hByfHAAR7CIEhXww570.png "title=" Screenshot from 2016-02-05 00-32-51.png "alt=" Wkiom1azfv2hbyfhaar7ciehxww570.png "/>
Python Basics (ii)