iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python学习笔记(二)Python基
  • 383
分享到

Python学习笔记(二)Python基

学习笔记Python 2023-01-31 00:01:46 383人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

[root@kaibin ~]# ipython In [1]: import platfORM In [2]: print platform.uname() ('linux', 'kaibin.test1', '2.6.32-431.el

[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:

[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

变量赋值:

数字:
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

In [1]: name="Jack"        #字符串是不可变量

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

In [3]: iname="Jack"

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

变量规则

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

*区分字母大小写

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

     Python2Python3的保留字有所不同

命名惯例

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

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

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

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

In [1]: 1+1
Out[1]: 2

In [2]: print _

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

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常用的数据类型

序列类型

    *列表:使用[]创建

In [11]: a1=["This","is","a","Pig"]
In [12]: a1[0]                    #引用对象
Out[12]: 'This'
In [13]: a1[0][0]
Out[13]: 'T'

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

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'

列表和元组的区别

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

    *字符串也属于序列类型

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()测量

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进行;

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()

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结构一个逗号分隔的对象列表,并为行尾自动添加一个换行符,如果不需要,则在最后个元素后添加逗号

In [19]: a="Jack"

In [20]: b="Tom"

In [21]: print a,b
Jack Tom

print的格式化输出:

wKioL1TFkTqgPP0QAAEKwGUKPfg178.jpg

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

数据类型的转换:

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

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

In [39]: dir(__builtins__)
Out[38]: 
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BufferError',
 'BytesWarning',
 'DeprecationWarning',
 .....

查看函数用法

In [39]: help(str)

In [40]: name="jack"

In [41]: name.upper()
Out[41]: 'JACK'

wKiom1TFmxzSn59JAAML3OGecUo047.jpg

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集合)

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

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

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'


--结束END--

本文标题: Python学习笔记(二)Python基

本文链接: https://www.lsjlt.com/news/182960.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python学习笔记(二)Python基
    [root@kaibin ~]# ipython In [1]: import platform In [2]: print platform.uname() ('Linux', 'kaibin.test1', '2.6.32-431.el...
    99+
    2023-01-31
    学习笔记 Python
  • Python学习笔记(二)
    学完了基础中的基础后,我们准备深入基础中的函数、类和对象。 function函数: 正如英文单词描述的,函数就是“功能”的意思,把完成一个功能所需要的代码打包起来放在一个函数下可以方便以后程序的重复调用,也能使整体代码条理清晰。正如前...
    99+
    2023-01-30
    学习笔记 Python
  • python scrapy学习笔记(二)
    使用scrapy批量抓取,参考http://python.jobbole.com/87155一、创建项目# scrapy startproject comics创建完成后的目录结构. ├── comics │   ├── __init__....
    99+
    2023-01-31
    学习笔记 python scrapy
  • Python学习基础笔记(全)
    换博客了,还是csdn好一些。 Python学习基础笔记 1.Python学习—linux下Python3的安装 2.Python学习—数据类型、运算符、条件语句 3.Python学习—循环语句 4.Python学习—字符串 5.Pyt...
    99+
    2023-01-31
    基础 笔记 Python
  • python学习笔记01-基础
    数据类型:(1)整数(2)浮点数整数和浮点数在计算机内部存储的方式是不同的(3)字符串字符\本身也要转义,所以\表示的字符就是\Python还允许用r''表示''内部的字符串默认不转义(4)布尔值在Python中,可以直接用True、F...
    99+
    2023-01-31
    学习笔记 基础 python
  • Python第二周 学习笔记(3)
    1.运用数组实现求10万以内质数: prime = [2] for i in range(3,100000,2): flag = False up = int(i**0.5)+1 for j in prime: ...
    99+
    2023-01-31
    学习笔记 Python
  • Python学习笔记(二):使用Pyth
    1.目的: 2.安装XlsxWriter 3.xlsxwriter常用功能: 4.在Excel中写数据: 4.1 一维表格生成 1.目的: 用xlwt来生成excel的,生成的后缀名为xls,在x...
    99+
    2023-01-31
    学习笔记 Python Pyth
  • Python学习笔记整理(二)pytho
    一、Python的数字类型 1、数字常量 python数字类型在程序中如何显示(换句话说,作为常量) 数字            常量 1234,-23,0        一般整数 9999...
    99+
    2023-01-31
    学习笔记 Python pytho
  • python学习笔记字符串(二)
    字符串类型(string)字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"123"等等。请注意,''或""本身只是一种表示方式,不是字符串的一部分,因此,字符串'abc'只有a,b,c这3个字符。如果'本身也是一个字符,那就...
    99+
    2023-01-31
    字符串 学习笔记 python
  • Python学习笔记—第二章—IPyth
    书目:Python(Unix和Linux系统管理指南)学习日期:20130807知识点总结:安装IPython:源码安装:tar zxvf ipython-0.13.2.tar.gz python setup.py install...
    99+
    2023-01-31
    第二章 学习笔记 Python
  • python基础概念学习笔记
    本次整理python数据类型为:列表list字典dict元组tuple集合set其中涉及概念:模组module类class对象object——————————————————————————————————————————数据类型type ...
    99+
    2023-01-31
    学习笔记 概念 基础
  • Python学习笔记整理(十二)Pyth
    一、函数基础 函数可以计算出一个返回值。作用:最大化代码重用,最小化代码冗余,流程的分解 1、函数相关的语句和表达式 语句        例子 Calls        myfunc(‘diege',...
    99+
    2023-01-31
    学习笔记 Python Pyth
  • Python学习笔记
    Python介绍 Python是一种解释型、面向对象的语言。 官网:www.python.org Python环境 解释器:www.python.org/downloads 运行方式: 交互模式。在IDLE中运行。 脚本模式。文件的后缀...
    99+
    2023-01-30
    学习笔记 Python
  • Python 学习笔记
    rs=Person.objects.all() all返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使用for循环可以获取数据。 print rs.query 会打印出原生sql语句 rs=Pe...
    99+
    2023-01-31
    学习笔记 Python
  • python学习笔记--趣学Python
    由反弹球和球拍构成的游戏。球会在屏幕上飞过来,玩家要用球拍把它弹回去 画布和画弹球 引入模块 #Tkinter -- Python的标准GUI库,Tk 接口,是python 内置的安装包 from tkinter import * i...
    99+
    2023-01-31
    学习笔记 python Python
  • Python学习笔记五(Python
    Python urllib模块提供了一个从指定的URL地址获取网页数据,然后对其进行分析处理,获取想要的数据。1.查看urllib模块提供的urlopen函数。help(urllib.urlopen) urlopen(url, data...
    99+
    2023-01-31
    学习笔记 Python
  • Python学习笔记四(Python
    Python os模块提供了一个统一的操作系统接口函数,通过python os模块可以实现对系统本身的命令,文件,目录进行操作,官方参考文档( http://docs.python.org/library/os)。1)os.sep 可以...
    99+
    2023-01-31
    学习笔记 Python
  • 【Python学习笔记】-Python中
    python中的格式为 为真时的结果 if 判定条件 else 为假时的结果 实例: print(1 if 5>3 else 0) 是先输出结果,再判定条件 输出1,如果5大于3,否则输出0 一般用于判断赋值中,例...
    99+
    2023-01-31
    学习笔记 Python
  • python 学习笔记第二章:安装pyt
       记得上次写python的学习笔记是三个月以前了,期间看过,也放下过,这次要坚持下来,一鼓作气,吃下python。    本文内容主要是根据《Python 简明教程》和自己的一些理解去写的,有问题,还请指出。   一、Linux 下安装...
    99+
    2023-01-31
    第二章 学习笔记 python
  • python——Matplotlib学习笔记
      Matplotlib是pyhon中一个强大的绘图图,可以理解为 MatLab 开源替代,鉴于MatLab的内存之大及安装之复杂,决定先学学Matplotlib这个库。  1Matplotlib的安装  window:  打开cmd,: ...
    99+
    2023-06-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作