Python编程基础
#以下是Python编程基础知识
import sys
from sys import argv,path #导入特定成员
print('path:',path)
print('============Python import mode===========================')
print('命令行参数为:')
for i in sys.argv:
print(i)
print('\n python 路径为',sys.path)
count =100
miles =1000.0
name ="runoob"
a,b,c = 1,2,"runoob"
print(100)
print(miles)
print(name)
print(a)
print(b)
print(c)
'''
数据标准
'''
"""
注释
"""
#标准数据类型#
#列表
list = ["abc",1,1.1]
#元组
tuple =("abcd",786,2.23,"runoob",70.2)
#集合 进行成员关系测试和删除重复元素 {}不能创建空集合
sites = {'google','taobao','jD',123.1,'fb',123}
if 123.1 in sites:
print("Runoob在集合中")
else:
print("Runoob不在集合中")
a = set('aaaadddgd')
b = set('aabbtt')
print(a-b)
print(a|b)
print(a&b)
print(a^b)#a 与 b中不同时存在的元素
#字典 键值对
dict = {}
dict['one'] = "1-菜鸟教程"
dict[2] = "222222"
tiny_dict = {'name':'runoob','code':1,'site':'www.runoob.com'}
print(tiny_dict)
print(tiny_dict.keys())
print(tiny_dict.values())
#列表推导式
#[表达式 for 变量 in 列表] 或者 [表达式]
names = ['Bob','Tom','alice','Jerry','Wendy','Smith']
new_names = [name.upper()for name in names if len(name)>3]
print(new_names)
#字典推导式{ key_expr: value_expr for value in collection }或{ key_expr: value_expr for value in collection if condition }
listdemo = ['Google','Runoob', 'Taobao']
# 将列表中各字符串值为键,各字符串的长度为值,组成键值对
newdict = {key:len(key) for key in listdemo}
print(newdict)
#集合推导式
# { expression for item in Sequence }
# 或
# { expression for item in Sequence if conditional }
a = {x for x in 'abracadabra' if x not in 'abc'}
#元组推导式
# (expression for item in Sequence )
# 或
# (expression for item in Sequence if conditional )
a = (x for x in range(1,10))
###############类
class MyClass:
i = 12345
def f(self):
return 'hello world'
#实例化
x = MyClass()
class Complex:
def __init__(self, realpart,imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0,-4.6)
#类定义
class people:
#定义基本属性
name = ''
age = 0
#私有属性类外无法访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s say : I %d years" %(self.name,self.age))
#实例化
pp = people("zsj",4,100)
pp.speak()
#继承
class student(people):
grade = ''
#调用父类的构函
def __init__(self,n,a,w,g):
people.__init__(self,n,a,w)
self.grade = g
#覆写父类的方法
def speak(self):
print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))
s = student('ken',10,60,3)
s.speak()
############################
def fibonacci(n): # 生成器函数 - 斐波那契
a, b, counter = 0, 1, 0
while True:
if (counter > n):
return
yield a
a, b = b, a + b
counter += 1
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成
while True:
try:
print (next(f), end=" ")
except StopIteration:
sys.exit()
#if语句
a = 'hello'
if('h' in a):
print("h is in a")
else:
print("h is not in a")
if('H' not in a):
print("H is not in a")
else:
print("H is in a")
#while语句
a,b = 0,1
while b < 10:
print(b)
a,b = b,a+b
else:
print (b, " 大于或等于 10")
a,b = 0,1
while b < 10:
print(b,end=',')
a,b = b,a+b
#迭代器
list =[1,2,3,4]
it = iter(list)
for x in it:
print(x,end= ' ')
#异常处理机制
while True:
try:
print(next(it))
except StopIteration:
sys.exit()

浙公网安备 33010602011771号