美文网首页
Python 入门

Python 入门

作者: Bocchi | 来源:发表于2019-02-26 21:00 被阅读0次

    详细入门请参考Python3教程


    1 起步


    在不同操作系统中搭建 Python 编程环境
    • Linux
      大多数Linux计算机中都默认安装了Python,可以在终端输入PythonPython3查看系统默认安装的Python版本(前者查看Python2版本信息,后者查看Python3版本信息)。
      如果你使用的是图形界面,可以考虑安装geany编辑器。
    • Windows
      强烈推荐使用安装 Pycharm+Anaconda 上手后极为舒畅(Pycharm+Anaconda在Linux上也可以使用)。
    • Pycharm+Anaconda配置
      Linux 下配置方法
      Windows 下配置方法

    2 简单数据类型


    字符串

    字符串就是一系列字符。在Python中,用引号括起来的都是字符串,其中的引号可以是单引号也可以是双引号。这中灵活性可以在字符串中包含引号和撇号。

    • 字符串常用方法
      name.title():首字母大写
      name.upper():所有字母大写
      name.lower():所有字母小写
      str1 + str2:拼接字符串
      name.rstrip():删除字符串末尾空白
      name.lstrip():删除字符串开头空白
      name.strip():删除开头和末尾空白
    数字
    • Python3中 3 / 2 结果为1.5,Python2中结果为1(省略小数)。

    3 列表


    添加和删除元素
    • 添加元素list1.append('element')
    • 插入元素list1.insert(0,'element')
    • 删除元素del list1[0]first = list1.pop(0)list1.remove('value')
    组织列表
    • 永久性排序
      list1.sort():顺序排序
      list1.sort(reverse=True):倒序排序
    • 临时排序list1.sorted()
    • 确定列表长度len(list1)
    数字列表
    • range()函数:生成一系列数字
    • list()函数:将一列数字转换为列表
    简单统计
    • max():求最大数
    • min():求最小数
    • sum():求和
    列表解析:squres = [value**2 for value in range(1,11)]
    切片:取列表的一部分
    复制列表
    • list1 = list2[:]:拷贝列表
    • list1 = list2:关联地址
    元组:不能改变的列表,用()表示。

    4 if、for、while语句


    • if
      if condition:
          do something
      #或
      if condition:
          do something
      else:
          do something
      #或
      if condition:
          do something
      elif condition:
          do something
      #或
      if condition:
          do something
      elif condition:
          do something
      else:
          do something
      
    • for
      for value in list1:
          do something
      #或
      for list1 in list2:
          do something
      
    • while
      while condition
          do something
          condition_value change or break
      

    相关文章

      网友评论

          本文标题:Python 入门

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