:
x 5, True
else, , True,
else return False
else condition... else return False , False, False -False-. , . if:
def function(x):
if len(x) == 5: return True
else: return x[0] == x[-1]
def funcor(x):
return (len(x)==5) or (x[0] == x[-1])
def funcany(x):
return any((len(x)==5, x[0] == x[-1]))
def funcverbal(sequence):
return len(sequence)==5 or sequence.endswith(sequence[0])
def test(func):
print('Testing %s function' % func)
for value in ('12345','annb','ansa','23424242',('1','2','1'), 123, '131'):
try:
print ("%r -> %r" % (value,func(value)))
except:
print ("Failed to call function with " + repr(value))
print(10 * '-'+'Finished testing '+str(func) + 10 * '-')
for thisfunction in (function, funcor, funcany, funcverbal):
test(thisfunction)
( , -)
isPalindrome , , , "anna" :
, "anna" 2 (1 0), ,
'a' 'a', ,
isPalindrome 'nn'
, "nn" 2 (1 0), ,
'n' 'n', ,
isPalindrome ''
, '' 2 (1 0), . , .
, , , .
def isPalindrome(s):
return s==s[::-1]