2.学习python的第二天

列表
定义:再[ ]内,可以存放多个任意类型的值,并以逗号隔开。一般用于存放学生爱好,课堂的周期等等。
#定义一个学生列表,可存放多个学生
student = ['name1','name2','name3','name4']
print(student[1])  #name2
student_info = ['name1',28,'male',['chifan','heshui']]
#取全部爱好
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= ['杨波',14,'male',['吃饭','喝酒']]
#在student_info列表末尾追加一个值
student_info.append('合肥')
print(student_info)
#6.删除
删除列表中索引为3 的值
del student_info[3]
print(student_info)
需要掌握的:
student_ info = [' name2’,95,’female', [’尬舞’,’喊麦’],95]
1. index获取列表中某个值的索引
print (student_ info. index(95)) # 1
2. count获取列表中某个值的数量
print (student_ info. count (95)) # 2
3.取值,默认取列表中最后一个值,类似删除,若pop()括号中写了索引,则取索引对应的值
student_ info. pop()pr int (student_ info)
取出列表中索引为2的值,并赋值给sex变量名
sex = student_ info. pop(2)
print(sex)
print (student_ info)
4.移除,把列表中的某个值的第一个值移除
student_ info. remove(95)
print(student_ info) # [' name2',' female', ["尬舞’,' 喊麦'],95]
name = student_ info. remove('name2’)
print (name)  #None
print(student_ info)    # [' female', [尬舞’,'喊麦', 95]
5.插入值
student_info=['name2',20.'female',['tiaowu','hanmai'],20]
#在student_info中,索引为3的位置插入‘合肥学院’
student_info.insert(3,'合肥学院')
print(stduent_info)
6.extend 合并列表
student_info1=['name2',20,'female',['tiaowu','hanmai'],20]
student_info2=['name3',22,'male',['chifan','shuijiao']]
#把student_info2所有值插入到student_info1中
student_info1.extend(student_info2)
print(student_info1)
元组:
定义:
在()内,可以存放多个任意类型的值,并以逗号隔开。注意:
元组与列表不一-样的是,只能在定义时初始化值,不能对其进行修改。优点:
在内存中 占用的资源比列表要小。
不可变类型:变量的值修改后,内存地址一定不一样
数字类型:int float  字符串类型:str  元组类型:tuple
可变类型:
列表类型:list  字典类型:dict
字典类型:
作用:在{}内,以逗号隔开可存放多个值,以key-value存取,取值速度快
定义:key碧玺是不可变类型,value可以是任意类型
1.按key存取值:可存可取
#存一个level:9的值到dict1字典中
dict1['level'] = 9
print(dict1)  #{'age':18,'name':'xiao','level':9}
print(dict1['name'])   #xiao
2.长度len
3.成员运算in和not in(只判断字典中的key)
print('name' in dict1)  #True
print('xiao' in dict1)  #False
print('xiao' not in dict1)  #True
4.删除
del dict1['level']
print(dict1)    #{'age':18,'name':'xiao'}
5.键keys(),值values(),键值对items()
#得到字典中所有key
print(dict1.keys())
#得到字典中所有值values
print(dict1.values())
#得到字典中所有iyems
print(dict1.items())
6.循环
#循环遍历字典中所有key
for key in dict1:
    print(key)
    print(dict1[key])
dict1 = {'age':18,'name':'xiao'}
print(dict1.get('sex'))
#若找不到sex,为其设置一个默认值
print(dict1.get('age'))
if 判断
语法:
if 判断条件:
若条件成立,则执行此处代码
逻辑代码
elif 判断条件:
若条件成立,则执行此处代码
逻辑代码
else:
若以上判断都不成立,则执行此处代码
逻辑代码
#判断两数大小
x=10
y=20
z=30
if x > y:
    print(x)
elif z > y:
    print(z)
else:
    print(y)
#while 循环
语法:
while 条件判断:
    #成立执行此处
    逻辑代码
break 跳出本次循环
continue 结束本次循环,进入下一次循环
while True:
    name = input('请输入猜测的字符:').strip()
    if name == 'xiao':
        print('xiao success!')
        break
    print('请重新输入!')
#限制循环次数
str = 'xiao'
#初始值
num = 0
#while循环
while num < 3:
    name = input('请输入猜测的字符:').strip()
    if name == 'xiao':
        print('xiao success!')
        break
    print('请重新输入!')
    num += 1
执行python文件的过程:
1.先启动python解释器,加载到内存中。
2.把写好的python文件加载到解释器中。
3.检测python语法,执行代码。
注意:必须指定字符编码,以什么方式写,就得以什么方式打开。如:utf-8
追加写文本文件
a = open('file.txt','a',encoding='utf-8')
a.write('\n合肥学院')
a.close
文件处理之上下文管理:
#with可以管理open打开的文件,会在with执行完毕后自动调用close()关闭文件
with open()
with open() as f "句柄"
#写
with open('file1.txt','w',encoding='uft-8') as f:
f.write('墨菲定理')
#读
with open('file1.txt','r',encoding='uft-8') as f:
res = f.read()
print(res)
#追加
with open('file1.txt','a',encoding='uft-8') as f:
f.write('围城')
#f.close()
with 管理多个文件
#通过with来管理open打开的两个文件句柄f_r,f_w
with open('name.jpg')as f_r,open('name_copy.jpg',''wb)as f_w:
#通过f_r句柄把图片的二进制流读取出来
res = f_r.read()
#通过f_w句柄把图片的二进制流写入name_copy.jpg文件中
f_w.write(res)
函数
什么是函数?
函数指的其实是一把工具
使用函数的好处:
1.解决代码冗余问题
2.使代码的结构更清晰
3.易管理
函数的使用必须遵循:先定义,后调用;
定义函数的三种形式:无参函数(不需要接收外部传入的参数),有参函数(需要接收外部传入的参数),空函数(pass)

1.无参函数
def login():
    user = input('请输入用户名').strip()
    pwd = input('请输入密码').strip()
    if user == 'xiao' and pwd == '123':
        print('login successful!')
    else:
        print('login error!')
#函数的内存地址
print(login)
#函数调用
login()
2.有参函数
username,password 用来接收外部传入的值
def login(username,password):
    user = input('请输入用户名').strip()
    pwd = input('请输入密码').strip()
    if user == username and pwd == password:
        print('login successful!')
    else:
        print('login error!')
#函数调用
#若函数在定义时需要接收参数,调用者必须为其穿传参
login('xiao','123')
3.空函数
#登录功能
def login():
#代表什么都不做
pass
参数的参数:
#在定义阶段:x,y称之为形参。
def func(x,y):
print(x,y)
#在调用阶段:10,100称之为形参
func(10,100)

猜你喜欢

转载自www.cnblogs.com/xunxunmimi2012/p/11086987.html