博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python入门二
阅读量:4356 次
发布时间:2019-06-07

本文共 2950 字,大约阅读时间需要 9 分钟。

1.列表实现:

students=['gjw','chj'] print(students[1])student_info=['gjw','18','男',['打球','喝酒']] print(student_info[1]) print(student_info[3][1]) print(student_info[0:4:2])末尾追加值 student_info.append('hfuu') print(student_info)删除值 del student_info[1] print(student_info)

  2.index获取列表索引值:

index获取列表中的索引值 student_info=['chj',22,'female',['唱','跳','篮球']] print(student_info.index(22)) print(student_info.count(22)) student_info.pop() print(student_info)取出列表中索引为2的值 sex=student_info.pop(2) print(sex) print(student_info)

  3.移除列表中某个值:

# student_info=['chj',22,'female',['唱','跳','篮球']]# print(student_info.index(22))# print(student_info.count(22))

  4.合并列表

student1_info=['gjw','18','男',['打球','喝酒']]student1_info.extend(student_info) print(student1_info)

  5.元组的各种:

tuple1=(1,2,3,4,'五') print(tuple1) print(tuple1[0:5:2])字典类型:key为不可变类型,value为任意类型 dict=({'age':18,'name':'gjw'}) print(dict) print(type(dict))按key存取值 dict['level']=9 print(dict) print(dict.keys()) print(dict.values())循环遍历所有的key值 for key in dict:     print(key)     print(dict[key])get取值 print(dict.get('sex','male')) print(dict.get('sex'))

  6.函数各种:

#if..else 循环,while循环 str1='gjw' while True:     name=input('请输入猜测的字符:').strip()     if name=='gjw':         print('gjw sucess')         breakprint('请重新输入:')

  7.执行Python文件等:

'''执行python的文件过程:    1:启动python解释器,加载到内存中    2:把写好的python文件加载到解释器中    3:检测python语法,执行代码打开文件会产生两种资源:    1:python程序    2:操作系统打开文件'''#参数一:文件的绝对路径,参数二:操作文件的模式wt:写文本,rt:读文本,at:追加文本 f=open('file.text',mode='wt',encoding='utf-8') f.write('gjw') f.close()#关闭操作系统文件资源 f = open('file.text', mode='r', encoding='utf-8') print(f.read()) f.close() #追加写文本文件 a=open('file.text','a',encoding='utf-8') a.write('\n hfuu') a.close()#写 with open('file.txt','w',encoding='utf-8') as f:     f.write('fuck you') #读 with open('file.txt', 'r', encoding='utf-8') as f:     res=f.read()     print(res)#追加 with open('file.txt', 'a', encoding='utf-8') as f:     f.write('mother fuck')rb模式读取二进制不需要指定字符编码读取照片cxk.jpg with open('cxk.jpg','rb') as f:     res=f.read()     print(res)jpg=res #把cxk。jpg二进制流写入cxl_copy.jpg文件中 with open('cxk_copy.jpg','wb')as f_w:     f_w.write(jpg)

  8.有参、无参函数:

# '''# 1、无参函数:不需要接受外部传入的参数# 2、需要外部接受传入的参数# 3、空函数# ''' def login(username,passward):      user=input('请输入用户名:').strip()      pwd=input('请输入密码:').strip()      if user==username and pwd==passward:          print('登录成功!')      else:         print('滚!') login('gjw','123')'''ATM    1.登录    2.注册    3.提现    4、取    5、还    6、转''' #1 def login():     pass #2 def register():     pass #3 def repay():     pass def func(x,y):     print(x,y)func(10,100)#x=10def func1():    x=20    print('from func1...')    print(x)    def func2():        print('from func2...')func1()print(func1)def f1():    passdef f2():    passdic1={
'1':f1(),'2':f2}choice=input('请选择功能编号;')if choice=='1': print(dic1[choice]) dic1[choice]()elif choice=='2': print(dic1[choice]) dic1[choice]()

 

转载于:https://www.cnblogs.com/WeanG/p/11086766.html

你可能感兴趣的文章
必背公式及常数
查看>>
利用CSS、JavaScript及Ajax实现图片预加载的三大方法
查看>>
js时间戳转时间格式
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Linux的用户态和内核态
查看>>
JavaScript原生错误及检测
查看>>
(原创) cocos2d-x 3.0+ lua 学习和工作(4) : 公共函数(3): 深度克隆clone()
查看>>
为什么写作
查看>>
整数子数组求最大和添加验证
查看>>
使用kubeadm安装Kubernetes
查看>>
Principal Component Analysis 主元分析
查看>>
linux分割字符串操作
查看>>
PHP学习2
查看>>
多实例Mysql配置
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>
hdu4348区间更新的主席树+标记永久化
查看>>
ZOJ 2532 Internship
查看>>
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>