《Python简易教程》读书笔记

来源:互联网 发布:单片机液晶数字钟论文 编辑:程序博客网 时间:2024/06/07 14:45

读书目的Python编程入门,了解基本语法,看懂Python代码
读书收获从数据类型、运算规则、流程控制、函数四个方面入手,了解了Python的基本语法,基本可以看懂Python代码

心得所有编程语言都都不外乎4部分(数据类型、运算规则、流程控制、封装或函数),任何代码的核心在于数据的变化

1.数据类型

1.1数据

1. 数字2. 字符串

1.2数据结构

1. list[ ]:*类似C语言中的数组*2. tuple( ):*类似C语言中的const数组*3. dict{ }:*类似C语言中的枚举*4. class:*类似C语言中的结构体*

2.运算规则

2.1基本运算符

1. + - * / ...

2.2扩展运算符

1. [0:3]:*数组切片操作符,从原数组中提取第一个到第三个变量生成新的list*2. range(1, 10):*遍历数字1到9*

3.流程控制

3.1分支

#!/usr/bin/pythonnumber = 23guess = int(raw_input('Enter an integer : '))if guess == number:    print 'Congratulations, you guessed it.'  elif guess < number:    print 'No, it is a little higher than that'else:    print 'No, it is a little lower than that'

3.2循环

number = 23running = Truewhile running:    guess = int(raw_input('Enter an integer : '))    if guess == number:        print 'Congratulations, you guessed it.'        running = False    elif guess < number:        print 'No, it is a little higher than that'    else:        print 'No, it is a little lower than that'
for i in range(1, 5):    print i

3.3异常

#!/usr/bin/pythonimport systry:    s = raw_input('Enter something-->')except EOFError:    print '\n why did you do an EOF on me'    sys.exit()except:    print '\nsome error exception occurred'
try:    s = raw_input('Enter something --> ')    if len(s) < 3:        raise ShortInputException(len(s), 3)except EOFError:    print '\nWhy did you do an EOF on me?'except ShortInputException, x:    print 'ShortInputException: The input was of length %d, \    was expecting at least %d' % (x.length, x.atleast)else:    print 'No exception was raised.'
try:    f = file('poem.txt')    while True: # our usual file-reading idiom        line = f.readline()        if len(line) == 0:            break        time.sleep(2)        print line,finally:    f.close()    print 'Cleaning up...closed the file'

4.封装(函数)

4.1自定义函数参数

4.2模块函数参数

import sys  #导入sys模块,可以使用sys模块中提供的函数、变量    __name__    #模块变量  自身运行时为'__main__'
import time    time.strftime('%Y%m%d%H%M%s')    time.sleep(n)
import os    os.system("command")    os.name    os.getcwd()    os.getenv()/putenv()    os.listdir()    os.remove()    os.path.split()
不需要导入模块    dir()       #列出模块定义的函数、变量、类    f = file('path', 'w/r/a')    f.write('string')    f.readline()    f.close()
import pickle    f = file('path', 'w/r/a')    pickle.dump(list1, f)    list2 = pickle.load(f)
1 0
原创粉丝点击