之前是看视频学习的,看老师讲视频有个好处就是,老师一般讲的比较易懂,适合入门
但有个坏处就是讲不了太多知识,这个时候,就需要通过自己看书去学习了。
接下来的计划,主要是把如下三本书系统过一遍了:
《【精001】Python编程快速上手—让繁琐工作自动化》
《【精002】Python编程:从入门到实践》
《【精003】Python核心编程_第3版》
接下来开始把,前几章讲的基础知识,可以大概浏览下,查缺补漏。
《【精001】Python编程快速上手—让繁琐工作自动化》
第一章 Python 基础
- input() 函数: 用户输入后,返回的是一个字符串,使用非字符串得时候,需要进行强制转换
 例如:
# 1  str <==> int
  int('12')     =  12   # str转int, 默认10进制   
  int('12', 16) =  18   # str转int,按16进制转换
  str(18) = '18'      # int转str, 默认10进制转换
  hex(18) = '0x12'    # int转str, 转换为16进制得字符串
  float('3.14') = 3.14    # str转float
  # 2  str <==> dict 字典
  import json
  json_str = '{"a":"b","c":"d"}'
  dictinfo = json.loads(json_str)   # str转dict, 输出dict类型
  json_str = json.dumps(dictinfo )  # dict转str, 输出str类型
  print(type(dictinfo), ' > ' , dictinfo)
  print(type(json_str), ' > ' , json_str)
  输出结果为:
  <class 'dict'>  >  {'a': 'b', 'c': 'd'}
  <class 'str'>  >  {"a": "b", "c": "d"}
  # 3  str ==> list 列表
  str1 = '12345' 
  list1 = list(str1)
  str2 = "123 sjhid dhi" 
  list2 = str2.split() # or list2 = str2.split(" ") 
  str3 = "www.google.com" 
  list3 = str3.split(".") 
  print(list1)  # ['1', '2', '3', '4', '5']
  print(list2)  # ['123', 'sjhid', 'dhi']
  print(list3)  # ['www', 'google', 'com']
  # 4  list ==> str 列表
  list_1 = ['1', '2', '3', '4', '5']
  str_1 = ''.join(list_1)
  print(str_1)   # 12345
  str_1 = '-'.join(list_1)
  print(str_1)   # 1-2-3-4-5
  str_1 = '-'.join(list_1[2:])
  print(str_1)   # 3-4-5第二章 控制流
- range(start, stop[, step]) 函数: 可创建一个整数列表,一般用在 for 循环中。
for i in range(6):
    print(i)   #  0 1 2 3 4 5
for i in range(6,12,2):
    print(i)   #  6 8 10- 导入模块 import
# 导入整个模块
import random
for i in range(5): 
    print(random.randint(1, 10))  # 6 1 7 4 9
# 导入模块中的部分
from random import randint
for i in range(5): 
    print( randint(1, 10)  )  # 3 5 6 7 2
# 同时导入多个模块
import sys, os, math- sys.exit() 提前退出程序(使用前需要 import sys)
第三章 函数
- global 语句: 如果需要在一个函数内修改全局变量,就使用 global 语句。
def spam(): 
    global eggs 
    eggs = 'spam' 
eggs = 'global'
print(eggs)  # global
spam()
print(eggs)  # spam- 异常处理
 在 Python 程序中遇到错误,或“异常”,意味着整个程序崩溃。
 你不希望这发生在真实世界的程序中。相反,你希望程序能检测错误,处理它们,然后继续运行。
 格式:
try:
    pass 代码块    # 在代码块中的所有错误夺回被捕捉
  except ZeroDivisionError:
    pass 错误处理实例:
def spam(divideBy): 
    return 42 / divideBy
print(spam(2))    # 该函数正常运行
print(spam(0))    # 此处会产生异常,因为除数不可以为0
输出结果:
PS C:\Users\Administrator\Desktop\tmp> python .\Untitled-4.py
21.0
Traceback (most recent call last):
  File ".\Untitled-4.py", line 5, in <module>
    print(spam(0))
  File ".\Untitled-4.py", line 2, in spam
    return 42 / divideBy
ZeroDivisionError: division by zero解决方法:在代码中加入异常判断
def spam(divideBy): 
    try:
        return 42 / divideBy
    except ZeroDivisionError:
        print('ERROR:  Invalid argument. ')
print(spam(2))
print(spam(0))
输出结果:
21.0
ERROR:  Invalid argument.
None









