《Python简易教程》读书笔记

本文介绍了Python编程的基础知识,包括数据类型、运算规则、流程控制及函数等核心概念,并通过实例展示了如何进行基本的输入输出操作、异常处理以及文件读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读书目的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/python

number = 23
guess = 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 = 23
running = True
while 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/python
import sys

try:
    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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值