python學習

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()

 

example2:

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()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章