Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268045
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: Python/Ruby

2015-09-02 01:40:10

1.if  else  流程控制

点击(此处)折叠或打开

  1. /*1.py*/
  2. #!/usr/bin/python
  3. if 1<2: /*前面有冒号*/
  4.     print "ok" /*统一空四个空格*/
  5.     print "ok" /*统一空四个空格,在同一个运算条件中*/
  6. /*相当于C 语言中的{ }将两行,扩起来*/
  7. /*************************************************
  8. 打印出两个ok
  9. ****************************************************/
  10. 执行: python 1.py
  11. ok
  12. ok
  13. ***************************************************
/*2.py*/

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. if True: /*以冒号结束 ,首字母大写 True False*/
  3.     print "ok" /*四个空格*/
  4. print "false" /* 当条件不成立时执行,print "false"*/   
/*Flase.py*/

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. if False:
  3.     print "ok"
  4. print "false"
2。增加函数

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. def fun():
  3.     return 1
  4. if fun():
  5.     print "ok"
  6. /********************/
  7. python 1.py
  8. ok
3.else

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. def fun():
  3.     return 1
  4. if 0:
  5.     print "ok"
  6. else:            /*冒号*/
  7.     print "bad"
4.elif的用法

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. def fun():
  3.     return 0
  4. x=int(raw_input("please input:"))
  5. if x>=90:
  6.     print "A"
  7. elif x>=80:
  8.     print "B"
  9. elif x>=70:
  10.     print "C"
  11. else :
  12.     print "bad"
5逻辑关系.and  or not 相当于 and 就是&  

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. x=int(raw_input("please input:"))
  3. y=int(raw_input("please input:"))
  4. if x>=90 and y>=90:
  5.     print "A"
  6. elif x>=80:
  7.     print "B"
  8. elif x>=70:
  9.     print "C"
  10. else :
  11.     print "bad"

  12. *************************
  13. python 1.py
  14. please input:90
  15. please input:90
  16. A
6.for循环

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. for x in "abcd":
  3.     print  x,"hello world"
  4. **********************
  5. python 1.py
  6. a,hello world
  7. b,hello world
  8. c,hello world
  9. d,hello world
7.range(10)

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. for x in range(10):
  3.     print x, "hello world"
  4. *************************
  5. python 1.py
  6. 0 hello world
  7. 1 hello world
  8. 2 hello world
  9. 3 hello world
  10. 4 hello world
  11. 5 hello world
  12. 6 hello world
  13. 7 hello world
  14. 8 hello world
  15. 9 hello world
8.range(1,11,2)  //第一个为初始值,第二个为结束值,第三个为步长

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. for x in range(1,11,2):
  3.     print x, "hello world"
  4. python 1.py
    1 hello world
    3 hello world
    5 hello world
    7 hello world
    9 hello world
9.range(1,101)

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. num=0
  3. for x in range(1,101):
  4.     num+=x
  5. print num


  6. ************
  7. 5050

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. num=0
  3. for x in range(1,101):
  4.     num+=x
  5. print num


  6. ************
  7. 5050
10.for遍历列表中的值

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. fruits=['bana','apple','man','str']
  3. for x in range(len(fruits)):
  4.     print 'Current fruit:',fruits[x]



  5. ***********************
  6. python 1.py
  7. Current fruit: bana
  8. Current fruit: apple
  9. Current fruit: man
  10. Current fruit: str
















阅读(1115) | 评论(0) | 转发(0) |
0

上一篇:python笔记2

下一篇:python 笔记5 for循环

给主人留下些什么吧!~~