Python
定義變量
直接定義
a=1
b=2
a+b
定義函數
def hello():
print "hello world!"
def max(a,b):
if a>b:
return a
else:
return b
hello()
print(max(8,6))
循環
for i in range(0,100):
print("Item{0},{1}".format(i,"hello,world!"))
此處的{0},{1},作爲佔位符號,把後面format之後的數據進行輸入。
面向對象--定義類
Example1:
class hello:
def __init__(self,name): ***此處的self,name是怎麼個意思?
self._name = name
def sayhello(self):
print("hello {0}".format(self._name))
h = hello("coin")
h.sayhello()
class hello:
def __init__(self,name):
self._name = name
def sayhello(self):
print("hello {0}".format(self._name))
class Hi(hello):
#此處使用賦類的方法
def __init__(self,name):
hello.__init__(self,name)
def sayHi(self):
print ("Hi {0}i".format(self._name))
h = hello("coin")
h.sayhello()
hi = Hi("zhangsan")
hi.sayHi()