day_02 python基础

# 25日上午
'''
列表
定义:在[]内,可以存放多个任意类型的值,并以逗号隔开
''''
students=['sb','2b']
print(students[1])
student_info=['ming',18,'male,['泡吧’,‘喝酒’]]
print(student_info[3][1])

切片(顾头不顾尾,步长)
print(student_info[0,4,2])
长度
print(len(student_info))
追加
student_info.append('打飞机')
删除
del student_info[2]
索引
print(student_info.index(18))
count 获取列表中某个值的数量
print(student_info.count(18))
取值,默认取列表中最后一个值,类似于删除
pop()内有了索引获取列表对应的索引
student_info.pop()
删除remove
student_info.remove[18]#把列表中第一个索引值删掉
插入值insert()
student_info.insert(3,'合肥学院’)
#合并列表
student_info1=['ming',18,'male,['泡吧’,‘喝酒’]]
student_info2=['阿郎',18,'male,['泡吧’,‘打飞机’]]
student_info1.extend(student_info2)

元组:tuple
    定义:在()内,可以存放多个任意类型的值,并以逗号隔开。注意:元组与列表不一样的是,只能在定义时初始化值,不能对其进行修改。
优点:在内存中占用资源较少
tuplel=(1,2,3,'wu')
#1按索引取值
print(tuplel[2])
#2切片
print(tuplel[0:5:3])
#3长度
#4成员运算
#5循环
for line in tuplel:
print(line,end'_')

字典类型:
  作用:在{}内,存多个值,key-value存取,取值速度快
  定义:key必须是不可变类型,value可以是任意类型
不可变类型:
'''
变量的值修改后,内存地址一定不一样
      数字类型 int,float
      字符串类型 str
      元组类型tuple
可变类型:
      列表类型  list
     字典类型 dict
#int
number = 100
print(id(number))
number = 111
#可变类型内存地址一样
列表
list1=[1,2,3]
list2=list1
list1.append(4)
print(id(list1))
print(id(list2))        
print(id(number))#两者id不一样
'''
dict1=dict({'age':18,'name':'ming'})
取值:字典名+[];[]内写值对应的key
print(dict1[age])
#1按key存取值
#存一个level:9的值到dict1字典中
dict1['level']=9
成员运算值判断字典中的key
删除
del dict1['level']
print(dict1.keys())
#循环
for key in dict1
print(key)
print(dict1[key])
#get
print(dict1.get('age'))
#[]取值
print(dict1['sex'])
#get
print(dict1.get('sex','male))

if 判断 
  语法:
 if判断条件:
elif 判断条件:
elsewhile 循环
    语法:
    while 条件判断:
    #成立执行此处代码
    break#跳出本层循环
    continue #进行下一次循环

文件处理:open()写文件wt,读文件rt,追加文件at
注意必须指定字符编码以什么方式写就得以什么方式打开
执行python代码过程,1先启动python解释器,2把写好的python文件加载到内存中,3检测python语法执行代码
打开文件会产生两种资源;
1python程序
2操作系统打开文件
#参数一:文件的绝对路径
#参数二:操作文件的模式
#参数三:encoding指定的字符编码
#写文本
f=open('file.txt',mode=wt,encoding='utf-8')
f.write('ming')
f.close()#关闭操作系统打开的文件
#读文本
f.open('file.txt','r',encoding='utf-8')
print(f.read())
f.close()
#追加
a=open('file.txt',‘a',encoding='utf-8')
a.write('\n 合肥学院')
a.close()


#25日下午
文件管理之上下文管理
#with可以管理open打开的文件,会在with执行完毕后自动调用close()关闭文件 with open()as f“句柄”
with open('file.txt',‘r’,encoding='utf-8')as f:
      res = f.read()
     print(res)

对图片.视频.音频读写
rb模式读取二进制不需要指定字符编码
with open.('cxk.jpg',‘rb’)as f:
res=f.read()
print(res)
 jpg=res
#把cxk.jpg的二进制流写入cx_copy.jpg文件中
with open('cxk_copy.jpg','wb')as f_w:
f_w.write(jpg)

with open.('cxk.jpg',‘rb’)as f_r,open('cxk_copy.jpg','wb')as f_w:
#通过f_r句柄把图片的二进制读取出来
      res = f_r.read()
#通过f_w句柄把图片的二进制流写入cxk_copy.jpg中
      f_w.write(res)
#with可以管理多个文件


四 函数
函数其实是一种工具
使用函数的好处:
1.解决代码冗余问题
2.代码的结构跟清晰
3.易管理
函数必须先定义,后调用
函数定义语法:
def 函数名(参数1,参数2...):
'''注释:声明函数'''
逻辑代码
return 返回值
def:defind 定义。
函数名:必须看起名知其意
():接收外部传入的参数
注释:用来声明函数的作用
return:返回调用者的值

'''
定义的三种形式
1.无参函数
不需要接收外部的参数
def login():
user=input('请输入用户名’).strip()
pwd=input('请输入密码').strip()
if user == 'ming' and pwd == '123':
    print('successful')

  else:
    print('arror')
#函数的内存地址
print(login)

#函数调用
login()

2.有参函数
def login(username,password):
user=input('请输入用户名’).strip()
pwd=input('请输入密码').strip()
if user == username  and pwd ==password:
    print('successful')

  else:
    print('arror')
login('ming','123')
3.空函数
pass

'''
函数的参数:
'''
#在定义阶段x,y称为形式参数
def func(x,y):
      print(x,y)
#在调用阶段:10,100称之为实参
fuc(10,100)

'''
位置参数
'''
在定义阶段:位置形参
def func(x,y):
      print(x,y)
在调用阶段:10,100位置实参
fuc(10,100)
'''
关键字参数:
     关键字实参
#在调用阶段:x=10,y=100称为关键字参数
#不能少传也不能多传
fuc(x=10,y=100'''
默认参数
'''
def foo(x=10,y=20)
      print(x,y)
#不传参数则使用默认参数
foo()
#传参数,使用传入的参数
foo(200,300)
#函数的嵌套定义
函数的名称空间:
内置:
       python解释器自带的都称为”内置名称空间“。
全局:
        所有顶着头写的变量,函数...都称为“全局名称空间”。
局部:
       在函数内部定义的,都称为“局部名称空间”。
名称空间加载顺序:
    内置——全局——局部
名称空间的访问顺序:
                局部——全局——内置
def func1():
       print'from func1...')
       def func2():
              print("from fuc2...')
    
#函数定义
print(func1)
def f1():
pass
def f2():
pass
dic1 = {'1':f1,'2':f2}
if choice =='1':
print(dic1[choice])
dic1[choice]()
elof choice == '2'print(dic1[choice])
dic1[choice]()

猜你喜欢

转载自www.cnblogs.com/lastdanceGD/p/11087255.html
今日推荐