switch-caseで不等号による条件分岐。
var answer=prompt("How old are you?")
switch(true){
case answer<20:
console.log("you are under " + answer)
break;
case answer<30:
console.log("you are under "+ answer)
break;
case answer<40:
console.log("you are under "+ answer)
break;
default:
console.log("have fun")
}
2014年8月8日金曜日
2014年7月16日水曜日
python: Dictionary .item()
x = {"a": "apple","b": "bee","c": "cat"}
print x.items()
>>> [('a', 'apple'), ('c', 'cat'), ('b', 'bee')]
print x.items()
>>> [('a', 'apple'), ('c', 'cat'), ('b', 'bee')]
2014年7月11日金曜日
2014年7月10日木曜日
python: list generator
lst = [x for x in range(100) if x % 3 == 0]
print lst
print lst
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
2014年7月9日水曜日
2014年7月8日火曜日
python zip
list_1 = [3, 9, 12, 15, 18]
list_2 = [2, 4, 8, 16, 32]
for a, b in zip(list_1, list_2):
if a>b:
print "a is bigger"
elif b>a:
print "b is bigger"
else:
print "a is equal to b"
list_2 = [2, 4, 8, 16, 32]
for a, b in zip(list_1, list_2):
if a>b:
print "a is bigger"
elif b>a:
print "b is bigger"
else:
print "a is equal to b"
python: enumurate
choices = ['soccer', 'tennis', 'basketball', 'balleyball']
print 'Your choices are:'
for index, item in enumerate(choices):
print index+1, item
print 'Your choices are:'
for index, item in enumerate(choices):
print index+1, item
2014年7月7日月曜日
2014年7月2日水曜日
python function
def square_cal(length,width):
return length*width
def square(length,width):
if length==width:
return square_cal(length,width)
else:
return "Not square"
return length*width
def square(length,width):
if length==width:
return square_cal(length,width)
else:
return "Not square"
python: print "%.2f" %var
var=1.234567
print "%.2f" %var
print "%.3f" %var
print "%.4f" %var
print "%.5f" %var
print "%.2f" %var
print "%.3f" %var
print "%.4f" %var
print "%.5f" %var
1.23
1.235
1.2346
1.23457
登録:
投稿 (Atom)