#!/usr/bin/env python
# coding: utf-8
for n in range(100):
if n % 10 == 3:
print('さーん', end=',')
elif n % 3 == 0:
print('あほ', end=',')
else:
print (n, end=' ,')
print('')
あほ,1 ,2 ,さーん,4 ,5 ,あほ,7 ,8 ,あほ,10 ,11 ,あほ,さーん,14 ,あほ,16 ,17 ,あほ,19 ,20 , あほ,22 ,さーん,あほ,25 ,26 ,あほ,28 ,29 ,あほ,31 ,32 , さーん,34 ,35 ,あほ,37 ,38 ,あほ,40 , 41 ,あほ,さーん,44 ,あほ,46 ,47 ,あほ,49 ,50 ,あほ,52 ,さーん,あほ,55 ,56 ,あほ,58 ,59 ,あほ, 61 ,62 ,さーん,64 ,65 ,あほ,67 ,68 ,あほ,70 ,71 ,あほ,さーん,74 ,あほ,76 ,77 ,あほ,79 ,80 , あほ,82 ,さーん,あほ,85 ,86 ,あほ,88 ,89 ,あほ,91 ,92 ,さーん,94 ,95 , あほ,97 ,98 ,あほ,
for ( str : array ) { dosomething(); doanything(); }
for str in iteratable : dosomething() doanything()
>>> for num, enum in enumerate(['a','b','c']): print (str(num) + ':' + enum) 0:a 1:b 2:c
>>> True and False False >>> True or False True >>> not True False
>>> True && False SyntaxError: invalid syntax >>> True || False SyntaxError: invalid syntax >>> ! True SyntaxError: invalid syntax
>>> None or 'string' 'string'
0 より小さかったら 0、それ以外はそのまま
>>> x=10 >>> 0 if x < 0 else x 10 >>> x=-10 >>> 0 if x < 0 else x 0
#!/usr/bin/env python
# coding: utf-8
import sys
flag=1
odd_number=1.0
qpai=0
diff=sys.float_info.max
while diff > 1E-8 :
diff = 1.0 / odd_number
qpai = qpai + flag * diff
flag *= -1
odd_number += 2
print(qpai * 4)
$ qpai.py 3.1415926735902504
#!/usr/bin/env python
# coding: utf-8
for n in range(6, 20) :
if n % 3 == 0 :
print(str(n) + ": continue")
continue
if n % 5 == 0 :
print(str(n) + ": break")
break
print(n)
else :
print (str(n) + ": all done")
}
$ python loop1.py 6: continue 7 8 9: continue 10: break
#!/usr/bin/env python
# coding: utf-8
for n in range(6, 20) :
if n % 3 == 0 :
print(str(n) + ": continue")
continue
if n % 15 == 0 :
print(str(n) + ": break")
break
print(n)
else :
print (str(n) + ": all done")
}
$ python loop2.py 6: continue 7 8 9: continue 10 11 12: continue 13 14 15: continue 16 17 18: continue 19 19: all done
>>> if False : pass else : 文は ('else') else >>>
>>> if True : pass else : print ('else') >>>