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