美文网首页
Python学习-基础1

Python学习-基础1

作者: 幽谷听泉 | 来源:发表于2016-10-10 16:16 被阅读0次
    • python 解释性的脚本语言:直接执行,不需要编译
    • C 编译型语言:先编译,再执行
    python特性:
    • 内部机制:

    1.python先将脚本编译成字节码(pyc,pyo)
    2.python虚拟机解释并运行字节码文件
    3.运行产生并输出结果

    • Python语言特点:明了 简单 优美

    • Python格式
      #coding:utf-8
      #写在python文件的首行,定义文件的编码格式

    • 当一个文件import另外一个文件时,执行,则被引用的那个文件会生成一个pyc字节码文件

    • 反斜杠\ 可以将一段超长的代码分成几行

    • 在python领域,一切数据都是对象

    • 一切变量都是对数据对象的引用

    #######变量命名规范

    • 只能用字母和_开头

    • 不能用关键字命名:比如if while and pass import for等等

    • 建议:变量名 函数名小写,采用下划线或者驼峰法命名,

    • 多重赋值
      > a,b,c = 1,2,3

    • 变量删除(也可多重删除del(a,b,c))
      > del(a)

    • 记住三个内置函数:type,help,dir

    type 确定变量的数据类型
    help 获取帮助文档
    dir 获取模块含有那些方法

    • 用内置函数去学习str对象
    python数据类型

    常用的数据类型:整型(int)、布尔(boolean)、字符串(str)、列表(list)、元组(tuple)、字典(dict)

    字符串
    • 字符串的子串"abc"是"123abcd"的子串

    • python 默认文件编码是ascii

    • 转义字符反斜杠(\)

    • 字母r输出转义字符组合
      >>> print("abc"")
      abc"
      >>> print(r"abc"")
      abc"

    • python下标以0开始

    • Python 占位符
      >>> print("My name is %s,and I am %d years old" %("LILei",18))
      My name is LiLei,and I am 18 years old
      >>> "This is %(whose)s %(fruit)s" % {'whose':'my','fruit':'apple'}
      'This is my apple'

    • 优秀的字符串拼接方案:

      • join
        >>> name = "LiLei"
        >>> age = "28"
        >>> job = "teacher"
        >>> ",".join([name,age,job])
        'LiLei,28,teacher'
    • 读写文本(open write close seek read readline)

      • 写入

        file = open('reg.txt','w')
        file.write('aaaaaaaaaaaaaaaaaaa\n')

        20
        >>> file.write('123456789\n')
        10
        >>> file.write('abcdefghigklmn\n')
        15
        >>> file.write('zxcvbnm\n')
        8
        >>> file.close()
        >>> 
        
    Paste_Image.png

    * 读取

          >>> file = open('reg.txt','r')
          >>> file.read()
          'aaaaaaaaaaaaaaaaaaa\n123456789\nabcdefghigklmn\nzxcvbnm\n'
          >>> file.seek(0)
          0
          >>> file.read(10)
          'aaaaaaaaaa'
          >>> file.read(10)
          ' aaaaaaaaa\n'
          >>> file.read(10)
          '123456789\n'
          >>> file.read(10)
          'abcdefghig'
          >>> file.seek(0)
          0
          >>> file.readline()
          'aaaaaaaaaaaaaaaaaaa\n'
          >>> file.readline()
          '123456789\n'
          >>> file.readline()
          'abcdefghigklmn\n'
          >>> file.readline()
          'zxcvbnm\n'
          >>> file.readline()
          ''
          >>> file.close()
          >>> 
    
    • 不可变数据类型:string int tuple

    • 可变数据类型:list dict

    • 修改字符串:replace
      >>> "caideyang".replace('a','A')
      'cAideyAng'
      >>>

    • 查询字符串位置:find
      >>> "this is the world".find("world")
      12
      >>> "this is the world"[12]
      'w'
      >>> "this is the world"[12:]
      'world'
      >>>

    • format占位符
      >>> "this is {1} {0}".format('apple','banana')
      'this is banana apple'
      >>> "this is {name} and age is {age}".format(name='caideyang',age=16)
      'this is caideyang and age is 16'

    • linecache模块:

      • 按行获取文本内容getline
        >>> linecache.getline('reg.txt',1)
        'aaaaaaaaaaaaaaaaaaa\n'
        >>> linecache.getline('reg.txt',2)
        '123456789\n'
        >>> linecache.getline('reg.txt',3)
        'abcdefghigklmn\n'
      • 将文本内容放入list表里
        >>> lines = linecache.getlines('reg.txt')
        >>> print(lines)
        ['aaaaaaaaaaaaaaaaaaa\n', '123456789\n', 'abcdefghigklmn\n', 'zxcvbnm\n']
    • split分割方法
      >>> s.split(',')
      ['i', 'am', 'lilei']
      >>> s.split(',')[1]
      'am'
      >>>

    • 关于列表
      >>> a = []
      >>> id
      <built-in function id>
      >>> id(a)
      140153319788616
      >>> a.append('I')
      >>> a
      ['I']
      >>> id(a)
      140153319788616
      >>> d = a
      >>> id(d)
      140153319788616
      >>> d
      ['I']
      >>> d.append('am')
      >>> d
      ['I', 'am']
      >>> a
      ['I', 'am']
      >>> del a[:] #清空列表对象里的元素
      >>> a
      []
      >>> del a #删除列表对象的引用
      >>> a
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      NameError: name 'a' is not defined
      >>>
      ####### 元组 tuple(不可变对象,没有修改排序等操作)

    ####### 集合:

    • 集合是没有顺序的概念,不能用切片和索引操作
    • 创建集合:set():可变集合,frozenset():不可变集合
    • 添加元素:add ,update
    • 删除元素:remove
    • 并集:| ;交集:&;差集:-
      >>> b = set('abcd')
      >>> a = set('bcdefg')
      >>> a
      {'f', 'd', 'e', 'g', 'c', 'b'}
      >>> b
      {'d', 'a', 'b', 'c'}
      >>> a | b
      {'a', 'b', 'f', 'd', 'e', 'g', 'c'}
      >>> a - b
      {'e', 'f', 'g'}
      >>> a & b
      {'d', 'b', 'c'}
      >>>
    • set可去列表里的重复元素
      >>> a
      ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'c', 'd', 'e', 'g', 'f', 'f', 'd', 'd', 'd', 'a']
      >>> a = list(set(a))
      >>> a
      ['f', 'd', 'e', 'g', 'c', 'b', 'a']
      >>>
    字典
    • 无序的,不能通过索引来读取,只能通过键来读取内容
    • 添加内容 a['name'] = 'caideyang'
    • 修改内容a['name'] = 'caideyang'
    • update修改添加,参数是一个字典类型,会覆盖相同键的值
    • 删除 del ,clear,pop
      >>> b
      {}
      >>> b['name']='caideyang'
      >>> b['age']=18
      >>> b
      {'age': 18, 'name': 'caideyang'}
      >>> b.update({'name':'chengwanwan','job':'CEO'})
      >>> b
      {'job': 'CEO', 'age': 18, 'name': 'chengwanwan'}
      >>> del b['job'] #删除字典里job元素的值
      >>> b
      {'age': 18, 'name': 'chengwanwan'}
      >>> b.pop('name') #传入的参数为键名,结果返回对应的值,并在字典中删除该元素
      'chengwanwan'
      >>> b
      {'age': 18}
      >>> b.clear() #清空字典
      >>> b
      {}
      >>>
    • in 成员关系操作,返回True或者False
      >>> b
      {'job': 'CEO', 'name': 'chengwanwan'}
      >>> 'job' in b
      True
      >>> 'Job' in b
      False
    • keys()返回字典里所有的键的列表
    • values()返回字典里所有的值的列表
    • itmes() 生成一个字典的容器[()]
    • get() 获取字典里键对应的值,如果没有对应的键,则不报错,可设置返回默认值
      >>> b.keys()
      dict_keys(['job', 'name'])
      >>> b.values()
      dict_values(['CEO', 'chengwanwan'])
      >>> b.items()
      dict_items([('job', 'CEO'), ('name', 'chengwanwan')])
      >>> b.get('name')
      'chengwanwan'
      >>> b.get('age') #获取age对应的值,不存在,则不显示结果
      >>> type(b.get('age'))
      <class 'NoneType'>
      >>> b.get('age',0)
      0
      >>>

    相关文章

      网友评论

          本文标题:Python学习-基础1

          本文链接:https://www.haomeiwen.com/subject/pyeyyttx.html