if 条件

if 语法

- if expression

    statement(s)

注意:python 使用缩进作为其语句的分组方法,建议使用4个空格


1
2
3
if not 1 2 and 1 == 1:
    print 'hello python'
    print 'True'

这个例子中,逻辑非的优先级比较高

先执行:

    not 1 > 2

在执行:

    1==1


1
2
3
4
5
6
if 10 2:
    print 'hello python'
    print 'True'
else:
    print 'ha'
print 'main'

这个例子中,print  'main' 这个条件是排除在 if 语句外的,无论如何都会输出。


if 多个条件,例子如下:

1
2
3
4
5
6
7
8
if 10 2:
    print 'hello python'
    print 'True'
elif 'a':
    print 'a'
else:
    print 'ha'
print 'main'

总结:

if条件练习,成绩分类

1
2
3
4
5
6
7
8
9
10
11
12
13
score = int(raw_input("Please a num: "))
if score >= 90:
    print "A"
    print 'very good'
elif score >= 80:
    print 'B'
    print 'good'
elif score >= 70:
    print 'C'
    print 'pass'
else:
    print 'D'
print 'END'


布尔值

逻辑值(bool)包含了两个值:

-True : 表示非空的量(比如:string、tuple、list、set、dictionary),所有非零数。

-False :   表示0,None,空的量等。


5.png

上栗中,用到了一个字符串的方法,yn.lower ,这个方法的作用把收到的字符串转为小写的。



for序列

循环

循环是一个结构,导致程序要重复一定的次数。

条件循环也是如此,当条件变为假,循环结束。


for循环

在序列里,使用for循环遍历

语法:

        - for iterating_var  in  sequence:

                statement(s)


常用for循环

1
2
for in range(111):
    print i

对序列做个遍历。


含有if条件的for循环

1
2
3
for in range(111):
    if % 2 == 0:
        print i

注意缩进


列表的重写:

几个栗子:

1
2
3
4
print [i for in range(111)]
print [i * 2 for in range(111)]
print [i for in range(111if % 2 == 0]
print [i ** 2 for in range(111)]
1
2
for in [i ** 2 for in range(111)]
    print i,

在一行中打印出列表里的元素。



用遍历的方式访问字典

定义字典的方法:

1
2
dic = {'a':1'b':2}
dic.fromkeys('abcdefgh',100)


  1. 创建字典

33.png

  2.遍历字典的key和值

44.png

 3.格式化输出

55.png

 4.items方法

66.png

5.iteritems 方法

77.png

同range和xrange的区别一样,items和iteritems也累死,iteritems也返回的是对象,不占用内存,

只有调用到的时候,才会占用单个的内存。


6.乘法口诀

1
2
3
4
for in xrange(1,10):
    for in xrange(1, i+1):
        print "%sx%s=%s" % (j, i, j*i)
    print

i 控制行,j 控制列,末尾 print 表示打印换行符


循环退出

for循环也有else

for 循环如果正常结束,才会执行else语句。


举例:

1
2
3
4
for in xrange(10):
    print i
else:
    print 'main end'

else 后面的也会输出。


1.如果不让语句执行完 ,break ,退出循环,继续执行程序,则else不会输出:

1
2
3
4
5
6
for in xrange(10):
    print i
    if == 5:
        break
else:
    print 'main end'

有 break 没有正常结束,不输出最后的代码。


2.continue 退出当前循环,继续后面的循环

1
2
3
4
5
6
7
8
for in xrange(10):
    if == 3:
        continue
    if == 5:
        break
    print i
else:
    print 'main end'

当 i 等于3 时候,循环内部,下面的语句不继续执行了,跳出当前,继续下一次循环


3.pass  占位

1
2
3
4
5
6
7
8
9
10
for in xrange(10):
    if == 3:
        countinue
    elif == 5:
        break
    elif == 6:
        pass
    print i
else:
    print 'main end'


4.彻底退出程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import sys
import time
 
for in xrange(10):
    if == 3:
        continue
    elif 1 == 5:
        continue
    elif == 6:
        pass
    elif == 7:
        sys.exit()
    print i
else:
    print 'main end'
print "hahahah"

所有sys.exit()下的程序,都不会输出。


random.randint() 随机数模块

88.png


while 循环

while 和 for 循环的区别

- for 循环用在有次数的循环上

- while 循环用在有条件的控制上


while 循环,直到表达式为假,才退出。

while 循环,表达式是一个逻辑表达式,必须返回一个True或False。


语法:

    while expression:

        statement(s)


while循环里有break,也可以退出循环


死循环:

永远成立,永远不会退出的循环。

1
2
while True:
    print 'hello'


判断初始值

1
2
3
4
5
6
= 0
while True:
    if == 10:
        break
    print n, 'hello'
    += 1

执行10次就break了~


读取键盘输入

1
2
3
4
5
while True:
    print 'hello'
    = raw_input("Please input something, q for quit:")
    if == "q":
        break


变量使用:

1
2
3
4
= ''
while x != 'q':
    print 'hello'
    = raw_input("Please input something, q for quit:")


判断的使用

1
2
3
4
5
6
= ''
while x != 'q':
    print 'hello'
    = raw_input("Please input something, q for quit:")
    if not x :
        break

当 x 为 非空的时候,就退出循环。


while 也有else

1
2
3
4
5
6
7
8
= ''
while x != 'q':
    print 'hello'
    = raw_input("Please input something, q for quit:")
    if not x :
        break
else:
    print 'world'

如果不是正常结束,就会执行else。


continue 用法

1
2
3
4
5
6
7
8
9
10
11
= ''
while x != 'q':
    print 'hello'
    = raw_input("Please input something, q for quit:")
    if not x :
        break
    if == 'quit':
        continue
    print 'continue'
else:
    print 'world'