day02-常用数据类型,文件处理,函数

'''
列表:
定义:在【】内可以存放多个任意类型的值,并以逗号隔开。
一般用于存放学生的爱好,课堂的周期等等。。。
'''
##定义一个学生列表,可存放多个学生
#students = ['qianyao','lixaolong','quandan']
#print(students[1]) #lixiaolong

#student_info = ['yangbo',18,'male',['跑步','喝酒']]
#print(student_info[3]) #取所有的爱好
#print(student_info[3][1]) #取第二个爱好

#优先掌握的操作:
# 1、按索引存取值(正向存取+反向存取):即可存也可以取
#print(student_ info[-2]) #杨波
# 2、切片(顾头不顾尾,步长)
#print (student_ info[0:4:2]) # [”杨波’,’male' ]
#3、长度
#print (len(student_ info)) # 4
# 4、成员运算in和not in
#print('杨波’in student_ info) # True
#print('杨波’not in student_ info) # False

#5、追加
#student_info = ['yangbo',84,'male',['paoba','hejiu']]
#student_info.append('hefeixueyuan')

#6、删除
#删除列表中索引为2 的值
del student_info[2]
print(student_info)


#需要掌握的:
student_info = ['尹浩卿',95,'female', ['尬舞','喊麦'],95]
#1.index获取列表中某个值的索引
#print(student_ info. index(95))# 1

#2.count 获取列表中某个值的数量
# print(student_ info. count (95)) # 2

# 3.取值,默认取列表中最后一个值,类似删除
# #若pop()括号中写了索引,则取索引对应的值
student_info.pop()
print(student_info)
#取出列表中索引为2的值,并赋值给sex变量名
sex = student_info.pop(2)
print(sex)
print(student_info)

#4.移除,把列表中的某个值的第一个值移除
student_info.remove(95)
print(student_info) # [' 尹浩卿',' female', ['尬舞','喊麦', 95]
name = student_info.remove('尹浩卿')

print(name) #None
print(student_info) # [' female', ['尬舞','喊麦', 95]

#5.插入值
student_info = ['尹浩卿',95,'female', [' 尬舞','喊mai'],95]
#在student_info中 索引为3的位置插入'合肥学院'
# student_info.sert(3,'合肥学院')
# print(student_info)

#6、entend合并列表
student_info1 = ['尹',95,'female',['尬舞','喊麦'],95]
student_info2 = ['喽',94,'female',['尬舞','喊麦']]
#把student_info2所有的值插入student_info1内
student-info1.extend(student_info2)
print(student_info1)

#7、循坏
for student in student_info:
print(student)


#元组:
#定义:在()内,可以存放多个任意类型的值,并以逗号隔开。
#注意:元组与列表不一-样的是,只能在定义时初始化值,不能对其进行修改。优点:在内存中占用的资源比列表要小。

#定义
tuple1 = (1,2,3,'五','六')
print(tuple1) #(1,2,3,'五','六')
#优先掌握的操作
#1、按索引取值(正向取+反向取):只能取
print(tuple1[2]) #3

# 2、切片(顾头不顾尾,步长)
# #从0开始切片到5-1,步长为3
print(tuple1[0.5:31) #(1,'五')

#3、长度
print(1en(tuple1)) #5

# 4、成员运算in和not in
print(1 in tuple1) # True
print(1 not in tuple1) # False

#5、循环
for line in tuple1
#print(line)
#print默认end参数是\n
print(line,end='_')


'''
不可变类型:
数字类型 int float
字符串类型 str
可变类型:
列表类型 list
字典类型 dict
'''

#不可变类型
#int
number=100
print(id(number)) #
number=111
print(id(number)) #

#float
sal=1.0
print(id(sal)) #
sal=1.0
print(id(sal)) #

str1='hello python!'
print(id(str1))
str2=str1.replace('hello','python')
print(id(str2))

#可变类型
#列表
list1=[1,2,3]

list2=list1

list1.append(4)
print(id(list1))

#list1和list2指向的是同一份内存地址
print(id(list1))
print(id(list2))
print(list))
print(list2)


'''
字典类型:
作用:
在{}内,以逗号隔开可存放多个值 以key-value存取,取值速度快
定义:
key必须是不可变类型,value可以是任意类型
'''

# dict1 = dict({'age': 18, 'name':' tank'})
# dict1 = {' age': 18,'name':'tank' }
print(dict1) # { 'age': 18, name' tank'}
print(type (dict1)) # <class 'dict'>
#取值,字典名+ [],括号内写值对应的key
print(dict1['age'])

#优先掌握的操作
#1、按key存取值:可存可取
#存一个level:9的值到ditc字典中
dict1['level']=9
print(dict1) #{'age':18,'name':'tank','level':9}
print(duct1['name']) #tank

#2、长度len

#3、成员运算in和not in 只判断自带中的key
print('name' in dict1) #true
print('tank' in dict1) #false
print('tank' not in dict1) #true

#4、删除
del dict['level']
print(dict) #{'age':18,'name':'tank'}

#5、键key(),值values(),键值对items()
#得到字典中所有的key
print(dict.key())
#得到字典中所有值values
print(duct1.values())
#得到字典中所有items
print(dict1.items())

#6、循坏
#循环遍历字典中所有的key
for key in dict1:
print(key)
print(dict1[key])

#get
dict1 = {'age':18,'name':'tank'}
#print(dict1.get('age'))

#[]取值
print(dict1['sex']) #keyEorro:'sex'

#取值
print(dict1.get('sex')) #None
#若找不到sex。为其设置一个默认值

