第十章 Introducing Python Statements

时间:2022-09-08 11:25:18

1.Python’s Statements

第十章 Introducing Python Statements

第十章 Introducing Python Statements



Statement rule special cases

a = 1; b = 2; print(a + b) # Three statements on one line       chain together only simple statements


mylist = [1111,      #cross multiple lines
2222,
3333]

if (A == 1 and
B == 2 and
C == 3):
print('spam' * 3)

Block rule special case

if x > y: print(x)  #single-line if statements, single-line while and for loops



while True:
    reply = input('Enter text:')
    if reply == 'stop': break
    try:
        num = int(reply)
    except:
        print('Bad!' * 8)
    else:                          #else part to be run if no exception is raised in the try part
        print(num ** 2)
print('Bye')