Python Learning Notes IV speaking

Source: Internet
Author: User

1.2 while

First, we verify that the variable running is true and then execute the while-block later. After executing this procedure, the condition is checked again, in this case, the condition is the running variable. If it is true, we execute the while-block again, otherwise we continue to execute the optional else-block and then execute the next statement.

When the while loop condition becomes false, the else block is executed-even when the condition is first tested. If the while loop has an else clause, it will always be executed, unless your while loop will never end in a loop!

True and False are called Boolean types. You can interpret them equivalently as values 1 and 0, respectively. Boolean types are important when testing important conditions, and they are not real value 1.

The else block is actually superfluous, because you can place the statement in the same piece (same as while), followed by the while statement, to achieve the same effect.

Set tab to 4 spaces:

: Set ts=4

Permanent effect needs to be modified in initial

While when true or a break jumps out of the loop.

[Email Protected]_nginx ~]# VI for2.py

#!/usr/bin/python

A = [1, "B", 3,4, "D"]

For item in a:

#do this for 5 time

If item = = "B" or item = = "D":

Continue

If Item = = 3:

Break

Print Item

B=5

While B > 0:

Print B

b-=1

"for2.py" 15L, 187C written

[[Email protected]_nginx ~]# python for2.py

1

5

4

3

2

1

Use while to read files

[email protected] ~]# cat while.py

#!! /usr/bin/python

Fd=open ('/root/poem.txt ')

While True:

Line=fd.readline ()

If not line:

Break

Print line,

[email protected] ~]# python while.py

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

Zhailiang

Or:

[email protected] ~]# cat while.py

#!/usr/bin/python

With open ('/root/poem.txt ') as FD:

While True:

line = Fd.readline ()

If not line:

Break

Print line,

[email protected] ~]# python while.py

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

Zhailiang

1.3 for

[[Email protected]_nginx ~]# python for2.py

1

B

3

4

D

[Email Protected]_nginx ~]# Cat for2.py

#!/usr/bin/python

A = [1, "B", 3,4, "D"]

For item in a:

#do this for 5 time

Print Item

Continue: Jump out of this loop and continue to the next loop

Break: Jump out of the loop

[[Email protected]_nginx ~]# python for2.py

1

3

4

[Email Protected]_nginx ~]# Cat for2.py

#!/usr/bin/python

A = [1, "B", 3,4, "D"]

For item in a:

#do this for 5 time

If item = = "B" or item = = "D":

Continue

Print Item

[[Email Protected]_nginx ~]#

To print a multiplication table:

[Email Protected]_nginx ~]# Cat for3.py

#!/usr/bin/python

Sum=0

For I in Xrange (1,10):

For j in Xrange (1,i+1):

Print "%sx%s=%s"% (j,i,j*i),

Print

[[Email protected]_nginx ~]# python for3.py

1x1=1

1x2=2 2x2=4

1x3=3 2x3=6 3x3=9

1x4=4 2x4=8 3x4=12 4x4=16

1x5=5 2x5=10 3x5=15 4x5=20 5x5=25

1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36

1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49

1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64

1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

Reads a text file, which reads the text file into memory, and if the text file is large, it has an impact on the system performance

[email protected] ~]# cat for.py

#!/usr/bin/python

Fd=open ('/root/poem.txt ')

For line in Fd.readlines ():

Print line,

[email protected] ~]# python for.py

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

Zhailiang

Further optimization:

[email protected] ~]# cat for.py

#!/usr/bin/python

Fd=open ('/root/poem.txt ')

For line in FD:

Print line,

[email protected] ~]# python for.py

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

Zhailiang

Script parsing:

FD is an object that reads a line and takes a row, without consuming the system at a significant cost.

Actual combat:

The system generates a random integer within 20, the player has 6 chances to guess, each time guesses have feedback (guess big, guess small, guess right-end)

6 plays, guessed right, the player wins, otherwise the system wins.

Random class

Import Random

Random.randint (1,20)

2. Actual combat: Statistical system remaining memory

The shell command to view the remaining memory is:

[Email protected] ~]# Cat/proc/meminfo

memtotal:5990132 KB

memfree:708940 KB

buffers:655116 KB

cached:3109280 KB

swapcached:792 KB

active:1958380 KB

inactive:2774108 KB

Active (anon): 190700 KB

Inactive (anon): 778676 KB

Active (file): 1767680 KB

Inactive (file): 1995432 KB

[[email protected] ~]# free

Total used free shared buffers Cached

mem:5990132 5281208 708924 1444 655136 3109356

-/+ buffers/cache:1516716 4473416

swap:2097148 8292 2088856

Available memory = System Free memory+buffers+cached.

[email protected] ~]# cat check_mem2.py

#!/usr/bin/python

With open ('/proc/meminfo ') as FD:

For line in FD:

If Line.startswith (' Memtotal '):

Total = Line.split () [1]

Continue

If Line.startswith (' Memfree '):

Free = Line.split () [1]

Break

Print "%.2f"% (int (free)/1024.0) + ' M '

[email protected] ~]# python check_mem2.py

690.26M

Only the yellow mark, if the actual available memory is =4473416

Os.path.split (): Splits the file name and path by path

1.PATH refers to the full path of a file as a parameter:

2. If a directory and file name is given, the output path and filename

3. If a directory name is given, the output path and the empty file name

Detach file name and path

>>> Import OS

>>> print os.path.split ('/dodo/soft/python/')

('/dodo/soft/python ', ')

>>> print os.path.split ('/dodo/soft/python ')

('/dodo/soft ', ' python ')

>>> str= "Hello Boy<[www.doiido.com]>byebye"

>>> print Str.split ("[") [1].split ("]") [0]

Www.doiido.com

>>> print Str.split ("[") [1].split ("]") [0].split (".")

[' www ', ' doiido ', ' com ']

Join ()

Python Learning Notes IV speaking

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.