查看: 16669|回复: 11

python学习手册连载

[复制链接]
  • TA的每日心情
    开心
    2019-9-21 13:55
  • 签到天数: 9 天

    [LV.3]偶尔看看II

    发表于 2013-8-31 14:44:55 | 显示全部楼层 |阅读模式
    本帖最后由 冰刀 于 2013-9-7 21:25 编辑

    python学习手册连载
    说明
    本着本人一贯的爱好 学习东西时喜欢记录下来 并且喜欢分享给大家 所以将最近学习的python分享给大家  争取将本文章持续连载下去 一周一更!
    本人是自学无师还未通可能会出现一些错误 请见谅 你可以对本文提建议 找漏洞
    08sec(www.ihonker.org ):
    QQ截图20130831145426.png

    By:   冰刀@08sec
    Email: bingdao@08sec.org
    我的小站(www.free2013.org)

    该贴持续更新 一周一更 大概是每周六更新一次

    不想发新贴 所以就直接 贴回复里了 大家自己翻吧
    ------------------------------------------
    连载(一)
    0x00 准备工作
    本文使用的python环境为:python2.7
    请自行下载
    点击下载
    准备好良好的学习心态
    因为这文章不是专业的 业余水平
    觉得文章内的东西没理解到再百度下去 或者 自己试验一下
    0x01 你要知道的python
    为了提起大家学习的兴趣  我是不会给大家讲 python有多好有多好
    以下是使用python做产品的公司:
    YouTube视频分享网站
    EVE online大型网络游戏
    Intel Cisco IBM 等等………
    希望大家是有坚定的意志 来学习一门语言 不管是生活中的语言还是编程语言
    优点
    简单:Python是一种代表简单主义思想的语言。阅读一个良好的Python程序就感觉像是在读英语一样。它使你能够专注于解决问题而不是去搞明白语言本身。
    易学:Python极其容易上手,因为Python有极其简单的说明文档[4]。
    速度快:Python 的底层是用 C 语言写的,很多标准库和第三方库也都是用 C 写的,运行速度非常快。[1]
    免费、开源:Python是FLOSS(自由/开放源码软件)之一。使用者可以自由地发布这个软件的拷贝、阅读它的源代码、对它做改动、把它的一部分用于新的自由软件中。FLOSS是基于一个团体分享知识的概念。
    高层语言:用Python语言编写程序的时候无需考虑诸如如何管理你的程序使用的内存一类的底层细节。
    缺点
    单行语句和命令行输出问题:很多时候不能将程序连写成一行,如import sys;for i in sys.path:print i。而perl和awk就无此限制,可以较为方便的在shell下完成简单程序,不需要如Python一样,必须将程序写入一个.py文件。
    独特的语法
    这也许不应该被称为局限,但是它用缩进来区分语句关系的方式还是给很多初学者带来了困惑。即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,而这是用肉眼无法分别的。
    运行速度慢:这里是指与C和c++相比.
                                                    //部分资料来自http://baike.baidu.com/view/21087.htm
    0x02 数字数字常量
    谁说一开始就要来个hello world  如果你觉得那我只能说你是super boy(sb)
    数字 对于我们这些上过学受过伤的孩子 一点都不陌生 都知道些常见的数字类型比如:
    整数(没有小数)
    浮点型(带有小数点的数字)
    负数
    十进制数
    有理分数
    集合
    等等
    数字                                      常量
    12342134,-425,0,122222222222                数字(无穷大小)
    1.654,1.0                                      浮点型
    0177,0x9ff,0b101010                           八进制,十六进制,二进制
    123124j                                       复数型
    …………..
    整数: 一般整数和长整数
    一般整数为32位  可以用l和L结尾 使其成为 长整数
    长整数为无穷大小 >32位都是长整数
    如何知道那是一个数字类型的呢?
    “什么这个还需要问么? 我肉眼就能看出来”
    那你还真错了
    请看图
    101348vcjlklw36v45nxuv.png

    “看”起来是一样的 但类型是绝对不同的 类型不同就会影响到后续的运算……….
    101415q8l1v82xv74j6282.png



    还有一些更高级的数字类型 这等以后我学到后再给大家讲
    内置数学工具
    在python当中支持简单的数学运算
    比如(进入交互界面 演示 win按ctrl+r 输入python即可打开  linux环境的直接python):
    Pyhon 2.7.4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    >>>1+3
    4
    >>>print ‘bitter sb’
    bitter sb
    在交互界面可以直接执行语句 比较方便
    Python提供的:
    一些处理数字的工具的表达式操作符:
    +, - , / ,* ,** 等等
    内置数学函数
    pow , abs(取绝对值) , round , hex , bin , max , min
    公用模块
    random , math
    越学习越会遇到这些东西
    下面举一斤荔枝 来演示给大家 便于理解 方便以后使用
    如果对上面的一些内置数学函数 不了解的话 就 使用help()
    比如help(round)
    101442ffuibi0r3f7gy7tn.jpg

    pow(a,b)= a**b 就是 a的b次方
    round(3.4) 将括号内的数字 四舍五入
    101502p5lgl0omorahumm0.jpg
    以上讲的比较简单  实在有兴趣的 买书去 或者百度一些书籍的PDF 再见
    修正记录:

    101528e2wd8fgguw0zzd02.png

    已修改



    -------------------
    pdf下载地址:
    链接:http://pan.baidu.com/share/link? ... 70&uk=523212399 密码:sbrg

    评分

    参与人数 1i币 +10 收起 理由
    Free_小东 + 10 膜拜编程大牛

    查看全部评分

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2019-9-21 13:55
  • 签到天数: 9 天

    [LV.3]偶尔看看II

     楼主| 发表于 2013-8-31 14:45:17 | 显示全部楼层

    连载(二)

    本帖最后由 冰刀 于 2013-8-31 15:03 编辑

    连载(二)
    -----------------
    0x02数字操作符:上面提到了表达式操作符那些符号

    些处理数字的工具的表达式操作符:
    +, - , / ,* ,** 等等


    我们现在来看看什么是操作符


    "表达式是处理数字的最基本的工具 由通常的数学符号和操作符号写出来的"
    上一篇就演示过的一些简单的操作比如:
    >>>4+3


    Python中的所有的表达式操作符中有一些是一看就懂的
    加减乘除等等

    不常见的比如
    is 操作符测试对象身份(就是内存地址,内存地址用id()函数查看)
    图片4.png

    Lambd创建匿名函数 后面会讲到








    以下是python表达式操作符已经程序(看不懂英文的谷歌翻译去)

    Python - Basic Operators:
    Operator
    Description
    Example
    +
    Addition - Adds values on either side of the operator
    a + b will give 30
    -
    Subtraction - Subtracts right hand operand from left hand operand
    a - b will give -10
    *
    Multiplication - Multiplies values on either side of the operator
    a * b will give 200
    /
    Division - Divides left hand operand by right hand operand
    b / a will give 2
    %
    Modulus - Divides left hand operand by right hand operand and returns remainder
    b % a will give 0
    **
    Exponent - Performs exponential (power) calculation on operators
    a**b will give 10 to the power 20
    //
    Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.
    9//2 is equal to 4 and 9.0//2.0 is equal to 4.0
    ==
    Checks if the value of two operands are equal or not, if yes then condition becomes true.
    (a == b) is not true.
    !=
    Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
    (a != b) is true.
    <>
    Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
    (a <> b) is true. This is similar to != operator.
    >
    Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
    (a > b) is not true.
    <
    Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
    (a < b) is true.
    >=
    Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
    (a >= b) is not true.
    <=
    Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
    (a <= b) is true.
    =
    Simple assignment operator, Assigns values from right side operands to left side operand
    c = a + b will assigne value of a + b into c
    +=
    Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
    c += a is equivalent to c = c + a
    -=
    Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
    c -= a is equivalent to c = c - a
    *=
    Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
    c *= a is equivalent to c = c * a
    /=
    Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
    c /= a is equivalent to c = c / a
    %=
    Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
    c %= a is equivalent to c = c % a
    **=
    Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand
    c **= a is equivalent to c = c ** a
    //=
    Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operand
    c //= a is equivalent to c = c // a
    &
    Binary AND Operator copies a bit to the result if it exists in both operands.
    (a & b) will give 12 which is 0000 1100
    |
    Binary OR Operator copies a bit if it exists in eather operand.
    (a | b) will give 61 which is 0011 1101
    ^
    Binary XOR Operator copies the bit if it is set in one operand but not both.
    (a ^ b) will give 49 which is 0011 0001
    ~
    Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
    (~a ) will give -60 which is 1100 0011
    <<
    Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
    a << 2 will give 240 which is 1111 0000
    >>
    Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
    a >> 2 will give 15 which is 0000 1111
    and
    Called Logical AND operator. If both the operands are true then then condition becomes true.
    (a and b) is true.
    or
    Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.
    (a or b) is true.
    not
    Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
    not(a && b) is false.
    in
    Evaluates to true if it finds a variable in the specified sequence and false otherwise.
    x in y, here in results in a 1 if x is a member of sequence y.
    not in
    Evaluates to true if it finds a variable in the specified sequence and false otherwise.
    x not in y, here not in results in a 1 if x is a member of sequence y.
    is
    Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
    x is y, here is results in 1 if id(x) equals id(y).
    is not
    Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.
    x is not y, here is not results in 1 if id(x) is not equal to id(y).







    操作符的优先级:如果你只使用一个操作符 那没什么影响
    如果是2个以上的操作符同时出现呢?
    该执行哪一个呢?
    就像 小学学数学的时候 老师交过我们一个口诀:
    加减乘除,从左到右依次进行

    下面是操作符的优先级顺序(看不懂英文的谷歌翻译去)
    Python Operators PrecedenceThe following table lists all operators from highest precedence to lowest.
    Operator
    Description
    **
    Exponentiation (raise to the power)
    ~ + -
    Ccomplement, unary plus and minus (method names for the last two are +@ and -@)
    * / % //
    Multiply, divide, modulo and floor division
    + -
    Addition and subtraction
    >> <<
    Right and left bitwise shift
    &
    Bitwise 'AND'
    ^ |
    Bitwise exclusive `OR' and regular `OR'
    <= < > >=
    Comparison operators
    <> == !=
    Equality operators
    = %= /= //= -= += |= &= >>= <<= *= **=
    Assignment operators
    is | is not
    Identity operators
    in | not in
    Membership operators
    not |or | and
    Logical operators


    上面的数学口诀还没背完呢  
    加减乘除 从左到右依次进行 有括号先算括号内的

    混合类型自动升级:
    提问:提到了一些数字类型 如 整数型 浮点型 复数型
    那么 3+3.14 后的结果是什么类型呢?
    是整数型?还是浮点型?为什么?



    答案是浮点型 这里是因为python将类型转换为了最复杂的类型
    浮点型比整数型复杂 复数型比浮点型复杂
    比如:
    图片1.png
    也可以用一些函数 手动去转换类型 如:
    图片2.png
    但这些是没有必要的  python在这种转换中 得到的结果肯定是你想要的

    混合类型转换只存在于数字类型 比如:

    图片3.png

    数字的就讲到这了 虽然感觉到还有许多没讲 但这个只是为了分享给大家我的python学习过程
    其他的我自己看就不给大家发出来了
    如果有兴趣的自己去下载或者购买实体书籍吧

    下一载就是 变量 有关的
    未完待赞


    链接:http://pan.baidu.com/share/link? ... 48&uk=523212399 密码:chn1
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2020-8-3 22:39
  • 签到天数: 84 天

    [LV.6]常住居民II

    发表于 2013-8-31 15:15:48 | 显示全部楼层
    膜拜编程大牛
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2020-12-4 20:42
  • 签到天数: 23 天

    [LV.4]偶尔看看III

    发表于 2013-8-31 15:21:12 | 显示全部楼层
    膜拜冰刀编程牛、
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2015-8-10 17:57
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2013-8-31 15:37:23 | 显示全部楼层
    冰刀牛逼哈  学习了!
    回复 支持 反对

    使用道具 举报

    弑天 该用户已被删除
    发表于 2013-8-31 19:38:47 | 显示全部楼层
    提示: 作者被禁止或删除 内容自动屏蔽
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2013-8-31 20:36:35 | 显示全部楼层
    忍住不当伸手党,留着以后看吧,开学要学VB、数据结构,自己又在自学C++,忙不过来了
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2021-9-9 23:35
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2013-8-31 23:17:04 | 显示全部楼层
    面向对像的,我怎么都学不会
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2016-8-16 17:47
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    发表于 2013-9-2 20:38:41 | 显示全部楼层
    刀刀 牛逼了
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2019-9-21 13:55
  • 签到天数: 9 天

    [LV.3]偶尔看看II

     楼主| 发表于 2013-9-7 21:23:39 | 显示全部楼层
    本帖最后由 冰刀 于 2013-9-7 21:26 编辑

    连载(三)
    0x03动态类型
    变量,对象和引用
    直接举例吧
    一个简单的语句:
    a = 3
    ------------
    >>>a = 3
    >>>a
    3
    ------------
    执行a =3 python做了这些事:
    创建一个对象代表值3
    在没有a变量的情况下,创建一个变量a
    将变量与对象3相互连接


    Python中 把 变量与对象3相互连接 称为引用 引用是一种关系

    按照这个流程 每按这流程运行一次就会创建一些东西比如:
    >>>id(a)
    4958296

    每次就会创建一个新对象(一块内存)



    变量没有类型
    >>>a=3
    >>>a=1.3
    >>>a=123123123L

    当我们做了以上操作时 难道就说a的类型从整数转换为浮点型转换为long了么?
    当然不是 a只是引用了一个对象

    Python中类型是与对象相关联的 而不是和变量相关联的

    变量没有类型!
    对象的垃圾收集>>>a=3
    >>>a=1.3
    >>>a=123123123L

    我们运行这些代码是可行的 因为只是进行了引用
    那么 引用了1.33去哪儿了?  123123123L引用后 1.3去了那儿?

    这是靠python中自动回收对象空间的技术叫做: “垃圾收集”

    在每一次 a引用一个新的对象时 python就会回收之前对象的空间

    共享引用
    >>>a = 3
    >>>b = a
    >>>a =4
    >>>b

    当我们运行这些语句后会返回什么值?


    4还是3?
    答案是3

    还是因为引用的关系
    a b 都是引用了3这个对象
    所以不管a怎么变 b还是引用的3

    但是不是不管什么情况都是这样的呢?

    >>>a=[1,2,3]
    >>>b=a
    >>>a[0]=3
    >>>b
    [3,2,3]

    很清楚的看到了在列表的情况下时
    是发生了一起改变的结果
    这是因为什么呢?  还是用id()来看看内在发生了什么吧
    >>>a=[1,2,3]
    >>>id(a)
    38952312
    >>>a[0]=3
    >>>a
    [3,2,3]
    >>>id(a)
    38952312

    可以看到还是一样的空间地址
    说明列表修改 是在原处修改对象
    这就导致了 ab会一起改变
    如果不想呢?
    那么就让b创建一个新的引用
    >>>a=[1,2,3]
    >>>id(a)
    38952392
    >>>b=a[:]
    >>>b
    [1,2,3]
    >>>id(d)
    38952312

    使用从头到尾的分片时  再修改ab是不会改变的



    这里顺便讲下关于的检验相等的值

    我们所知道最简单的就是
    >>>a=b
    true
    >>>a is b
    false

    is是比较引用的方法
    b不是引用的a  是引用a的完全分片
    所以是false


    就到这把  

    未完待赞~

    下回讲点基础和字符串的东西

    pdf下载:
    链接:http://pan.baidu.com/share/link? ... 40&uk=523212399 密码:dior
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    指导单位

    江苏省公安厅

    江苏省通信管理局

    浙江省台州刑侦支队

    DEFCON GROUP 86025

    旗下站点

    邮箱系统

    应急响应中心

    红盟安全

    联系我们

    官方QQ群:112851260

    官方邮箱:security#ihonker.org(#改成@)

    官方核心成员

    Archiver|手机版|小黑屋| ( 苏ICP备2021031567号 )

    GMT+8, 2024-5-15 09:10 , Processed in 0.059375 second(s), 18 queries , Gzip On, MemCache On.

    Powered by ihonker.com

    Copyright © 2015-现在.

  • 返回顶部