Python Basics (ii)

Source: Internet
Author: User
Tags fdf

    • 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

    1. Man.items ()

    2. Man.keys ()

    3. Man.values ()


    • Operation of the text

      Text: Persisting

    1. Find File

    2. Open File

    3. Read and write operations

    4. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.