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