Python编程 :从入门到实践 读书笔记

Python编程 从入门到实践

第一部分 基础知识

  • 第一章 起步

    • 1.1 搭建编程环境

      • 1.1.1 Python2和Python3

        Python2已停止维护,应使用Python3

      • 1.1.2 运行Python代码片段

        print("Hello Python interpreter!")	# python shell 中直接输入
        
      • 1.1.3 Hello World程序

        print("Hello World!);
        
    • 1.2 在不同操作系统中搭建Python编程环境

      编辑器可以使用文件编辑器,在命令行中运行Python文件。

      • 1.2.1 在Linux系统中搭建Python编程环境

        1. 检查Python版本

          python --version
          
        2. 安装文本编辑器

          PyCharm,VS Code, Geany,Sublime Text

        3. 运行Hello World程序

          print("Hello Python world!")
          
        4. 在终端会话中运行Python代码

          print("Hello Python world!")
          
      • 1.2.2 在OS X系统中搭建Python编程环境

      • 1.2.3 在Windows系统中搭建Python编程环境

    • 1.3 解决安装问题

    • 1.4 从终端运行Python程序

      python < Python文件名 >

      • 1.4.1 在Linux和OS X系统中从终端运行Python程序
      • 1.4.2 在Windows系统中从终端运行Python程序
    • 1.5 小结

  • 第二章 变量和简单数据类型

    • 2.1 运行hello_world.py时发生的情况

    • 2.2 变量

      message = "Hello Python world!"
      print(message)
      message = "Hello Python Crash Course world!"
      print(message)
      
      • 2.2.1 变量的命名和使用

        就目前而言,应使用小写的Python变量名。

      • 2.2.2 使用变量时避免命名错误

    • 2.3 字符串

      • 2.3.1 使用方法修改字符串的大小写

        name = "ada love lace"
        print(name.title())	// 将输出Ada Love Lace,title以首字母大写的方式显示每个单词
        print(name.upper())	// 将字符串全部大写
        print(name.lower())	// 将字符串全部小写
        
      • 2.3.2 合并(拼接)字符串

        first_name = "ada"
        last_name = "lovelace"
        full_name = first_name + " " + last_name	// 通过+来合并字符串
        print(full_name)
        message = "Hello, " + full_name.title() + "!"
        print(message)
        
      • 2.3.3 使用制表符或换行符来添加空白

        \t,\n之类

        print("Python")
        print("\tPython")
        print("Languages:\nPython\nC\nJavaScript")
        print("Languages:\n\tPython\n\tC\n\tJavaScript")
        
      • 2.3.4 删除空白

        favorite_language = ' python '
        print(favorite_language.lstrip()) # 删除字符串开头空白
        print(favorite_language.rstrip()) # 删除字符串尾部空白
        print(favorite_language.strip()) # 同时剔除字符串两端的空白
        
      • 2.3.5 使用字符串时避免语法错误

        message = "One of Python's strengths is its diverse community." # 正确使用单引号和多引号
        print(message)
        
      • 2.3.6 Python2中的print语句(已忽略)

    • 2.4 数字

      • 2.4.1 整数

        +,-,*,/,**(乘方)

        print(3**2)	// 3的平方
        print(3**3)	// 3的立方
        
      • 2.4.2 浮点数

      • 2.4.3 使用函数str()避免类型错误

        age = 23
        message = "Happy " + str(age) + "rd Birthday!"
        print(message)
        
      • 2.4.4 Python2中的整数(已忽略)

    • 2.5 注释

      • 2.5.1 如何编写注释

        " # : 单行注释

        # 向大家问好
        print("Hello Python people!")
        

        多行注释:用三个点‘’‘ 多行注释内容’‘’(三个单引号或三个双引号)

      • 2.5.2 该编写什么样的注释

    • 2.6 Python之禅

      命令行中输入“import this”

      Beautiful is better than ugly. //代码可以编写得漂亮而优雅

      Explicit is better than implicit.

      Simple is better than implicit. //两个解决方案,一个简单,一个复杂,就选择简单的吧。

      Complex is better than complicated.//现实是复杂的,有时候可能没有简单的解决方案。这种情况下,就选择最简单可行的解决方案吧。

      Readability counts. //即便是复杂的代码,也要让它易于理解。

      There should be one – and preferably only one – obvious way to do it. //如果让两名Python程序员去解决同一个问题,他们提供的解决方案应大致相同。

      Now is better than never. // 不要企图编写完美无缺的代码;先编写行之有效的代码,再决定是对其做进一步改进,还是转而去编写新代码。

    • 2.7 小结

  • 第三章 列表简介

    • 3.1 列表是什么

      • 3.1.1 访问列表元素

        Python中用方括号([ ])来表示列表,用“,”分隔其中元素

        bicycles = [‘trek’, ‘cannondale’, ‘redline’, ‘specialized’]

        bicycles[0]:访问列表中某个元素

        bicycles = ['trek', 'cannondale', 'redline', 'specialized']
        print(bicycles)	# 显示列表全部内容
        print(bicycles[0])	# 显示列表元素
        print(bicycles[0].title())
        
      • 3.1.2 索引从0而不是1开始

        索引-1表示最后一个列表元素,-2表示倒数第二个元素。

        bicycles = ['trck', 'cannondale', 'redline', 'specialized']
        print(bicycles[-1])	# 显示列表最后一个元素
        
      • 3.1.3 使用列表中的各个值

        bicycles = ['trek', 'cannondale', 'redline', 'specialized']
        message = "My first bicycle was a " + bicycles[0].title() + "."
        print(message)
        
    • 3.2 修改、添加和删除元素

      • 3.2.1 修改列表元素

        通过赋值修改元素

        motorcycles = ['honda', 'yamaha', 'suzuki']
        print(motorcycles)
        motorcycles[0] = 'ducati'
        print(motorcycles)
        
      • 3.2.2 在列表中添加元素

        motorcycles = ['honda', 'yamaha', 'suzuki']
        print(motorcycles)
        motorcycles.append('ducati') # 在元素末尾添加元素
        print(motorcycles)
        motorcycles.insert(0, 'ducati') # 在指定index位置添加新元素
        print(motorcycles)
        
      • 3.2.3 从列表中删除元素

        motorcycles = ['honda', 'yamaha', 'suzuki']
        print(motorcycles)
        del motorcycels[0] # 删除指定位置元素
        print(motorcycles)
        
        poped_motorcycle = motorcycles.pop() # 删除最后一个元素,并返回其值
        print(poped_motorcycle)
        print(poped_motorcycels)
        
        first_owned = motorcycles.pop(0)
        print("The first motorcycle I owned was a " + first_owned.title() + '.')
        
        motorcycles.remove('ducati') # 根据值删除元素,只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。
        print(motorcycles)
        
        too_expensive = 'ducati'
        motorcycles.remove(too_expensive)
        
    • 3.3 组织列表

      • 3.3.1 使用方法sort()对列表进行永久性排序

        cars = ['bmw', 'audi', 'toyota', 'subaru']
        cars.sort() # 从小到大排序
        print(cars)
        cars.sort(reverse = True) # 从大到小排序
        print(cars)
        
      • 3.3.2 使用函数sorted()对列表进行临时排序

        原列表元素不变,返回一个排好序的列表。

        cars = ['bmw', 'audi', 'toyota', 'subaru']
        print("Here is the original list:")
        print(cars)
        print("Here is the sorted list:")
        print(sorted(cars))
        print("Here is the original list again:")
        print(cars)
        print("Here is the reverse sorted list:")
        print(sorted(cars, reverse = True))
        
      • 3.3.3 倒着打印列表reverse()永久反转

        cars = ['bmw', 'audi', 'toyota', 'subrau']
        print(cars)
        cars.reverse()
        print(cars)
        
      • 3.3.4 确定列表的长度len

        cars = ['bmw', 'audi', 'toyota', 'subrau']
        print(len(cars))
        
    • 3.4 使用列表时避免索引错误

      出现索引错误时,打印列表长度判断一下。

    • 3.5 小结

      for in 循环遍历列表。注意缩进,注意冒号
      range():生成一系列数字。不包含最后一个数,可以指定步长。
      list():将值转换为列表
      min,max,sum:数值计算
      “:”:切片。列表中获取部分值
      列表复制:friend_food = my_food[:]
      元组:只读列表。dimensions = (200, 50)

if 语句
and
or
in
not in
if_else
if_elif_else

字典
键值对
del:删除键值对
set:集合(无重复值)
{}:字典定义方法

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值