Python學習之條件語句

2021-07-04 06:42:31 字數 4409 閱讀 7694

# coding=utf-8

# 使用逗號隔開抖個輸出語句

print 'age:', 42

# 輸出自動轉換後的字串

name = 'gumby'

salutation = 'mr.'

greeting = 'hello.'

print greeting + ',', salutation, name

import math as foobar

print foobar.sqrt(4)

print '賦值魔法'

# 個數要對應上,否則會有異常

x, y, z = 1, 2, 3

print x, y, z

x, y = y, x

print x, y, z

values = 1, 2, 3

x, y, z = values

print x

scoundrel =

key, value = scoundrel.popitem()

print key

print value

print '鏈式賦值'

x = y = 'ok'

print x, y

print "增量賦值"

x = 2

x += 1

print x

x *= 2

print x

fnord = 'foo'

fnord += 'bar'

fnord *= 2

print fnord

print '條件語句'

# 這些值都被看作為假:false, none, 0, "", (), , {}

print bool('i am test')

# 注意有個分號

name = 'llllllllllllll4445'

if name.endswith('2345'):

print 'true'

elif name.endswith('345'):

print 'true2'

else:

if name.endswith('45'):

print "true3"

else:

print 'false'

if 'a' in 'main':

print 'true'

if 'alpha' < 'beta':

print "true"

if [1, 2] < [2, 1]:

print 'true'

if [2, [1, 4]] < [2, [1, 5]]:

print 'true'

number = 2

if number > 1 and number < 10:

print 'right'

number = -1

if number > 1 or number == -1:

print 'right2'

print '斷言'

assert number < 1

print '迴圈'

x = 1

while x < 5:

print x

x += 1

words = ['this', 'is', 'an']

for word in words:

print word

# 迭代

print range(0, 10)

print range(0, 10, 2)

for number in range(0, 5):

print number

d =

for key in d:

print key, 'corresponds to ', d[key]

names = ['anne', 'bett', 'ccc', 'dadd']

ages = [12, 34, 56, 78]

for i in range(len(names)):

print names[i], 'is', ages[i], 'years old'

# zip函式可以把連個序列壓縮在一起,返回乙個元組的列表

print zip(names, ages)

# 最短的用完為止

print zip(range(5), xrange(10000))

strings = ['aa', 'bb', 'cc', 'aa', 'dd aa ee']

for index, string in enumerate(strings):

if 'aa' in string:

strings[index] = 'jj'

print strings

print sorted([3, 2, 5, 1, 3])

print list(reversed('hello,world!'))

print ''.join(reversed('hello, world!'))

print '跳出迴圈'

from math import sqrt

for n in range(99, 0, -1):

root = sqrt(n)

if root == int(root):

print n

break

else:

print "didn't find it"

print '列表推導式'

print [x * x for x in range(5)]

print [x * x for x in range(5) if x % 3 == 0]

print [(x, y) for x in range(3) for y in range(3)]

# pass 什麼也不做

if name == 'a':

print 'a'

elif name == 'b':

pass

else:

print 'error'

x = 1

del x

def square2(x):

return x * x

a = [1, 2, 3, 4, 5]

b = [6, 7]

# 把a中的每個值作為函式square2的引數,返回乙個列表

print map(square2, a)

print map(none, a, b)

輸出:"d:\program files\python\python.exe" g:/hellopython/list/syntax.py

age: 42

hello., mr. gumby

2.0賦值魔法

1 2 3

2 1 3

1girlfriend

marion

鏈式賦值

ok ok

增量賦值36

foobarfoobar

條件語句

true

true3

true

true

true

true

right

right2

斷言迴圈12

34this

isan

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 2, 4, 6, 8]01

234y corresponds to  2

x corresponds to  1

z corresponds to  3

anne is 12 years old

bett is 34 years old

ccc is 56 years old

dadd is 78 years old

[('anne', 12), ('bett', 34), ('ccc', 56), ('dadd', 78)]

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

['jj', 'bb', 'cc', 'jj', 'jj']

[1, 2, 3, 3, 5]

['!', 'd', 'l', 'r', 'o', 'w', ',', 'o', 'l', 'l', 'e', 'h']

!dlrow ,olleh

跳出迴圈

81列表推導式

[0, 1, 4, 9, 16]

[0, 9]

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

error

[1, 4, 9, 16, 25]

[(1, 6), (2, 7), (3, none), (4, none), (5, none)]

process finished with exit code 0

python學習之條件語句(if迴圈)

python條件語句是通過一條或多條語句的執行結果 true或者false 來決定執行的 塊。可以通過下圖來簡單了解條件語句的執行過程 python程式語言指定任何非0和非空 null 值為true,0 或者 null為false。python 程式設計中 if 語句用於控制程式的執行,基本形式為 ...

PYTHON基礎之條件語句

了解到一些python基礎語句 那就反手敲一敲!1,關於個稅演算法 基本演算法 扣除三險一金後月收入 工資 三險一金 應納稅所得額 扣除三險一金後月收入 扣除標準 應納個人所得稅 稅額 全月 應納稅所得額 適用稅率 速算扣除數 個稅基本條件 扣除標準3500元 月三險一金繳納 的基準工資 上限為76...

初始Python之條件語句

二狗有一天去趕集,他的女朋友對他說,去買乙個西瓜吧,如果有西紅柿的話買兩個?正常來說二狗帶回來的應該是乙個西瓜或者乙個西瓜兩個西紅柿。但是呢二狗是個程式猿,最後二狗帶了兩個西瓜回來 這是為什麼呢?我們首先來看看這句話在二狗的腦海裡面是怎麼執行的。菱形部分就是我們今天要介紹的條件語句?什麼是條件語句?...