Python笔记(1)----条件、循环语句
来源:互联网 发布:淘宝偏远地区有哪些 编辑:程序博客网 时间:2024/05/13 14:59
1,if条件语句
if 判断条件:
执行语句
else:
执行语句
条件较多时可以用or或者and,and(同时成立)、or(一个成立即可)。
if (num > 1 and num < 5) or (num > 6 and num < 9):
print "yes"
else:
print "no"
2,while循环语句
while 判断条件:
执行语句
count = 0
while (count < 9):
print"the count is:",count
count = count + 1
print "good bye"
while语句的continue、break命令
i=1
while i <10:
i +=1
if i%2>0:
continue
print i
i=1
while 1:
print i
i+=1
if i>10:
break
while 无线循环
v = 1
while v == 1:
num = raw_input("Enter a number : ")
print "You entered: ", num
while.....else循环
c = 0
while c < 5:
print c, "is less than 5"
c = c + 1
else:
print c, " is not less than 5"
3,for循环
for interating_var in sequence:
statements(s)
fruits = ['banana','apple','mango']
for fruit in fruits:
print "current fruit:",fruit
通过索引执行循环
fruits = ['banana','apple','mango']
for index in range(len(fruits)):
print "current fruit:",fruits[index]
注:range()返回一个序列数,len()返回列表的长度。
for....else
for num in range(10,20):
for i in range(2,num):
if num % i == 0:
j = num/i
print "%d equal %d * %d" % (num ,i,j)
break
else:
print num, "is a prime number"
4,循环嵌套
i = 2
while(i < 100):
j = 2
while(j <= (i/j)):
if not(i%j):
break
j = j + 1
if (j > i/j):
print i, "is a prime number"
i = i + 1
print "Good bye!"
pass语句
for letter in 'Python':
if letter == 'h':
pass
print 'this is pass block'
print 'current letter :', letter
print "Good bye!"
if 判断条件:
执行语句
else:
执行语句
num = 3if num < 5: print "yes"else: print "no
条件较多时可以用or或者and,and(同时成立)、or(一个成立即可)。
if (num > 1 and num < 5) or (num > 6 and num < 9):
print "yes"
else:
print "no"
2,while循环语句
while 判断条件:
执行语句
count = 0
while (count < 9):
print"the count is:",count
count = count + 1
print "good bye"
while语句的continue、break命令
i=1
while i <10:
i +=1
if i%2>0:
continue
print i
i=1
while 1:
print i
i+=1
if i>10:
break
while 无线循环
v = 1
while v == 1:
num = raw_input("Enter a number : ")
print "You entered: ", num
while.....else循环
c = 0
while c < 5:
print c, "is less than 5"
c = c + 1
else:
print c, " is not less than 5"
3,for循环
for interating_var in sequence:
statements(s)
fruits = ['banana','apple','mango']
for fruit in fruits:
print "current fruit:",fruit
通过索引执行循环
fruits = ['banana','apple','mango']
for index in range(len(fruits)):
print "current fruit:",fruits[index]
注:range()返回一个序列数,len()返回列表的长度。
for....else
for num in range(10,20):
for i in range(2,num):
if num % i == 0:
j = num/i
print "%d equal %d * %d" % (num ,i,j)
break
else:
print num, "is a prime number"
4,循环嵌套
i = 2
while(i < 100):
j = 2
while(j <= (i/j)):
if not(i%j):
break
j = j + 1
if (j > i/j):
print i, "is a prime number"
i = i + 1
print "Good bye!"
pass语句
for letter in 'Python':
if letter == 'h':
pass
print 'this is pass block'
print 'current letter :', letter
print "Good bye!"
阅读全文
0 0
- Python笔记(1)----条件、循环语句
- python 学习笔记(2) 条件语句与循环语句
- Python学习笔记(三)-- 条件语句 + 循环语句
- python学习笔记——循环语句条件语句
- Python学习(条件和循环语句)
- Python基础之语句(条件、循环)
- python 条件,循环和其他语句的学习笔记
- Python学习笔记:条件、循环和其他语句
- Python学习笔记(五)条件,循环和其他语句(上)
- Python学习笔记(五),条件,循环和其它语句(下)
- python基础教程_学习笔记7:条件、循环、其它语句
- Python基础教程笔记——条件,循环和其他语句
- Python基础教程笔记——条件,循环和其他语句
- 《Python基础教程》学习笔记 Chap5 条件、循环和其他语句
- python学习笔记5-条件,循环和其他语句
- python笔记(8)----条件、循环及其他语句
- 第5章 Python笔记 条件、循环和其他语句
- Python学习笔记(五)—条件判断和循环语句
- 1等于0.循环9吗?
- 分享三个USB抓包软件---Bus Hound,USBlyzer 和-USBTrace
- Leetcode 303 Range Sum Query
- linux查看目录的四种方法(ls只显示目录)
- Apache2
- Python笔记(1)----条件、循环语句
- execl保护工作簿解锁
- mysql的主从复制,读写分离
- 页面盗链检查&页面标题头小图标
- hdu1166+线段树经典题
- activiti 查询流程定义
- drbd是一个用软件实现的,无共享,服务器之间镜像块设备内容的储存复制
- git/github学习笔记-阅读笔记二
- shiro缓存