In [2]:
>>> x = int(input("please input number"))
In [3]:
>>> if x < 0:
... x = 0
... print("Negative changed to zero")
... elif x == 0:
... print("Zero")
... elif x == 1:
... print("Single")
... else:
... print('More')
...
In [4]:
>>> words = ['Cat','Dog','Windows']
>>> for w in words :
print(w, len(w))
In [5]:
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
>>> words
Out[5]:
In [6]:
>>> for i in range(10):
print(i,end=",")
In [13]:
>>> a = ['Mary','had','a','big','apple']
... for i in range(len(a)):
print(i,a[i])
In [14]:
>>> list(range(5))
Out[14]:
In [18]:
>>> for n in range(2,10):
for x in range(2,n):
if n % x == 0:
print(n,'quals',x,'*',n//x)
break
else:
print(n,'是一个质数')
In [25]:
>>> for n in range(2,10):
if n % 2 == 0:
print(n,'is an even number')
continue
print(n, 'is an odd number')
In [ ]:
>>> while True:
... pass
In [9]:
>>> def fib(n):
a, b = 0, 1
while a < n:
print(a,end=' ')
a, b = b, a+b
print()
>>> fib(2000)
In [10]:
>>> f = fib
>>> f(200)
In [11]:
>>> print(fib(0))
In [14]:
>>> def fib2(n):
a, b= 0, 1
result = []
while a < n:
result.append(a)
a, b =b, a+b
return result
>>> f100 = fib2(100)
>>> f100
Out[14]:
In [24]:
>>> def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise OSError('uncooperative user')
print(complaint)
In [28]:
>>> i = 5
... def f(arg=i):
print(arg)
>>> i = 6
>>> f()
In [32]:
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
In [37]:
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print("-- This parrot wouldn't", action, end=' ')
print('If you put', voltage, 'volts through it')
print('--Lovely prumpt, the', type)
print("--It's", state, '!' )
In [40]:
parrot(1000)
parrot(voltage=1000)
parrot(voltage=100000, action='vooooom')
parrot(action='voooooom', voltage=1000000)
parrot('a million', 'bereft of life', 'jump')
parrot('a thousand', state = 'pushing up the daisies')
In [45]:
>>> def function(a):
... pass
...
>>> function(0, a=0)
In [55]:
def cheeseshop(kind, *arguments, **keywords):
print("-- Do you have any", kind, "?")
print("-- I'm sorry, we're all out of", kind)
for arg in arguments:
print(arg)
print("-" * 40)
keys = sorted(keywords.keys())
for kw in keys:
print(kw, ":", keywords[kw])
In [58]:
cheeseshop('Limburger', "It's very runny, sir!",
"It's really very, VERY runny, sir!",
shopkeeper = "Mike colin",
client = "John kees",
sketch = "Cheeseshop Sketch")
In [59]:
def write_mutiple_items(file, separator, *args):
file.write(separator.join(args))
In [62]:
>>> def concat(*args, sep='/'):
... return sep.join(args)
...
>>> concat('earth','mars','moons')
Out[62]:
In [65]:
>>> concat('earth','mars','moon', sep='.')
Out[65]:
In [67]:
>>> list(range(4,9))
Out[67]:
In [71]:
>>> args = [3,9]
>>> list(range(*args))
Out[71]:
In [73]:
>>> def parrot(voltage, state='a stiff', action='voom'):
... print("-- This parrot wouldn't", action, end=' ')
... print("if you put", voltage, "volts through it.", end=' ')
... print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
In [76]:
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
>>> f(1)
Out[76]:
In [77]:
>>> pairs = [(1,'one'), (2,'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair:pair[1])
>>> pairs
Out[77]:
In [81]:
>>> def my_function():
... """Do nothing, but document it.
... fadsfdf
... No, really, it doesn't do anything.
... """
... pass
...
>>> print(my_function.__doc__)
In [82]:
>>> def f(ham: str, eggs: str = 'eggs') -> str:
... print("Annotations:", f.__annotations__)
... print("Arguments:", ham, eggs)
... return ham + ' and ' + eggs
...
>>> f('spam')
Out[82]: