Python のデータ型
% python Python 2.2.2 (#1, Oct 31 2002, 13:31:46) [GCC 2.95.3 20010315 (release)] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> 654 / 321 2 >>> 654. / 321 2.0373831775700935 >>> |
#!/pub/sol8/bin/python import string, sys def enzanf(a,b): print '実行' print ' a = %g' % a print ' b = %g' % b print 'a + b = %g' % (a + b) print 'a - b = %g' % (a - b) print 'a * b = %g' % (a * b) if b != 0: print 'a / b = %g' % (a / b) if __name__ == '__main__': a = string.atof(sys.argv[1]) b = string.atof(sys.argv[2]) enzanf(a, b) |
>>> a = "Hello, World" >>> b = "Hello, \"World\"" >>> print b Hello, "World" >>> c = 'Hello, "World"' >>> print c Hello, "World" >>> d = """Hello! ... Hi! Howdy. ... """ >>> print d Hello! Hi! Howdy. >>> |
>>> a = "Hello " >>> b = "World " >>> a + b 'Hello World ' >>> a * 5 + b 'Hello Hello Hello Hello Hello World ' >>> a = "Hello %s" >>> a % b 'Hello World ' >>> |
>>> a = [1, 3, 5, 7, 9] >>> a[0] 1 >>> a[3] 7 >>> a[1:4] [3, 5, 7] >>> a[3] = 8 >>> a [1, 3, 5, 8, 9] >>> a.append(11) >>> a [1, 3, 5, 8, 9, 11] >>> del a[2] >>> a [1, 3, 8, 9, 11] |
>>> d = {'spam': 2, 'eggs': 3} >>> d['spam'] 2 >>> d.keys() ['eggs', 'spam'] >>> d.values() [3, 2] >>> |
#!/pub/sol8/bin/python import sys stack = [] def push(data): print 'push: %s' % data stack.append(data) print 'stack: %s' % stack def pop(): data = stack.pop() print 'pop: %s' % data print 'stack: %s' % stack if __name__ == '__main__': for i in sys.argv[1:]: push(i) while len(stack) > 0: pop() |
#!/pub/sol8/bin/python import sys phone = { '菊地': '8336', '塩田': '8337', } def get_phone(teacher): if teacher in phone.keys(): print '%s 先生の電話は 088-844-%s' % (teacher, phone[teacher]) else: print 'そんな先生知らね!' if __name__ == '__main__': get_phone(sys.argv[1]) |
<a href="http://www.is.kochi-u.ac.jp/~tkikuchi/>菊地のホームページ