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','尬舞','喊麦','']
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','尬舞','喊麦','']
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','']
student_info_b=['g',17,'femele','尬舞1','喊麦2','']
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)) #
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()
#参数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()

3、函数

#函数
#1 解决代码冗余的问题
#2 使代码结构更清晰
#3 方便管理
先定义后调用
def 函数名(参数1,参数2,.....):
逻辑代码
返回值(可有可无) 函数定义的三种方式
1 无参函数
2 有餐函数
3 空函数 pass
#
def login(): user=input('请输入用户名').strip()
pwd=input('请输入密码').strip()
if user=="cheng" and pwd=="":
print("login successful")
else:
print("login error")
print(login)
login()#调用 #
def login(username,password):
if username== "cheng" and password == "":
print("login successful")
else:
print("login error")
login('cheng','') # '''
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={'':f1,'':f2} ch =input("请选择功能")
if ch=='':
print(dic1[ch])
dic1[ch]()
elif ch=='':
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()函数,和函数的命名空间的区别。

最新文章

  1. DPA/Ignite由于DNS问题导致连接不上被监控的数据库服务器
  2. JAVA动手动脑异常处理
  3. BZOJ4260 Codechef REBXOR 题解
  4. allegro使用汇总 [转贴]
  5. DirectX 3D 之C#开发
  6. JSOI2007建筑抢修
  7. php 大流量网站访问
  8. iPhone 5s网络钓鱼邮件,和苹果发布会同步亮相
  9. React Native运行原理解析
  10. [Swift]LeetCode907. 子数组的最小值之和 | Sum of Subarray Minimums
  11. mysql修改表结构语句
  12. Java并发——Fork/Join框架与ForkJoinPool
  13. JS脚本-零星片段
  14. python 全栈开发,Day133(玩具与玩具之间的对话,基于jieba gensim pypinyin实现的自然语言处理,打包apk)
  15. Java并发程序设计(十三)锁的性能优化
  16. SUSE Enterprise Server 12 SP3 64 设置防火墙开放8080端口
  17. HttpClient配置SSL绕过https证书
  18. 经典算法 Manacher算法详解
  19. Linux时钟
  20. 树链剖分处理+线段树解决问题 HDU 5029

热门文章

  1. Hadoop Hive概念学习系列之hive里的索引(十三)
  2. bzoj1835: [ZJOI2010]base 基站选址
  3. YTU 2844: 改错题A-看电影
  4. Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)
  5. Masonry remake更新约束
  6. 计算机设计思想 —— 代理(proxy)
  7. hdu5410(完全背包变形)
  8. poj 3281 Dining【最大流】
  9. P4323 [JSOI2016]独特的树叶(树哈希)
  10. 初窥MySQL性能调优