码迷,mamicode.com
首页 > 编程语言 > 详细

2. Python语言基本语法

时间:2016-04-15 21:49:10      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:


(1)定义变量

1 a = 10
2 b = 2
3 c = a + b

(2)判断语句

 1 score = 90
 2 
 3 if score >= 80 :
 4    print("优秀")
 5 elif score >= 70 :
 6    print("中等")
 7 elif score >= 60 :
 8   print("及格")
 9 else:
10    print("不及格")

(3)循环

1 for i in range(0,5,2):
2    print(i)
3 print("================")
4 for i in range(0,5):
5    print(i)
6 print("================")
print("Item {0} {1}".format(10,"eee"))
for i in range(0,5):
print(i)
else:
print("循环结束")

(4)函数

 1 def sayHello():
 2    print("Hello world")
 3 
 4 sayHello()
 5 
 6 def myMax(x,y):
 7    if x > y :
 8       print x,"is Max"
 9    else:
10       print y,"is Max"
11 myMax(10,9)
12 
13 def myMaxVal(x,y):
14    if x > y :
15       return x
16    return y
17 
18 print(myMaxVal(10,8))

(5)对象

 1 class Hello:
 2    def __init__(self, name):
 3       self.name = name
 4       print("Hello 构造函数")
 5    def __del__(self):
 6       print("Hello 析构函数")
 7 
 8    def sayHello(self):
 9       print("Hello {0}".format(self.name))
10 
11 class Hi(Hello):
12    def __int__(self,name):
13       Hello.__init__(self,name)
14       self.name = name
15       print("Hi 构造函数")
16 
17    def sayHi(self):
18       print("Hi {0}".format(self.name))
19 
20 #Hello对象
21 h = Hello("zhangsan")
22 h.sayHello()
23 
24 #Hi对象
25 hi = Hi("lisi")
26 hi.sayHi()
27 hi.sayHello()

 

 

 

 

2. Python语言基本语法

标签:

原文地址:http://www.cnblogs.com/renhl/p/5396721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!