Created
May 16, 2016 22:42
-
-
Save chuck0523/f0c8c66e9642024b6ef2babec57be34a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
# 関数 | |
def hello(name, times = 3): | |
print "hello %s! " % name * times | |
hello("tom", 2) | |
hello("bob") | |
hello(name = "me", times = 10) | |
# 変数のスコープ | |
name = "chuck" | |
def getName(): | |
name = "anohter_chuck" | |
return name | |
print name | |
print getName() | |
# pass: 何も処理をしない。 | |
def hello2(): | |
pass | |
# pythonでは空の関数が定義できない。passを使って実装。 | |
# リスト <> 関数 map | |
def double(x): | |
return x * x | |
print map(double, [2, 5, 8]) | |
# 無名関数(lambdaを使う) | |
print map(lambda x:x * x, [2, 5, 8]) | |
# オブジェクト(変数と関数をまとめて管理) | |
# クラスとインスタンス | |
class User(object): | |
def __init__(self, name): | |
self.name = name | |
def greet(self): | |
print "my name is %s!" % self.name | |
bob = User("Bob") | |
tom = User("Tom") | |
print bob.name | |
bob.greet() | |
tom.greet() | |
# 継承 | |
class User(object): | |
def __init__(self, name): | |
self.name = name | |
def greet(self): | |
print "my name is %s!" % self.name | |
class SuperUser(User): | |
def shout(self): | |
print "%s is SUPER!!" % self.name | |
tom = SuperUser("Tom") | |
tom.greet() | |
tom.shout() | |
# モジュール: デフォルトで用意されている便利な部品 | |
import math, random | |
# 一部のみimport | |
from datetime import date | |
print math.ceil(5.2) # 6.0 | |
for i in range(5): | |
print random.random() | |
print date.today() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment