input用于接收用户输入
>>> input("Please input:")
Please input:2
'2'
值互换
>>> x=3
>>> y=5
>>> x,y = y,x
>>> x
5
>>> y
3
单引号与双引号,转义符与java用法一致
>>> print("Let's go")
Let's go
>>> print('"This is test"')
"This is test"
>>> print("'This\'s test'")
'This's test'
长字符串 """
>>> t = """
test1
test2
test3
"""
>>> print(t)
test1
test2
test3
字符串与数字计算
>>> print("This is test\n"*10)
This is test
This is test
This is test
This is test
This is test
This is test
This is test
This is test
This is test
This is test
条件语句
if 条件:
xxx
else:
xxx
while 条件:
条件true时运行 代码体
break跳出一层循环体
count = 3
while count>0:
temp = input("请输入数字:")
guess = int(temp)
if guess==8:
print("猜对了")
break
else:
if(guess>8):
print("大了")
else:
print("小了")
count = count-1
print("end")
请输入数字:9
大了
请输入数字:2
小了
请输入数字:8
猜对了
end
random
random.randint(1,10) 1到10内随机数
>>> import random
>>> random.randint(1,10)
3
random.getstate() 获取之后随机数产生时的计算机内部状态
random.setstate() 将内部状态赋值
>>> import random
>>> x = random.getstate()
>>> random.randint(1,100)
72
>>> random.randint(1,100)
72
>>> random.randint(1,100)
62
>>> random.randint(1,100)
96
>>> random.setstate(x)
>>> random.randint(1,100)
72
>>> random.randint(1,100)
72
>>> random.randint(1,100)
62
>>> random.randint(1,100)
96