前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python学习笔记(二)Python基

Python学习笔记(二)Python基

作者头像
py3study
发布2020-01-15 22:50:50
4680
发布2020-01-15 22:50:50
举报
文章被收录于专栏:python3
代码语言:javascript
复制
[root@kaibin ~]# ipython
In [1]: import platform
In [2]: print platform.uname()
('Linux', 'kaibin.test1', '2.6.32-431.el6.x86_64', '#1 SMP Fri Nov 22 03:15:09 UTC 2013', 'x86_64', 'x86_64')
In [3]: dir(platform)        #查看platform支持的功能:dir(platform)
In [4]: exit()

简单方法:

       1.编译安装新版本至某特定路径:

       2.pyenv

源码包安装python:

代码语言:javascript
复制
[root@kaibin ~]# ll Python-2.7.6.tgz 
-rw-r--r--. 1 root root 14725931 1月  25 2015 Python-2.7.6.tgz
[root@kaibin ~]# tar xvf Python-2.7.6.tgz
依赖readlinde-devel开发程序
[root@kaibin ~]# yum -y install readline-devel
[root@kaibin ~]# cd Python-2.7.6
[root@kaibin Python-2.7.6]# ls
config.guess  configure.ac  Grammar     Lib      Makefile.pre.in  Objects  PCbuild        README    Tools
config.sub    Demo          Include     LICENSE  Misc             Parser   pyconfig.h.in  RISCOS
configure     Doc           install-sh  Mac      Modules          PC       Python         setup.py
[root@kaibin Python-2.7.6]# ./configure --prefix=/usr/local/python-27
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux2
checking EXTRAPLATDIR... 
checking for --without-gcc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/Python-2.7.6':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
需要安装 gcc
[root@kaibin Python-2.7.6]# yum -y install gcc
[root@kaibin Python-2.7.6]# ./configure --prefix=/usr/local/python-27
[root@kaibin Python-2.7.6]# make && make install
Tab补全功能:(为ipython功能模块
[root@kaibin ~]# wget http://archive.ipython.org/release/1.2.1/ipython-1.2.1.tar.gz
[root@kaibin ~]# tar xf ipython-1.2.1.tar.gz 
[root@kaibin ~]# cd ipython-1.2.1
[root@kaibin ipython-1.2.1]# /usr/local/python-27/bin/python2.7 setup.py build
[root@kaibin ipython-1.2.1]# /usr/local/python-27/bin/python2.7 setup.py install
[root@kaibin ipython-1.2.1]# ln -sv /usr/local/python-27/bin/python2.7 /usr/bin/python2.7
"/usr/bin/python2.7" -> "/usr/local/python-27/bin/python2.7"       
[root@kaibin ipython-1.2.1]# ln -sv /usr/local/python-27/bin/ipython /usr/bin/ipython
"/usr/bin/ipython" -> "/usr/local/python-27/bin/ipython"

数据结构

通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些元素可以是数字或者字符,甚至可以是其他的数据结构

Python最基本的数据结构是序列

序列中的每个元素被分配一个序号--即元素的位置,也成为索引:索引从0开始编号

Python包含6种内建的数据序列:列表,元组,数字串

Unicode字符串,buffer对象和xrange对象

Python的关键要素

1.基本数据类型

2.对象引用

3.组合数据类型

4.逻辑操作符

5.控制流语句

6.算数操作符

7.输入/输出

8.函数的创建与调用

1. 基本数据类型

wKioL1TE6K-Q4c0WAAGsYZgS4EU727.jpg
wKioL1TE6K-Q4c0WAAGsYZgS4EU727.jpg

变量赋值:

代码语言:javascript
复制
数字:
In [8]: num=1

In [9]: id(num)
Out[9]: 32365672

In [10]: num=2

In [11]: id(num)
Out[11]: 32365648
字母:
In [12]: name="Jerry"
In [15]: print name[0]
J
In [13]: print name[1]
e

In [14]: print name[2]
r
In [16]: id(name)
Out[16]: 43542256
查看变量类型:
In [17]: type(num)
Out[17]: int

In [18]: type(name)
Out[18]: str
#查看platform支持的功能:dir(platform)
In [3]: dir(platform)

2.对象引用

wKiom1TE6FWRk1AZAAKWsWiVGWc954.jpg
wKiom1TE6FWRk1AZAAKWsWiVGWc954.jpg
代码语言:javascript
复制
In [1]: name="Jack"        #字符串是不可变量

In [2]: id(name)
Out[2]: 41585664

In [3]: iname="Jack"

In [4]: id(iname)
Out[4]: 41585664

变量规则

*只能包含字母,数字,和下划线,且不能以数字开头

*区分字母大小写

*禁止使用保留字(系统中的关键字)

     Python2和Python3的保留字有所不同

命名惯例

*以单一下划线开头的变量名(_x)不会被from module import*语句导入

*前后有双下划线的变量名(__x__)是系统定义的变量名,对Python解释器有特殊的意义

*以两个下划线开头但结尾没有下划线的变量名(__x)是类的本地变量

*交互模式下,变量名"_"用于保存最后表达式的结果

代码语言:javascript
复制
In [1]: 1+1
Out[1]: 2

In [2]: print _

注意:变量名没有类型,对象才有

代码语言:javascript
复制
In [7]: name="Jack"
In [8]: type(name)
Out[8]: str

In [9]: name=3.14
In [10]: type(name)
Out[10]: float

3.组合数据类型

数据结构

通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合

Python常用的数据类型

序列类型

    *列表:使用[]创建

代码语言:javascript
复制
In [11]: a1=["This","is","a","Pig"]
In [12]: a1[0]                    #引用对象
Out[12]: 'This'
In [13]: a1[0][0]
Out[13]: 'T'

    *元组:使用()创建,如('one','two')

代码语言:javascript
复制
In [14]: t1=("This","is")
In [15]: t1[0]
Out[15]: 'This'
In [16]: t1[1]
Out[16]: 'is'
In [17]: t1[0][0]
Out[17]: 'T'

列表和元组的区别

代码语言:javascript
复制
In [11]: a1=["This","is","a","Pig"]
In [18]: print a1
['This', 'is', 'a', 'Pig']
In [19]: a1[3]="sheep"
In [20]: print a1
['This', 'is', 'a', 'sheep']
In [21]: id(a1)
Out[21]: 45304216
In [22]: a1[3]="Jack"
In [23]: id(a1)
Out[23]: 45304216            #前后内存空间位置一致

In [14]: t1=("This","is")
In [24]: print t1
('This', 'is')
In [28]: t1[1]="what"        #尝试修改,但是报错,无法修改
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-367d778d7cb3> in <module>()
----> 1 t1[1]="what"

TypeError: 'tuple' object does not support item assignment

    *字符串也属于序列类型

代码语言:javascript
复制
In [29]: name="Jerry"

In [30]: name[0]
Out[30]: 'J'

In [31]: name[0:1]
Out[31]: 'J'

In [32]: name[0:2]
Out[32]: 'Je'

In [33]: name[:2]
Out[33]: 'Je'

In [34]: name[2:]
Out[34]: 'rry'

In [35]: name[0:4]
Out[35]: 'Jerr'

In [36]: name[0:4:2]
Out[36]: 'Jr'

集合类型

集合

映射

字典

字典是可变对象,元组是不可变序列

Python中,组合数据类型也是对象,因此其可以嵌套

实质上,列表和元组并不是真正存储数据,而是存放对象引用

Python对象可以具有其可以被调用的特定"(方法函数)"

元组,列表以及字符串等数据类型是"有大小的",其长度可使用内置函数len()测量

代码语言:javascript
复制
In [37]: print name
Jerry
In [38]: len(name)        #获取元素个数
Out[38]: 5

In [39]: print a1
['This', 'is', 'a', 'Jack']
In [40]: len(a1)
Out[40]: 4

4.逻辑操作符

*逻辑运算时任何程序设计语言的基本功能

*Python提供了4组逻辑运算

    *身份操作符

        is:判定左端对象引用是否相同于右端对象引用;也可以与None进行;

代码语言:javascript
复制
In [7]: name="Hello"
In [8]: name1="Hi"
In [9]: name is name1
Out[9]: False

In [10]: name="Hello"
In [11]: name1="Hello"
In [12]: name is name1
Out[12]: True

In [13]: type(name) is type(name1)
Out[13]: True

*比较操作符

        <,>,<=,>=,!=,==

*成员操作符

        in或not in:测试成员关系

*逻辑运算符

        an,or,not

5.控制流语句

*控制流语句是过程式编程语句的基本控制机制

*python的常见控制流语句

    if

    while

    for...in

    try

6.算数操作符

*Python提供了完整的算数操作符集

*很多的Python数据类型也可以使用增强型的赋值操作符,如+=,-=

*同样的功能使用增强型的赋值操作符的性能较好

*Python的int类型是不可变的,因此,增强型赋值的实际过程是创建了一个新的对象来存储结果后将变量名执行了重新绑定

7.输入输出

*现实中,具有实际功能的程序必须能够读取输入(如从键盘或文件中),以及产生输出,并写到终端文件中;

*Python的输入/输出

    输出

    Python3:print()函数

    Python2: print语句

    输入

    input()

    raw_input()

代码语言:javascript
复制
In [14]: raw_input("please input a num:")
please input a num:4
Out[14]: '4'

In [16]: a=raw_input("please input a num:")
please input a num:b
In [18]: print a
b

*Python解释器提供了3种标准文件对象,分别是标准输入,标准输出和标准错误,他们在sys模块中分别以sys.stdin,sys.stdout,和sys.stderr形式提供

*Python的print语句实现打印一一一个对程序友好的标准输入流接口

*从技术角度来讲,print是把一个多或者多个对象转换为其文本表达形式,然后发送给标准输入或者另一个类似文件的流

    在Python中,打印与文件和流的概念联系紧密

        文件写入方法是把字符串写入到任意文件

        print默认把对象打印到stdout流,并添加了一定的格式化

    实质上,print语句只是Python的人性化特征的具体实现,他提供了sys.stdout.write()的简单接   口,再加上一些默认的格式设置

    print结构一个逗号分隔的对象列表,并为行尾自动添加一个换行符,如果不需要,则在最后个元素后添加逗号

代码语言:javascript
复制
In [19]: a="Jack"

In [20]: b="Tom"

In [21]: print a,b
Jack Tom

print的格式化输出:

wKioL1TFkTqgPP0QAAEKwGUKPfg178.jpg
wKioL1TFkTqgPP0QAAEKwGUKPfg178.jpg
代码语言:javascript
复制
In [23]: num=7.9

In [24]: print "The num is %f" % num
The num is 7.900000

In [25]: print "The num is %d" % num
The num is 7

In [26]: num1=9.13

In [27]: print "The nums are %d and %f" %(num,num1)
The nums are 7 and 9.130000

In [28]: print "The nums are %d and %f" %(num,3.1)
The nums are 7 and 3.100000

In [29]: print "The nums are %e and %f" %(num,3.1)
The nums are 7.900000e+00 and 3.100000

数据类型的转换:

代码语言:javascript
复制
In [23]: num=7.9
In [30]: name="hello"

In [31]: print "The name is %s." %name
The name is hello.

In [32]: print "The name is %s." %num
The name is 7.9.

In [33]: test1=str(num)

In [34]: type(test1)
Out[34]: str

In [35]: type(num)
Out[35]: float

查看常用的内置函数(由内建函数引用)

代码语言:javascript
复制
In [39]: dir(__builtins__)
Out[38]: 
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BufferError',
 'BytesWarning',
 'DeprecationWarning',
 .....

查看函数用法

代码语言:javascript
复制
In [39]: help(str)

In [40]: name="jack"

In [41]: name.upper()
Out[41]: 'JACK'
wKiom1TFmxzSn59JAAML3OGecUo047.jpg
wKiom1TFmxzSn59JAAML3OGecUo047.jpg
代码语言:javascript
复制
In [42]: print "The nums are %e and %f" %(num,3.1)
The nums are 7.900000e+00 and 3.100000

In [43]: print "The nums are %+e and %f" %(num,3.1)
The nums are +7.900000e+00 and 3.100000

In [44]: print "The nums are %+e and %f" %(num,-3.1)
The nums are +7.900000e+00 and -3.100000

In [45]: print "The nums are %f and %f" %(num,-3.1)
The nums are 7.900000 and -3.100000

In [46]: print "The nums are %+f and %+f" %(num,-3.1)
The nums are +7.900000 and -3.100000

In [48]: print "The nums are %+20f and %+f" %(num,-3.1)
The nums are            +7.900000 and -3.100000

In [50]: print "The nums are %-20f and %+f" %(num,-3.1)
The nums are 7.900000             and -3.100000

In [5]: print "The nums are %+20.10f and %+f" %(num,-3.1)
The nums are        +7.9000000000 and -3.100000

In [7]: print "The nums are %+20.15f and %+f" %(num,-3.1)
The nums are   +7.900000000000000 and -3.100000

字典:键值的集合(kv集合)

代码语言:javascript
复制
In [9]: d1={'a':31, 'b':68}
In [10]: d1['a']
Out[10]: 31

In [11]: d1={1:31, 2:68}
In [12]: d1[1],d1[2]
Out[12]: (31, 68)

In [8]: d1={'a':31,'b':66}
In [9]: print "The float is %(a)f." %d1
The float is 31.000000.

In [10]: d2={0:31,1:66}
In [11]: d2[0]
Out[11]: 31
In [12]: print "The float is %(0)f." %d2            #尝试用数字元素,但是失败
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-f157136aebc0> in <module>()
----> 1 print "The float is %(0)f." %d2

KeyError: '0'

8.函数的创建与调用

wKiom1THtEfg1XWXAAOj3gAPm3o139.jpg
wKiom1THtEfg1XWXAAOj3gAPm3o139.jpg
代码语言:javascript
复制
In [14]: def printName(name):        #自定义函数printName
   ....:     print name
   ....:     

In [15]: printName("Jack")           #调用printName函数,传参"Jack"
Jack

In [16]: test="Hello"

In [17]: printName(test)
Hello

In [19]: callable(printName)        #查看函数是否能够调用
Out[19]: True

In [21]: dir(__builtin__)           #查看可用内置函数
Out[21]: 
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BufferError',
 'BytesWarning',
 'DeprecationWarning',
 'EOFError',
 'Ellipsis',
 ......
 
In [23]: help(range)                #查看range的用法


In [24]: range(10)
Out[24]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [27]: range(1,11)
Out[27]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [28]: range(2,11,2)
Out[28]: [2, 4, 6, 8, 10]

import相当于bashell中的source

wKiom1THunzjvcEJAALw-V9HPzc864.jpg
wKiom1THunzjvcEJAALw-V9HPzc864.jpg
代码语言:javascript
复制
In [29]: import random                  #加载random模块
In [30]: random.random()                #随机生成一个随机数
Out[30]: 0.10932424859218137

In [32]: students=['a','b','c','d']      #定义一个列表 

In [33]: random.choice(students)         #从中随机选取一个参数
Out[33]: 'b'

In [34]: random.choice(students)
Out[34]: 'c'
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/06/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档