地板除 运算符(floor division operator) // 先做除法,然后将结果保留到整数。
>>> minutes = 105
>>> minutes / 60
1.75
>>> minutes = 105
>>> hours = minutes // 60
>>> hours
1
求余运算符 %。
布尔表达式:
5 == 5 = True
type(True) = <class 'bool'>
逻辑运算符:
and
or
not
条件语句if :
if x > 0:
print('x is positive')
# pass 语句
if x < 0:
pass # 待完成:需要处理负数值!
if x % 2 == 0:
print('x is even')
else:
print('x is odd')
# elif
if x < y:
print('x is less than y')
elif x > y:
print('x is greater than y')
else:
print('x and y are equal')
if 0 < x:
if x < 10:
print('x is a positive single-digit number.')
if 0 < x and x < 10:
print('x is a positive single-digit number.')
if 0 < x < 10:
print('x is a positive single-digit number.')
递归(recursion)。
键盘输入:
Python 提供了一个内建函数 input ,可以暂停程序运行,并等待用户输入。
在Python 2中,这个函数的名字叫raw_input。
>>> text = input()
What are you waiting for?
>>> text
What are you waiting for?
>>> name = input('What...is your name?\n')
What...is your name?
Arthur, King of the Britons!
>>> name
Arthur, King of the Britons!
>>> prompt = 'What...is the airspeed velocity of an unladen swallow?\n'
>>> speed = input(prompt)
What...is the airspeed velocity of an unladen swallow?
42
>>> int(speed)
42
循环:
def countdown(n):
while n > 0:
print(n)
n = n - 1
print('Blastoff!')
# break 语句 来跳出循环。
while True:
line = input('> ')
if line == 'done':
break
print(line)
print('Done!')
for i in range(4):
print('Hello!')
网友评论