Notes for beginners!
Avoid reserved words as they are used in built-in functions such as list, abs, str! See below for details. http://qiita.com/cm3/items/6a856c44dd92632aa54f
bool
a and b #True if both a and b are true
a or b #True if a or b is true
not a #True if a is false
none
if hoge is None:
#Processing when None
if hoge is not None:
#When not None
array
values = [1, 2, 3, 4]
print values[0] #1
print "length=" + str( len(values) ) #length=4
push
list = ["A", "B", "C"]
list.append("D")
print list # ["A", "B", "C", "D"]
Other related functions http://www.pythonweb.jp/tutorial/list/
dict
d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}
dict
for k, v in d.items():
print k, v # Tanaka 80, Yamada 30, Suzuki 40
for k in d.keys():
print k, d[k] # Suzuki 40, Yamada 30, Tanaka 80
for v in d.values():
print v # 80, 30, 40
for k, v in d.iteritems():
print k, v # Tanaka 80, Yamada 30, Suzuki 40
for
#Loop from 0 to 9
for i in range(0, 10):
print "hoge " + str(i)
for
#Scan the contents of the array
values = [ "hoge", "fuge", 123 ]
for value in values:
print value
if
if hoge < 1:
print "fuga1"
elif hoge < 2:
print "fuga2"
else:
print "fuga3"
cast
cast
print "hoge" + str( 100 ) #hoge100
print 10+int("5") #15
floor
import math
math.floor(x)
math.ceil(x)
function
def hoge(a,b):
print "hoge";
return "hoge" + a + b
#call
hoge("AAA","BBB")#hogeAAABBB
__name__
A special variable __name__
that you often see. In the case of the main program, it will be __name__ ==" __main__ "
. When you import, the file name is entered. It seems that the following notation is often used to distinguish them.
__name__
if __name__ = "__main__":
print "It's the main program"
#else:
# print "It's imported"
How to use http://www.python-izm.com/contents/application/json.shtml Load and save http://d.hatena.ne.jp/fenrifja/20130306/1362571700
http://www.tohoho-web.com/python/class.html#class
http://python.matrix.jp/pages/tips/import.html
http://www.tohoho-web.com/python/index.html
Recommended Posts