python 基础(一)

申明:本博主使用python 3.x的版本,文章中如无明确提示均为python 3.x编译器下进行运行的,请各位知悉。

 

一、python的发展历史及介绍

    1、python的历史
        网上的文章很多,请自行百度.....

    2、python和其他语言的优势
        语言并无优劣之分,博主学习python也是基于工作需要。如果你一定想要指导python语言的优劣也请自行百度。

    3、python是解释性语言
        计算机语言可以分为编译型和解释性两大类,举个例子来说明:一个中国人是看不懂英文文章的,如果他想阅读英文文章有两种方式,如果这篇英文文章有中文的翻译版,就可以直接去读对应的中文版文件就可以,这个有点类似编译型,已经有出版社事先翻译成中文,可以直接阅读,如果没有中文的翻译版,这个时候就需要有翻译官了,由翻译官逐句的将文章解释成中文,也可以完成阅读,这个就类似于解释性语言。编译型语言在执行前就已经编译好了(类似于已经翻译好的中文文章),所以可以直接运行,然而解释性语言不需要编译但是在执行的
时候需要逐条进行解释(类似于翻译官逐条翻译)。

 

 

二、python的安装与入门
    

    1、python的安装

        Linux平台下自带python,无需安装
        windows平台,下载安装包,基本上都是下一步,安装时选择把安装路径加入环境变量即可

    2、python程序员的敲门砖
         linux(执行前需要赋予可执行权限):           

1 hello.py
2 
3 #!/usr/bin/env python
4 print("hello world")
5 
6 执行:./hello.py

 

 

 

三、python变量

 

    1、变量的作用

         (1)存储中间值
         (2)便于程序调整
        当使用变量时,执行修改变量值,则整个程序中引用该变量的语句都会发生相应的变化,无需再手动修改各条语句

    2、变量定义的规则:
        (1)变量名只能是字母、数字和下划线的组成,即变量名中不能包含特殊字符
        (2)变量名不能以数字开头
        (3)关键字不能作为变量名

 

四、python字符编码

     python 2.x默认使用ascii编码,无法输出中文

     python 3.x默认使用utf-8字符集

 

注意:

python 2可通过以下语句指定编码方式:

1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 print("你好,世界!")

 

 

 

五、数据类型

    1、int(整型)

        python 2:中存在以下限制:
       32位系统:取值范围-2**31~2**31-1
       64位系统:取值范围-2**63~2**63-1

   

     2、long(长整型)

          python 2.x超过整型的表示范围自动转换成长整型,即加上L

    3、浮点型(博主暂时用不到,想了解可以百度)

    4、复数(博主暂时用不到,想了解可以百度)

 

    5、布尔值:

        真或假:True or False

 

    6、字符串:

 1    字符串的操作:
 2 >>> msg = "  hello world!  "
 3 >>> msg.strip()      //去取字符串两边的空格
 4 'hello world!'
 5 >>> 
 6 >>> msg.count("l")     //统计"l"在字符串中个数
 7 3
 8 >>> msg.index("h")     //查找第一个"h"的索引值
 9 2
10 >>> msg.find("o")    //查找第一个"o"在字符串中的位置
11 6
12 >>> msg[2:4]        //取索引为2~4(不含4)之间的字符,顾头不顾尾原则
13 'he'
14 >>> 
15 >>> len(msg)        //字符串的长度
16 16
17 >>>

 

 

 

    7、列表:
        同类元素的集合      

 1     name=['admin','root','system']
 2 
 3 >>> name=['admin','root','system']
 4 >>> name.append("test")                   //添加元素
 5 >>> name
 6 ['admin', 'root', 'system', 'test']   
 7 >>> name.insert(2,"scott")               //列表插入
 8 >>> name
 9 ['admin', 'root', 'scott', 'system', 'test']
