python学习(re0)day 2

2019/6/25

一、前言

今天是学习python的第二天.

二、学习内容

1、常用数据类型及内置方法

1.列表(list)

定义:在中括号[]内存放任意多个值,用逗号隔开.

具体函数和内置方法如下:

#定义学生列表,可存放多个学生
students=['a','b','c','d']
print(students[1])
student_info=['e',18,'mele',['喝酒','泡吧']]
print(student_info[3])
print(student_info[3][1])

#2 切片(顾头不顾尾,步长)
print(student_info[0:4:2])
#3 长度
print(len(student_info))
#4 成员运算
print('e' in student_info)
print('e' not in student_info)
#5 追加
student_info=['e',18,'mele',['喝酒','泡吧']]
student_info.append('合肥学院')
#6 删除
del student_info[2]
print(student_info)
#7 index获取某个值的索引
student_info_1=['h',17,'femele','尬舞','喊麦','17']
print(student_info_1.index(17))
#8 获取某个值的数量
print(student_info_1.count(17))
#9 pop取值 默认取列表中最后一个值 有索引就取索引的值
student_info_1.pop()
print(student_info_1)
#
sex=student_info_1.pop(2)
print(sex)
print(student_info_1)

#10 remove
student_info_1=['h',17,'femele','尬舞','喊麦','17']
student_info_1.remove(17)#从左到右删除第一个遇到的值
print(student_info_1)
name=student_info_1.remove('h')
print(name)
print(student_info_1)
#11 insert 插入
student_info_1.insert(3,'合肥学院')
print(student_info_1)
#12 extend 合并列表
student_info_a=['h',17,'femele','尬舞1','喊麦2','17']
student_info_b=['g',17,'femele','尬舞1','喊麦2','17']
student_info_a.extend(student_info_b)
print(student_info_a)
#13 循环
for student in student_info_1:
    print(student)

2.元组(tuple)

定义:在中括号()内存放任意多个值,用逗号隔开.

注意:元组与列表不一样的是,只能在定义的时候初始化,不能对其进行修改.

优点:在内存中占用的资源比列表要小.

#tuple元组
tuple1=(1,2,3,'','five')
print(tuple1)
#1 按索引取值
print(tuple1[2])#取第三个值
#2 切片(顾头不顾尾,步长)
print(tuple1[0:5:2])
#3 长度
print(len(tuple1))
#4 成员运算
print(1 in tuple1)
print(1 not in tuple1)
#5 循环
for line in tuple1:
    print(line)
    print(line,end='')

3.可变和不可变类型

''''
#不可变类型
数字类型
int
float
字符串类型
str
元组
tuple
#可变类型
列表list
字典dict
集合可变和不可变都有'''
number=100
print(id(number))
number=111
print(id(number))
print()

sal=100.1
print(id(sal))
sal=111.1
print(id(sal))
print()

str1='hello python'
print(id(str1))
str2=str1.replace('hello','like')
print(id(str2))
#可变类型

#list1与list2指向同一个内存地址
list1=[1,2,3]
list2=list1
list1.append(4)
print(id(list1))
print(id(list2))

 4.字典(dict)

定义:在{}内,可存放多个值,以key-value存取,取值速度快,key是不可变,value可变

#字典dict
dict1=({'age':18,'name':'peke'})
print(dict1)
print(type(dict1))
#取值 字典名+[] 括号内是对应的值
print(dict1['age'])
#2 存储一个level:9到字典中
dict1['level']=9
print(dict1)

#3 len
print(len(dict1))

#4
print('name'in dict1)#值判断key
print('peke'not in dict1)

#5 删除
del dict1['level']
print(dict1)

#6 key value items
print(dict1.keys())
print(dict1.values())
print(dict1.items())

#7 循环
for key in dict1:
    print(key)
    print(dict1[key])

#8 get
print(dict1.get('age'))
''''''
#print(dict1['sex'])#KeyError: 'sex' cant find
dict1=({'age':18,'name':'peke'})
print(dict1.get('sex'))

