some tips about python One

来源:互联网 发布:鲜活的数据 编辑:程序博客网 时间:2024/05/10 06:57

a = [1,2,3,4,5]  #列表

multi_dim_a = [[1,2,3],

    [4,5,6],

    [7,8,9]]   #多维列表

print(a[1])  #2

print(multi_dim_a[0][1]) #2,第0行第1个元素

print(a[0]) #1

print(multi_dim_a[2][2])#5,第2行第2个元素,从0开始

a_list = [1,2,3,4,5,6,7,8,9] #列表

d = {'apple':1,'pear':2,'orange':3} #字典

d1 = {1:'a','c':'b'} #字典 {key : value}

print(d['apple']) #1,字典输出

print(a_list[0]) #1

del d['pear'] #字典删除

print(d)

d['b'] = 20 #字典添加元素 key value

print(d) #字典无序

def fun():

d = {'apple' : [1,2,3], 'pear' : {1 : 3;3 : 'a'},'orange':fun()} #字典的value可以为数字,字符串,列表,字典,函数

print(d['pear'][3]) #a


import time  #引入模块

print(time.localtime()) #加载模块功能


import time as t #缩减模块名称

print(t.localtime())


from time import time,localtime #仅仅引入time和localtime功能

print(time.localtime())

print(time.time()) # 因为指明了从time模块引入,可以简写为print(localtime()) print(time())


from time import * #引入time模块的所有功能

print(localtime())

print(time()) #同理可以简写

print(gettime())


#定义自己的模块

#新建文件m1,py作为模块

#在m1.py中输入

def printdata(data):

print(data)

#保存

#打开另一个practice.py文件

#确保m1.py和practice.py在同一个目录下

import m1 #引入m1模块

m1.printdata('I am python') #调用m1模块的功能

#在mac系统中,python下载好的外部模块会保存在,Macintosh->Library->Frameworks->Python.framework->versions->3.5->lib->python3.5->site.packages

#可以把自己定义的模块放在此目录下,和其他模块一样引用


a = True

while a:

b = input('type something') #输入后返回的是字符串

if b == '1':

a = False #退出循环

else:

pass #什么都不做,继续循环

print('finish running')


while True:

b = input('type something')

if b == '1':

break #直接跳出循环,print('finish running')

else:

pass

print('still in while')

print('finish running')


while True:

b = input('type something')

if b == '1':

continue #直接跳转到下一次的判断,while True

else:

pass

print('still in while')

print('finish running')


try:  #尝试

file = open('newbeginning','r')

except Exception as e: #接收错误,存储在变量e中

print(e) #打印错误信息,No such file or directory: 'newbeginning'


try:

file = open('newbeginning','r+') #r+ means 只读+写入

except Exception as e:

print('there is no file named as newbeginning')

response = input('Do you want to create a new file?')

if response == 'y':

file = open('newbeginning','w')

else:

pass #什么都不做

else: #对应 except Exception as e:

file.write('AAAA')

file.close()


a = [1,2,3]

b = [4,5,6]

zip(a,b)  #合并列表,生成对象<zip object at 0x1056a1648>

list(zip(a,b)) #利用list将其可视化 [(1,4),(2,5),(3,6)]

for i,j in zip(a,b):

print(i/2,j*2)  #i/2自动转换成浮点数 0.5,1.0,1.5

list(zip(a,a,b)) # [(1,1,4),(2,2,5),(3,3,6)]


def fun1(x,y):

return (x+y)

fun1(2,3)


fun2 = lambda x,y : x+y #生成一个简单的函数

fun2(2,3)


map(fun1,[1],[2]) #[1]对应fun1的第一个参数,[2]对应fun1的第二个参数,生成一个object

list(map(fun1,[1],[2])) #将结果进行可视化,3,实现了函数的功能

list(map(fun1,[1,3],[2,5]))#结果为[3,8]


import copy

a = [1,2,3]

b = a

id(a) #a在硬盘中的索引

id(b) #b在硬盘中的索引

b[0] = 11 # a为[11,2,3]

a[1] = 22 # b为[11,22,3],a或b的改变都会影响索引指向的数据

print(id(a) == id(b)) #True

c = copy.copy(a) # copy模块的copy功能

print(id(a) == id(c)) #False,索引不同

c[1] = 2222#a的值不会受到影响,c为[11,2222,3]

#但是copy.copy()可能对于list中的list元素,比如[1,2,[1,2,3]]其中的[1,2,3],复制对象或者原对象的修改会对彼此造成影响

#想要完全摆脱对彼此的影响,可以使用copy.deepcopy()

e = copy.deepcopy(a)

#总结一下,b = a 指向同样的存储空间,彼此的修改会对对方产生影响

#copy.copy() 浅复制,第一层的元素,比如[1,2,[1,2,3]]中的1和2不会指向同样的存储空间,但是更深一层的[1,2,3]元素还是会指向同样的存储空间

#copy.deepcopy() 深复制,所有的元素都指向不同的存储空间,彼此互不影响


import threading

def thread_job():

print('This is an added Thread, number is %s' % threading.current_thread())

def main():

added_thread = threading.Thread(target = thread_job) #创建一个线程,它的target目标是thread_job

added_thread.start() #运行线程

print(threading.active_count()) #一共有几个线程

print(threading.enumerate()) #查看有哪几个线程

print(threading.current_thread()) #查看正在运行的线程

if __name__ == '__main__':

main()


原创粉丝点击