10 >>> 
11 >>> name[1]                      //取出元素
12 'root'
13 >>> 
14 >>> del name[2]                 //删除列表元素
15 >>> 
16 >>> 
17 >>> 
18 >>> name
19 ['admin', 'root', 'system', 'test']
20 >>> name.remove("test")                //删除列表元素
21 >>> name
22 ['admin', 'root', 'system']
23 >>> name.index("root")                 //列表索引值
24 1
25 >>> 
26 >>> name[1] = "ROOT"
27 >>> name
28 ['admin', 'ROOT', 'system']
29 >>> name[name.index("ROOT")] = "sys"   
30 >>> name
31 ['admin', 'sys', 'system']
32 >>> 
33 >>> name.append("scott")
34 >>> name.append("jack")
35 >>> name.append("apple")
36 >>> name.insert(2,"test")
37 >>> name
38 ['admin', 'sys', 'test', 'system', 'scott', 'jack', 'apple']
39 >>> name.append("test")
40 >>> 
41 >>> 
42 >>> name
43 ['admin', 'sys', 'test', 'system', 'scott', 'jack', 'apple', 'test']
44 >>> name.count("test")                                //列表某一元素数量统计
45 2
46 >>> name.sort()                                    //列表元素排序,只能是相同类型的如字符串
47 >>> name
48 ['admin', 'apple', 'jack', 'scott', 'sys', 'system', 'test', 'test']
49 >>> name.reverse()                                 //列表反转
50 >>> name
51 ['test', 'test', 'system', 'sys', 'scott', 'jack', 'apple', 'admin']
52 >>> 
53 >>> name[:5]                               //列表切片
54 ['test', 'test', 'system', 'sys', 'scott']
55 >>> name[2:4]                         //列表切片
56 ['system', 'sys'] 
57 >>> 
58 >>> name[0:-1:1]                 //列表切片,设定步长
59 ['test', 'test', 'system', 'sys', 'scott', 'jack', 'apple']
60 >>> 
61 >>> name[0:-1:2]                  //列表切片,设定步长
62 ['test', 'system', 'scott', 'apple']
63 >>> 
64 
65 >>> type(name)
66 <class 'list'>
67 >>> 
68 >>> 
69 >>> 
70 >>> id(name)
71 44239128
72 >>> names=name
73 >>> id(names)
74 44239128
75 >>> 
76 >>> name1=name.copy()      //列表复制,不同于赋值,复制相当于重新分配一块内存地址
77 >>> id(name1)
78 44236032

 

 

 

六、python的运算
    6.1数学运算:
     

符号 意义 示例
+ 加 3+2结果为5
- 减 3-1结果为2
* 乘 2*2结果为4
/ 除 5/2结果为2.5
% 取余数 5%2结果为1
** 幂 2**3结果为8
// 取商 5//2结果为2

 

 

 

 

 

 

 

 

   

    6.2比较运算

符号 意义 示例
== 等于 a==2
!= 不等于 3 !=1
> 大于 3>1
< 小于 1<3
>= 大于等于 3>=3
<= 小于等于 3<=3

 

 

 

 

 

 

 

 

 

    6.3赋值运算

符号 意义 示例
= 赋值 c=a+b
+= 加法赋值 c+=a相当于c=c+a
-= 减法赋值 c-=a相当于c=c-a
*= 乘法赋值 c*=a相当于c=c*a
/= 除法赋值 c/=a相当于c=c/a
%= 取模赋值 c%=a相当于c=c%a
**= 幂赋值 c**=a相当于c=c**a
//= 取整赋值 c//=a相当于c=c//a

 

 

 

 

 

 

 

 

 

    6.4逻辑运算

符号 意义 示例
and 与 (a and b) a和b全为真是返回true

or

或 (a or b) a和b有一个为真是返回true

not

非 a为true,则not a为false

 

 

 

 

 

 

 

 

 

    6.5成员运算

符号 意义 示例
is 存在 a在b序列中,若a在b序列中返回true

not is

不存在 a不在b序列中,若a不在b序列中返回true

 

 

 

 

 

 

 

 

     

    6.6身份运算

符号 意义 示例
is 引用同一个对象 x is y,若果id(x)等于id(y),is返回结果1

is not

不是引用同一个对象 x is not y,若果id(x)不等于id(y),is not返回结果1

 

 

 

 

 

 

 

 

 

    6.7位运算(后续补充)

 

七、用户交互

1 >>> input("username:")
2 username:bigroot
3 'bigroot'
4 >>>

 

 

八、流程控制(后续补充)
   

    8.1判断结构

         if ... else

   

    8.2循环结果
         for in
         while true

 

九、三元运算


result = 值1 if 条件 else 值2
如果条件为真:result = 值1
如果条件为假:result = 值2

posted @ 2017-01-11 14:39  春野之火  阅读(240)  评论(0)    收藏  举报