设为首页 收藏本站
查看: 1167|回复: 0

[经验分享] python3 笔记

[复制链接]

尚未签到

发表于 2018-8-7 08:30:33 | 显示全部楼层 |阅读模式
  .1# 数的定义:
  #
1.整型:int 如 1
2.长整型 long ,是比较大的整数
3.浮点数float/double 如:1.1
4.复数: a+bj 如1+2j
  格式化:
  format ---字符串拼接
  %d,%f,%s 格式化输出
  str()、int()、float()强转类型函数
  #age=int(input('请输入您的年龄:'))
  #print ( age-1)
  #强转化的函数例子
  #age=int(input('请输入您的年龄:'))
  #print ( '你的周岁是:'+str(age-1))
  #age=input('请输入你的年龄:')
  #print('你的周岁是:'+str(int(age)-1))
  强转类型的函数总结(int、float、str)
  1.如果想要将一个整数和浮点与一个字符串连接,str()函数比较方便
  2.如果有一个些字符串,希望将他们用于数值运算,int()函数比较方便
  3.如果将一个不能求值为整数的值传递给int(),python报错valueErro.
  4.如果需要对浮点数进行取整运算,也可以int()函数。
  #编写一个程序(可以询问姓名和年龄,并且告知姓名的字符个数和你明年的年龄)
