交互模式下面默认调用repr(a)这个方法
>>>a=1
>>>a
1
代码块,多敲一次回车
>>> n=10
>>> if n==10:
...
... print ("equal")
... else:
...
... print ("not equal")
...
equal
>>> print ("a")
a
input 读入的都是字符串
>>> s=input("请输入一个数字:")
请输入一个数字:10
>>> s
'10'
>>> print ("s")
s
>>> print (s)
10
>>>
>>> type(s)
<class 'str'>
>>> myage=input("请输入的生日的月份:")
请输入的生日的月份:6
>>> if myage==7
File "" , line 1
if myage==7
^
SyntaxError: invalid syntax
>>>
>>> if myage==7:
...
... print ("its my age")
... else:
...
... print ("its not my age")
...
its not my age
>>>
列表、元组和字符串都是序列
>>> for i in range(5):
...
... print (i)
...
0
1
2
3
4
>>> for i in "abc"
File "" , line 1
for i in "abc"
^
SyntaxError: invalid syntax
>>> for i in "abc":
...
... print (i)
...
a
b
c
>>> for i in int[1,2,3]:
...
... print (i)
...
Traceback (most recent call last):
File "" , line 1, in
TypeError: 'type' object is not subscriptable
>>> for i in [1,2,3]:
...
... print (i)
...
1
2
3
>>> for i in (1,2,3):
...
... print (i)
...
1
2
3
>>> for i in range(5,10):
...
... print (i)
...
5
6
7
8
9
>>> for i in range(10,-1,-1):
...
... print (i)
...
10
9
8
7
6
5
4
3
2
1
0
for i in range(10,-1,-1):
…
… continue
…
… print (i)
…for i in range(10,-1,-1):
…
… if (i==5):
…
…
… print(i)
…
5for i in range(10,-1,-1):
…
… if(i==5):
…
…
… break
…
>>> for i in range(10):
... if i==8:
... break
... else:
... print("no break")
...
>>> for i in range(10):
... continue
... else:
... print("no break!")
...
no break!
>>> for i in range(10,-1,-1):
... if(i==8):
...
...
... break
... else:
...
... print ("no break")
...
>>> for i in range(10,1,1):
...
... continue
... else:
...
... print("no break")
...
no break
>>>
>>> for i in range(10):
...
... if i%2==1:
...
...
... continue
...
... print(i)
...
0
2
4
6
8
>>>
>>> for i in range(10):
...
... if(i%2==1):
...
...
... break
...
... print (i)
...
0
>>>
where
>>> while n>=1:
...
... print (n)
...
... n-=1
...
5
4
3
2
1
>>> n=n-1 -->n-=1
>>> while n>=1:
...
... if n>=2:
...
...
... print (n)
...
... else:
...
...
... break
...
... n-=1
...
5
4
3
2
>>>
>>> while 1:
...
... break
... else:
...
... print ("no")
...
>>> n=5
>>> while n>=6:
...
... print (n)
... else:
...
... print("no break")
...
no break
>>>
练习2:
你可以输入3种字母:e、a、r
如果等于e,那么退出循环
如果输入 a,执行continue
如果输入r,那么再读取一次字母,并打印
只允许输入三次字母,重输入字母的不算在内
while 1:
letter = input("请输入指令")
if letter == "e":
break
elif letter =="a":
continue
elif letter == "r":
letter = input("请输入指令")
print(letter)
n=3
while n>=1:
letter = input("请输入指令")
n-=1
if letter == "e":
break
elif letter =="a":
continue
elif letter == "r":
letter = input("请输入指令")
print(letter)