print(dict1.get('sex','male')

 

 

'''
if判断:
语法:
if判断条件:
#若条件成立 则执行此处代码
逻辑代码
elif判断条件:
#若成立,则执行此处代码
逻辑代码
else:
#若以上判断都不成立,则执行此处代码
逻辑代码
'''

#判断大小
x=10
y=20
z=30
#缩进快捷键 tab往右移动四个空格 shitf+tab往左移动四个空格
if x>y :
print(x)
elif z>y:
print(z)
else:
print(y)


'''
while循环:
语法:
while 条件判断:
# 成立执行此处
逻辑代码
break # 跳出本层循环
continue # 结束本次循环 进入下一循环
'''
str1='tank'
#while循环
while True:
name =input('输入猜测的字符:').strip()
if name=='tank'
print('tank success')
break
print('请重新输入')

#限制循环次数
str1='tank'
#初始值
num=0
#while循环
while num<3:
name =input('输入猜测的字符:').strip()
if name=='tank'
print('tank success')
break
print('请重新输入')
num +=1



'''
文件处理:
open()
读文件:rt 读文本
写文件:wt 写文本
追加文件:at 追加文本
注意:必须指定字符编码,以什么方式写,就得以什么方式打开
执行Python代码的过程:1、先启动Python解释器
2、把写好的Python文件加载到解释器中
3、检测Python语法,执行代码
SyntaxError:语法错误
打开文件会产生两种资源:
1、Python程序
2、操作系统打开文件

文件处理之上下文处理:
# with可以管理open打开的文件,会在with执行完毕后自动调用close()关闭文件
with open()

'''

#参数一:文件的绝对路径
#参数二:操作文件的模式
#参数三:encoding指定的字符编码
f=open('file,txt',mode='wt',encoding='utf-8')
f.write('tank')
f.close() #关闭操作系统文件资源

#读文件 r=rt
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()

'''
上下文管理
with open() as f "句柄"
'''
#读
with open('file.txt','r',encoding='utf-8') as f:
res =f.read()
print(res)

#写
with open('file.txt','w',encoding='utf-8') as f:
f.write(钱钟书)

#追加
with open('file.txt','a',encoding='utf-8')as f:
f.write(围城)
f.close()

'''
对图片、视频、音频,进行读写'
'''
#读取相片cxk.jpg
with open('cxk.jpg','rb') as f:
res = f.read()
print(res)
jpg = res
#把cxk.jpg的二进制流写入cxk_copy.jpg文件中
with open('cx;.jpg','wb') as f_w:
f_w.write(jpg)

'''
with管理多个文件
'''
#通过with来管理open打开的两个文件句柄f_r,f_w
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)



'''
什么是函数?
函数指的其实是一把工具
函数有什么好处?
1、解决代码冗余问题
2、是代码的结构更清晰
3、易管理
函数的使用必须遵循:先定义后调用
函数定义语法:def 函数名(参数1,参数2....):
注释:声明函数
逻辑代码
return 返回值
def:define 定义
函数名:必须看其名知其意
():接受外部传入的参数
注释:用来声明函数的作用
return: 返回给调用者的值
'''

'''
定义函数的三种形式:
1、无参函数:不需要接受外部传入的参数
2、有参函数:需要接受外部传入的参数
3、空函数:pass
'''
#1、无参函数
def login():
user = input('请输入用户名'),strip()
pwd = input('请输入密码'),strip()

if user == 'tank' and pwd == '123':
print('login success')
else:
print('login error!')
#函数的内存地址
print(login)
#函数调用
login()

#2、有参函数
#username,password用来接收外部传入的值
def login(username,password):
user = input('请输入用户名'),strip()
pwd = input('请输入密码'),strip()

if user == 'tank' and pwd == '123':
print('login success')
else:
print('login error!')
#函数调用
#若函数在定义时需要接受参数,调用者必须为其传参
login('tank','123')

#空函数
'''
ATM:
1、登录
2、注册
3、提现
4、取款
5、转账
6、还款
'''

#登录功能
#def login():
#代表什么都不做
#pass

#注册功能
#def login():
#代表什么都不做
# pass
#。。。

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

'''
位置参数
'''
#在定义阶段:位置形参xy
def func(x,y):
print(x,y)
#在调用阶段:位置实参
func(10,100)

'''
关键字参数:关键字实参 按照关键字传参
'''
#位置形参x,y
def func(x,y):
print(x,y)
#在调用阶段:x=10,y=100称为关键字实参
func(y=111,x=10) # 10 111
#不能少传
func(y=111) #TypeError
#不能多传
func(y=111,x=10,z=100) #TypeError

'''
默认参数:在定义阶段为参数设置默认值
'''
def foo(x=10,y=20):
print(x,y)
#不传参 则使用默认参数
foo()
#传参 则使用传入参数
foo(200,300)



'''
函数的嵌套定义:在函数内部定义函数
函数对象:函数的内存地址
函数的名称空间:
全局:所有顶着头写的变量、函数...都称之为全局名称空间
局部:在函数内部定义的,都称之为局部名称空间
内置:python解释器自带的都称为内置名称空间
名称空间加载顺序:内置--全局-局部
查找顺序:局部-全局-内置
'''
#函数的嵌套定义
def func1():
print('from func1...')
def func2():
print('from func2...')

#函数对象
print(func1)
def f1():
pass
def f2()
pass
dic1 = {'1':f1,'2':f2}
choice = inout('请选择的编号')
if choice == '1':
print(dic1[choice])
dic1[choice]
elif choice == '2':
printf(dic1[choice])
dic1[choice]

#名称空间
#嵌套定义
x=10
def func1():
#x=20
print('from func1...')
print(x)
def func2():
print('from func2...')
func1()




转载于:https://www.cnblogs.com/123456wyf/p/11086917.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值