#元祖#print('元祖开始')tuple=('ab',111,'bb',222,1.2)
tinytuple =(123,'aa')print(tuple)print(tuple[0])print(tuple[1:3])print(tuple[2:3])print(tuple[2:])print(tinytuple *2)print(tinytuple+tuple)print('元祖结束')print('set集合开始')
student ={'tom','jock','zhenni','mary','rose'}print(student)if'rose'in student:print('rose在集合')else:print('rose不再集合zhong')
a =set('ascd')
b =set('efgh')print(a)print(a-b)print(a|b)print(a^b)dict={}dict['one']='1-可以'dict[2]='2-可以'
tinydict ={'name':'刘世豪','id':1,'site':'www.wocao.com'}print(dict['one'])print(dict[2])print(tinydict)print(tinydict.keys())print(tinydict.values())#循环字符串
a ='asdsadsa'for i in a :print(end=i)#循环列表
a =[1,2,3,4]for i in a :print(i)'''按照字符串控制循环次数----------'''import random
a ='asdsadsa'#字符串个数为8 循环8次 i == 5 也没用 字符串不能和数字进行比较直接忽略for i in a:print(random.randint(1,10))if i ==5:break'''使用循环测试列表------------'''
asd =['六十','撒旦撒','撒打啥的阿斯顿是算','撒倒萨撒旦撒','撒旦撒旦撒']for i in asd:print(i,len(i))#len方法输出一个列表中元素的长度# #如果len方法里面是列表名 则输出列表长度
ss ='aaa'print('shuru')whileTrue:if ss ==input():breakprint('输入错误')print('成功')for i inrange(10):if i%2!=0:print(i)continue
i+=2print(i,'aaa')"""List方法---------------"""
list1 =[123,234]
list2 =[123,345]
list1 *=5print(list1 == list2)print(dir(list))#list里常用的方法print(list1.count(123))#计算元素出现次数print(list1.index(123,4,6))#返回索引位置 例 从4开始都6 123这个元素出现在第几个print(list1.reverse())#反转列表'''list排序----------------------'''
list1 =[1,6,4,32,44,31]for i in list1:print(i)print(list1)print(list1.sort())#列表元素从小到大排列print(list1)
list1.sort(reverse=True)#直接倒序排列列表print(list1)print(list1.reverse())#想从大到小拍需要sort排完之后在用反转方法返回print(list1)
list2 = list1[:]#可以拷贝列表 把list1拷贝到list2'''元组'''
temp =1print(temp)print(type(temp))#输出元素类型 这里是一个int型
temp2 =[]print(type(temp2))#这里是个空列表
temp3 =()print(type(temp3))#这里是个空元组
temp4 =(1,)print(temp4)print(type(temp4))#这里是个元组print(8*(8))#直接做一个乘法运算 得64print(8*(8,))#这里定义一个元组然后元组的元素输出8次 (8,8,8,8,8,8,8)
temp5 =('asd','dsa','ddd','rrr')
temp5 = temp5[:2]+('hhh',)+ temp5[2:]#往元组中插入一个数据,先把元组两边拆开,然后放入元素print(temp5)del temp5 #删除元组 一般不用,可以自动回收
states ={'oregen':'OR','Florida':'FL','California':'CA','New York':'NY','Michigan':'MI'}#定义键值 对应的关系
cities={'CA':'San Francisco','MI':'Detroit','FL':'Jacksonville'}
cities['NY']='New YorkOne'
cities['OR']='Portland''''字典-----------------'''print('-1'*10)print('NY state has :',cities['NY'])print('OR State has :',cities['OR'])#键是 cities['NY'] = 值是'New YorkOne'print('-2'*10)print("Michigan's abbreviation is : ",states['Michigan'])print("Florida has avvreviation is :", states['Florida'])#键是 states['Michigan'] = 值是'MI'print('-3'*10)print("Michign has:",cities[states['Michigan']])print("Florida has :",cities[states['Florida']])#键是states['Michigan'] 值是MI# cities[MI] = 值是'Detroit'print('-4'*10)for state,abbrev inlist(states.items()):print(f"{state} is abbreviatied{abbrev}")#Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。#把字典用for循环迭代到state,abbrev 两个变量中 ,然后输出了他们键和值 键是state 值是abbrevprint('-5'*10)for abbrev,city inlist(cities.items()):print(f"{abbrev} has the city{city}")#Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。print('-6'*10)for state,abbrev inlist(states.items()):print(f"{state}state is abbreviated {abbrev}")print(f"and has city {cities[abbrev]}")#先取出abbrev的值 再以abbrev的值做键找到对应的值print('-7'*10)
state = states.get('texas')ifnot state:print('sorry , no texas.')
city = cities.get('tx','Does Not Exist')print(f'The city for the state "tx" is "{city}')#判断值存不存在
from sys import argv
filename ="111.txt"#读写操作print(f"我们要读写的文件是{filename}")print("你如果不想这样可以CTRL-C (^c).")print("if you do want that , hit return(如果你想要这个 回车).")input("?")print("opening the file.....(打开文件)")
target =open(filename,'r+')print("truncating thr file . goodbye(截断这个文件 再见)")
target.truncate()print("now i'm going to ask you for three lines(现在我要问你三行)")
line1 =input("line1")
line2 =input("line2")
line3 =input("line3")print("i'm going to write these to the file(我去写道文件中).")
target.write(line1+'\n'+line2+'\n'+line3+'\n')print("and finally ,we close it(最后,我们现在把他关闭)")str=open(filename)
target.close()print(str.read())
from sys import argv
from os.path import exists #
from_file ="111.txt"
tofile ="222.txt"print(f"copying from {from_file} to {tofile}")#从 1 复制到2#we could do these two on one line , how?
in_file =open(from_file,encoding="utf-8")
indata = in_file.read()print(f"the input file is{len(indata)}bytes long(输入的文件时in_file文件的长度)")print(f"daes the output file exits? {exists(tofile)}()退出了吗")print("ready , hit return to continue , ctrl-c to abort()点击返回继续")input()
out_file =open(tofile,'w',encoding="utf-8")
out_file.write(indata)print("alright,all done.")
out_file.close()
in_file.close()
方法定义
from sys import argv
scricpt, input_file = argv
defprint_all(f):print(f.read())defrewind(f):
f.seek(0)defprint_a_line(line_count, f):print(line_count,"this is ", f.readline())
current_file =open(input_file)print("First let's rewind , kind of like a tape:倒退回去\n")
print_all(current_file)print("now let's rewind ,kind of like a tape ")
rewind(current_file)print("let's print three lines:")
curren_line =1
print_a_line(curren_line, current_file)
curren_line = curren_line +1
print_a_line(curren_line, current_file)
curren_line = curren_line +1
print_a_line(curren_line, current_file)
defformaula(start_num):
one = start_num *500
two = one/1000
three = two /100return one ,two ,three
start_num1 =10000
first , second , third = formaula(start_num1)print("whith a staring point of{}".format(start_num1))print(f"we'd have {first} ,{second},{third}")
start_num1 = start_num1/10
formaula = formaula(start_num1)print("{},{},{}".format(*formaula))