dict2=(dict1.get('sex','mele'))
print(dict2)

2、文件处理

#文件处理
'''
写文件
wt
读文件
rt
追加写文件
at
#指定字符编码 以什么方式写就得以什么方式打开
执行代码的过程
1 先启动python解释器
2 把写好的python文件加载到解释器中
3 检测python的语法 执行代码
'''
#参数1 文件的绝对路径
#参数2 操作文件的模式
f=open('file.txt',mode='wt',encoding='utf-8')
f.write('tank')
f.close()#关闭操作系统文件资源

#2 读文本文件
f=open('file.txt','r',encoding='utf-8')
print(f.read())
f.close()

#3 追加
f=open('file.txt','a',encoding='utf-8')
f.write('\n 合肥学院')
f.close()

上下文处理(利用with处理)

# 上下文处理
#with 可以管理open打开的文件 会在with执行完后自动调用close() 关闭文件
# with open() as f 句柄
with open('file.txt',mode='wt',encoding='utf-8') as f1:
   f.write('墨菲定律')

with open('file.txt',mode='r',encoding='utf-8') as f2:
   print(f.read())

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

#with 管理图片
with open('cxk.jpg','rb') as f4:
   res=f.read()
   print(res)
jpg=res

with open('cxk.jpg','wb') as f5:
   f.write(jpg)


#with管理多个文件
with open('cxk.jpg','rb') as f4,open('cxk.jpg','wb') as f5:
   res=f4.read()

   f5.write(res)

 

3、函数 

#函数
#1 解决代码冗余的问题
#2 使代码结构更清晰
#3 方便管理
先定义后调用
def 函数名(参数1,参数2,.....):
逻辑代码
返回值(可有可无)

函数定义的三种方式
1 无参函数
2 有餐函数
3 空函数 pass
#1
def login():

     user=input('请输入用户名').strip()
     pwd=input('请输入密码').strip()
     if user=="cheng" and pwd=="123":
         print("login successful")
     else:
        print("login error")
print(login)
login()#调用


#2
def login(username,password):
    if username== "cheng" and password == "123":
        print("login successful")
    else:
        print("login error")
login('cheng','123')

#3

'''
ATM
1:提现
2:...
...
....
..
..

'''
def register():
    pass

#在定义阶段x,y为形参

#在调用阶段x,y为实参

 

#关键数参数
def func(x,y):
    print(x,y)
func(x=100,y=10)
#传参数的时候不能多传也不能少传


#默认参数
'''
在定义阶段,为参数设置默认值


'''
def foo(x=10,y=10):
    print(x,y)

foo()
foo(11,22)


'''
    函数的嵌套定义
    函数的对象
    函数的名称空间
    
    在python中顶格写的全部称为全局名称空间
    在函数内部定义的为局部名称空间
    python解释器自带的都称为内置名称空间
    加载顺序
    内置—>全局—>局部
    查找顺序
    局部->全局—>内置
    
'''
# 1 在函数内部定义函数
def func1():
    print('from func1')
    def func2():
        print('form func2')
# 2 函数对象
def f1():
    pass
def f2():
    pass
dic1={'1':f1,'2':f2}

ch =input("请选择功能")
if ch=='1':
    print(dic1[ch])
    dic1[ch]()
elif ch=='2':
    print(dic1[ch])
    dic1[ch]()

# 3 函数的名称空间
x=10
def func1():
    print('from func1...')
    x=20
    print(x)
    def func2():
        print('form func2...')

三、总结

今天是python从零开始学习的第二天(day2),昨天把python的数据类型中的字符串学了,今天则把列表,字典,元组数据类型进行学习,然后又复习了文件操作和函数基本操作,其他有诸如with来处理上下文,省略了文件操作中的close()函数,和函数的命名空间的区别.

 

posted @ 2019-06-25 19:10  青藤门下走狗  阅读(171)  评论(0编辑  收藏  举报