print('welcome')
name=str(input('what\'s your name:'))
print('很高兴见到你:'+name)
age=int(input('你的年龄:'))
print ('你的姓名字符个数:'+str(len(name)))
print('你明年的年龄:'+str(age+1))
  #综合数据类型-list
  列表 list 是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。
  常规操作: 访问列表中的值 、更新列表、删除列表元素、列表函数&方法。
  使用方括号定义列表。  type() 查看类型
  >> str(11)
  '11'
  >> float(11)
  11.0
  定义列表:
  >> list1=[1,1,2,3,]
  >> list1
  [1, 1, 2, 3]
  查看类型:
  >> type(list1)
  <class 'list'>
  >>
  访问列表:
  >> len(list1)
  4
  >> print(list1[2])
  2
  >> print(list1[1])
  1
  >> print(list1[0])
  1
  双重列表:
  >> list2=[1.,2.,3.,4.]
  >> list3=[list1,list2]
  >> print(list1)
  [1, 1, 2, 3]
  >> print(list2)
  [1.0, 2.0, 3.0, 4.0]
  >> print(list3[0])
  [1, 1, 2, 3]
  >> print(list3[0] [2])
  2
  >> print(list3[1] [2])
  3.0
  >> print(list3[1] [2])
  3.0
  >> print(list3[1] [3])
  4.0
  >> print(list3)
  [[1, 1, 2, 3], [1.0, 2.0, 3.0, 4.0]]
  >> list3
  [[1, 1, 2, 3], [1.0, 2.0, 3.0, 4.0]]
  更新列表:
  >> list3[0]=[1,2,3,4,5,6,7,8,]
  >> list3
  [[1, 2, 3, 4, 5, 6, 7, 8], [1.0, 2.0, 3.0, 4.0]]
  >> del list3[0]
  删除列表
  >> list3
  [[1.0, 2.0, 3.0, 4.0]]
  >>
  插入数据列表:
  >> list1=[1,2,3,4]
  >> list1.append('11')
  >> list1
  [1, 2, 3, 4, '11']
  统计元素出现的次数
  >> print(list1.count(2))
  1
  追加列表
  >>list1.extend[1,2,3]
  >>list1
  [1,2,3,4,11,1,2,3]
  在定制的索引位置添加元素
  >>list1.insert(4,'22')
  >>list1
  [1,2,3,4,'22',11,1,2,3]
  删除指定坐标的元素
  >> list1.pop(2)
  3
  >> list1
  [1, 2, 4, '22', 11, 1, 2, 3]
  >>
  remove移除列表某个值第一次匹配的项
  >>list1.remove(1)
  >>list1
  [2, 4, '22', 11, 1, 2, 3]
  方向列表
  >> list1.reverse()
  >> list1
  [3, 2, 1, 11, '22', 4, 2]
  排序列表  sort
  >> list1.sort()
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  TypeError: unorderable types: str() < int()
  报错,因为列表有字符串,先删掉字符在排序而python2不会报错这样错的。
  >> list1
  [1, 2, 3, 11, '22', 4, 2]
  指定删除某个元素
  >> list1.pop(4)
  '22'
  >> list1
  [1, 2, 3, 11, 4, 2]
  >> list1.sort()
  >> list1
  [1, 2, 2, 3, 4, 11]
  >>
  切片:
  >> list1=[1,2,2,3,4]
  >> print(list1[2:4])
  [2, 3]
  >> print(list1[1:4])
  [2, 2, 3]
  >> print(list1[-2:-4])
  []
  >> print(list1[-4:-2])
  [2, 2]
  >>
  元祖 tuple
  python的元组合列表类似,不同之处在于元组的元素不能修改。
  常规操作:
  访问元组、修改元组、删除元组 、无关闭分隔符、内置函数
  2.定义一个值得元祖
  >> t2=(9)
  >> t2
  9
  >> type(t2)
  <class 'int'>
  定义元祖
  >> t3=()
  >> type(t3)
  <class 'tuple'>
  >> t1=(1,2,3,4,4)
  >> type(t3)
  <class 'tuple'>
  >> t3
  ()
  >> t1
  (1, 2, 3, 4, 4)
  >>
  访问元组
  >> t1
  (1, 2, 3, 4, 4)
  >> print (t1[1])
  修改元组元素 不能修改(保证数据安全),可以先转化为列表 在修改后在转元组。
  >> egg=('hello',2,3,4,5)
  >> egg[1]=99
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  TypeError: 'tuple' object does not support item assignment
  删除元组
  >> del t1
  >> t1
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  NameError: name 't1' is not defined
  >>
  无关闭分隔符。
  >> type(('hello'))
  <class 'str'>
  >> type(('hello',))
  <class 'tuple'>
  多元素赋值
  >> x,y,z=(1,2,3)
  >> print(x,x,y)
  1,2,3
  例题:
  x=2,y=3  进行互换:
  1.使用变量,进行赋值
  >> x=2
  >> y=3
  >> z=x
  >> x
  2
  >> z
  2
  >> x=y
  >> x
  3
  >> y=z
  >> y
  2
  B.使用
  >> x=2
  >> y=3
  >> x,y=(y,x)
  >> print(x,y)
  3 2
  元组的优势
  1.可以用元组告诉所有读代码的人,你不打算改变这个序列的值
  如果需要一个永远不改变的值的序列,就使用的元组
  2.因为元组的不可变性,内容不会变化的,python可以实现一些优化,让使用元组的代码闭
  使用列表代码更快。
  综合数据类型-dict
  字典-dict
  字典是另一种可变容器模型,且可存储任意类型对象。
  常规操作。
  访问字典里的值
  修改字典
  删除字典元素
  字典键的特性
  字典内置函数&方法
  -花括号定义 键值对形式存在
  >> dict={'a':1,b:2}
  >> type(dict)
  <class 'dict'>
  在字典中,键是唯一的,如果存在两个一样的键,后者会替代前者
  #键不可变得,可以使用数字,字符串,元组,但是列表不行
  访问字典
  >> print(dict2['a'])
  4
  如果访问不存在的键,会报错keyerro.
  >> dict1['c']+dict1['a']+dict1['b']
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  KeyError: 'a'
  --->>在列表中不存在的下标不能进行元素的修改。
  ----》》在字典中可以直接设置一个不存在的键,并且给他一个值
  >> list1[6]=10
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  IndexError: list assignment index out of range
  >> dict1
  {'c': 1, 'b': 2}
  >> dict1['a']=dict1['c']+dict['b']
  >> dict1
  {'c': 1, 'a': 21, 'b': 2}
  删除字典中的元素
  >> del dict2['c']
  >> dict2
  {'a': 4, 'b': 2}
  清空:dict2.clear()
  >> dict2.clear()
  >> dict2
  {}
  字典函数:
  创建一个字典,以序列中的元素作为新字典的键,val作为字典的使用键的初始值。
  dict.fromkeys(seq,val)
  如:
  >> dict3={}
  >> dict4=dict3.fromkeys([1,2,3,4],0)
  >> dict4
  {1: 0, 2: 0, 3: 0, 4: 0}
  >> dict3
  {}
  当访问一个不存在键会报错,
  为了避免返回错误,影响程序正常的运行
  dict.get(key,default=value)
  返回指定键的值,如果键不存在,返回default默认值value
  >> dict5={'a':1,'b':2}
  >> print(dict5.get('b','没有这个值'))
  2
  >> print(dict5.get('c','没有这个值'))
  没有这个值
  >>
  ID    名字   年龄
  1      后裔    24
  2      嫦娥   30
  3      露露
  >> dict1={1:{'ID':'001','名字':'后裔','年龄':24},2:{'ID':'002','名字':'嫦娥','
  年龄':30},3:{'ID':'003','名字':'露露'}}
  >> print(dict1[3].get('年龄',25))
  25
  9.与get类似,区别键不存在,增加键,并赋值。
  dict.setdefault(key,value)
  >> print(dict1.setdefault('年龄',18))  *与dict1 元素并列了*** .
  18
  >> dict1
  {1: {'名字': '后裔', '年龄': 24, 'ID': '001'}, 2: {'名字': '嫦娥', '年龄': 30, '
  ID': '002'}, 3: {'名字': '露露', 'ID': '003'}, '年龄': 18}
  >> del dict1['年龄']
  >> print(dict1[3].setdefault('年龄',18))
  18
  >> dict1
  {1: {'名字': '后裔', '年龄': 24, 'ID': '001'}, 2: {'名字': '嫦娥', '年龄': 30, '
  ID': '002'}, 3: {'名字': '露露', '年龄': 18, 'ID': '003'}}
  >>
  判断字典中是否存在某个键
  >> dict1
  {'a': 0, 'b': 'meiyou'}
  >> print('a' in dict1)
  True
  返回字典中所有的键、所有的值。
  dict.keys()/dict.values()
  >> dict7={0:[1,2,3],1:[2,3,4],2:[4,5,6]}
  >> print(dict7.keys())
  dict_keys([0, 1, 2])
  >> print(dict7.values())
  dict_values([[1, 2, 3], [2, 3, 4], [4, 5, 6]])
  把字典中键/值更新到另一个字典中。
  dict.update()
  >> dict1_1={'a':1,'b':2}
  >> dict1_2={'c':3,'d':4}
  >> dict1_1.update(dict1_2)
  >> dict1_1
  {'a': 1, 'b': 2, 'd': 4, 'c': 3}
  >>
  数据综合类型-set&序列、切片
  A.序列和分片
  str1='my name is leon'
  #1.取得name子串
  >> str1='my name is leon'
  >> print(str1[3:7])
  name
  >> print(str1[-12:-8])
  name
  #2.取得leon子串
  >> print(str1[11:])
  leon
  >> print(str1[-4:])
  leon
  F.集合 set
  集合(set)和字典(dict)类似,它是一组key的集合,但不存储value.
  集合的特性就是:key不能重复,唯一。
  常用操作:
  创建集合
  遍历集合
  添加元素
  交集、并集、差集
  -花括号定义-
  >> s1={'a','b','c'}
  >> type(s1)
  <class 'set'>
  >> s2=set('abc')
  >> s2
  {'a', 'b', 'c'}
  >> type(s2)
  <class 'set'>
  访问 通过循环结构
  >> for e in s2 :print(e)
  ...
  a
  b
  c
  转化列表在访问
  >> list1=list(s2)
  >> print(list1[0])
  a
  增加元素
  >> s3=set('abcder')
  >> s3.add('g')
  >> s3
  {'d', 'r', 'g', 'e', 'b', 'c', 'a'}
  >>
  删除元素
  >> s3.remove('a')
  >> s3
  {'d', 'r', 'g', 'e', 'b', 'c'}
  交集、并集,差集
  交集
  >> set4_1=set('acvsdf')
  >> set4_2=set('aacvskk')
  >> print(set4_1&set4_2)
  {'a', 'v', 's', 'c'}
  >>
  并集:
  >> print(set4_1|set4_2)
  {'d', 'k', 'v', 'c', 'f', 'a', 's'}
  >>
  差集
  >> print(set4_1-set4_2)
  {'d', 'f'}
  >>
  >> print(set4_2-set4_1)
  {'k'}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-547923-1-1.html 上篇帖子: Python 内建函数 下篇帖子: day01:python标识符
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表