博客列表 >python 30-40

python 30-40

XuanGG的博客
XuanGG的博客原创
2018年03月19日 17:29:35528浏览

# test30
'''
scores = [10,20,30,40,60]
for i in scores:
   if i ==30:
       continue
 
   print i
'''


#---------------------------------------------------------------------------

# test 31 异常处理 try ...excep语句来处理异常,吧可能引发异常的语句放在Try快中
'''
try:
   a =1
   print a
except:
   print 'error'
print 'done'
'''

# test 32 字典就是一个键/值对的集合。建必须是唯一的且只能是简单对象
'''
score = {
   'mike':55,
   'xuan':100,
   'GG':20

}

for names in score:
   print names,
   print score[names]

'''

# test 33 模块
# 模块可以理解为是一个包含了函数和变量的py文件。
# 在你的程序中引入了某个模块,就可以使用其中的函数和变量。
'''
import random

print random.randint(1, 10)
print random.choice([1, 3, 5])

from math import pi
print pi
'''
# test 34 函数的默认参数
'''
def name(a='1'):
   print a
name()
'''
# test 35 面向对象
#面向对象编程最主要的两个概念就是:类(class)和对象(object)。
#类是一种抽象的类型而对象是这种类型的实类
'''
class myclass:
   name = 'sam'
   def sayhello(self):
       print 'Hello %s' % self.name

mc = myclass()
print mc.name
mc.sayhello()
'''
'''
class Car:
   speed = 60.0
   def drive(self,distance):
       time = distance / self.speed
       print time
car = Car()
car.drive(100.0)
car.drive(200)
'''

# test38 and or
'''
a = 3
result = (a > 0) and "big" or "small"
print result

a = 0
b = 17
c = True and a or b
d = (True and [a] or [b])[0]
print c, d
'''

# test 39 元祖 也是一种序列 在创建后不能被修改
'''
a = (1,2,3,4,5)

for i in a :
   print a[0]
'''

#40 数学运算python自带了一些基本的数学运算方法,包含在math模块中。
'''
import math
print math.pi
print math.floor(1.2)
'''

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议