码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习笔记第四讲

时间:2018-04-06 17:39:44      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:dom   inactive   min   生成   hello   读取文件   nbsp   cached   wap   

1.2       while

首先,我们检验变量running是否为True,然后执行后面的 while-块 。在执行了这块程序之后,再次检验条件,在这个例子中,条件是running变量。如果它是真的,我们再次执行while-块,否则,我们继续执行可选的else-块,并接着执行下一个语句。

当while循环条件变为False的时候,else块才被执行——这甚至也可能是在条件第一次被检验的时候。如果while循环有一个else从句,它将始终被执行,除非你的while循环将永远循环下去不会结束!

True和False被称为布尔类型。你可以分别把它们等效地理解为值1和0。在检验重要条件的时候,布尔类型十分重要,它们并不是真实的值1。

else块事实上是多余的,因为你可以把其中的语句放在同一块(与while相同)中,跟在while语句之后,这样可以取得相同的效果。

设置tab为4个空格:

:set ts=4

永久生效需要在initial中修改

 

While 当为true或者有break则跳出循环。

 [root@shanghai-WEB-228-102_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

[root@shanghai-WEB-228-102_nginx ~]# python for2.py

1

5

4

3

2

1

 

用while读取文件

[root@SSAVL2261 ~]# cat while.py

#!!/usr/bin/python

fd=open(‘/root/poem.txt‘)

while True:

                line=fd.readline()

                if not line:

                                break

                print line,

 

[root@SSAVL2261 ~]# python while.py

Programming is fun

When the work is done

if you wanna make your work also fun:

        use Python!

Zhailiang

 

或者:

[root@SSAVL2261 ~]# cat while.py

#!/usr/bin/python

with open(‘/root/poem.txt‘) as fd:

                while True:

                                line = fd.readline()

                                if not line:

                                                break

                                print line,

 

[root@SSAVL2261 ~]# python while.py

Programming is fun

When the work is done

if you wanna make your work also fun:

        use Python!

Zhailiang

 

 

1.3       for

[root@shanghai-WEB-228-102_nginx ~]# python for2.py

1

b

3

4

d

[root@shanghai-WEB-228-102_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 :跳出本次循环,继续下一个循环

break:跳出整个循环

[root@shanghai-WEB-228-102_nginx ~]# python for2.py

1

3

4

[root@shanghai-WEB-228-102_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

[root@shanghai-WEB-228-102_nginx ~]#

 

打印乘法口诀表:

[root@shanghai-WEB-228-102_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

[root@shanghai-WEB-228-102_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

 

读取文本文件,该脚本将文本文件读到内存里,如果文本文件很大,会对系统性能有影响

[root@SSAVL2261 ~]# cat for.py

#!/usr/bin/python

fd=open(‘/root/poem.txt‘)

for line in fd.readlines():

        print line,

[root@SSAVL2261 ~]# python for.py

Programming is fun

When the work is done

if you wanna make your work also fun:

        use Python!

zhailiang

 

进一步优化:

[root@SSAVL2261 ~]# cat for.py

#!/usr/bin/python

fd=open(‘/root/poem.txt‘)

for line in fd:

        print line,

[root@SSAVL2261 ~]# python for.py

Programming is fun

When the work is done

if you wanna make your work also fun:

        use Python!

Zhailiang

脚本解析:

fd是一个对象,读一行,取一行,不会占用系统很大开销。

实战:

系统生成一个20以内的随机整数,玩家有6次机会进行猜猜看,每次猜测都有反馈(猜大了,猜小了,猜对了—结束)

6次中,猜对了,玩家赢,否则系统赢。

随机类

Import random

random.randint(1,20)

 

2.  实战:统计系统剩余内存

查看剩余内存的shell命令为:

[root@SSAVL2261 ~]# 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

[root@SSAVL2261 ~]# free

             total       used       free     shared    buffers     cached

Mem:       5990132    5281208     708924       1444     655136    3109356

-/+ buffers/cache:    1516716    4473416

Swap:      2097148       8292    2088856

可用内存=系统free memory+buffers+cached.

[root@SSAVL2261 ~]# 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‘

[root@SSAVL2261 ~]# python check_mem2.py

690.26M

查的只是黄色标记的,如果是实际的可用内存为=4473416

os.path.split():按照路径将文件名和路径分割开

1.PATH指一个文件的全路径作为参数:

2.如果给出的是一个目录和文件名,则输出路径和文件名

3.如果给出的是一个目录名,则输出路径和为空文件名

分离文件名和路径

>>> 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学习笔记第四讲

标签:dom   inactive   min   生成   hello   读取文件   nbsp   cached   wap   

原文地址:https://www.cnblogs.com/kingleoric/p/8728085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!