python语句分类_Python新手入门【语句类型】
1.條件語句if用法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例1:if 基本用法
flag = False
name = 'luren'
if name == 'python': # 判斷變量是否為 python
flag = True # 條件成立時設(shè)置標(biāo)志為真
print 'welcome boss' # 并輸出歡迎信息
else:
print name # 條件不成立時輸出變量名稱
輸出結(jié)果為:
luren # 輸出結(jié)果elif用法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例2:elif用法
num = 5
if num == 3: # 判斷num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零時輸出
print 'error'
else:
print 'roadman' # 條件均不成立時輸出
輸出結(jié)果為:
roadman # 輸出結(jié)果
2.循環(huán)語句
2.1 循環(huán)類型while循環(huán)
#!/usr/bin/python
count = 0
while (count < 9):
print('The count is:', count)
count = count + 1
print("Good bye!")
輸出結(jié)果為:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!for循環(huán)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'Python': # 第一個實例
print '當(dāng)前字母 :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二個實例
print '當(dāng)前水果 :', fruit
print "Good bye!"
輸出結(jié)果為:
當(dāng)前字母 : P
當(dāng)前字母 : y
當(dāng)前字母 : t
當(dāng)前字母 : h
當(dāng)前字母 : o
當(dāng)前字母 : n
當(dāng)前水果 : banana
當(dāng)前水果 : apple
當(dāng)前水果 : mango
Good bye!
總結(jié)
以上是生活随笔為你收集整理的python语句分类_Python新手入门【语句类型】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android onpreviewfra
- 下一篇: websocket python爬虫_p