Learning Python


Python是一种 解释型、面向对象、动态数据类型的高级程序设计语言。Python最具特色的就是 使用缩进来表示代码块。缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。

如何查看Python的版本和安装位置

  • 可以通过如下的方法查看Python的版本
python -V

或者

python --version
  • 可以通过如下的方法查看Python的安装位置
which python

或者

python -c "import sys; print sys.executable"

或者

python -c "import os; print os.sys.executable"

如何查看Numpy的版本和安装位置

  • 可以通过如下的方法查看Numpy的版本
python -c "import numpy; print numpy.version.version"

或者

python -c "import numpy; print numpy.__version__"
  • 可以通过如下的方法查看Python的安装位置
python -c "import numpy; print numpy.__file__"

如何设置和查看PYTHONPATH

这是默认的Python模块搜索路径,它的格式和系统PATH是一样的。

  • 方法一:在命令窗口中添加路径
export PYTHONPATH=$PYTHONPATH:/home/someone

注意:此方法只在当前命令窗口生效,即如果打开一个新的Terminal 窗口,定位到当前目录,打印PYTHONPATH 是没有刚才加入的路径的.

  • 方法二:在Python脚本中添加路径
import sys
sys.path.append('/home/someone/')

可以使用下面的方法来查看路径是否正确。

import os
print sys.path

交互式编程

命令行中输入 Python 命令即可启动交互式编程

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

脚本式编程

将以下的源代码拷贝至test.py文件中。

print "Hello, Python!";

这里,假设你已经设置了Python解释器PATH变量。使用以下命令运行程序:

$ python test.py

输出结果:

Hello, Python!

让我们尝试另一种方式来执行Python脚本。修改test.py文件,如下所示:

#!/usr/bin/python

print "Hello, Python!";

这里,假定您的Python解释器在/usr/bin目录中,使用以下命令执行脚本:

$ chmod +x test.py     # 脚本文件添加可执行权限
$./test.py

输出结果:

Hello, Python!

Python字符串

  1. 使用单引号(’)
    你可以用单引号指示字符串,就如同’Quote me on this’这样。所有的空白,即空格和制表符都照原样保留。

  2. 使用双引号(")
    在双引号中的字符串与单引号中的字符串的使用完全相同,例如"What’s your name?"。

  3. 使用三引号(’’'或""")
    利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。

字符串查找

  1. find()方法:
info = 'abca'
print info.find('a')##从下标0开始,查找在字符串里第一个出现的子串,返回结果:0

info = 'abca'
print info.find('a',1)##从下标1开始,查找在字符串里第一个出现的子串:返回结果3

info = 'abca'
print info.find('333')##返回-1,查找不到返回-1
  1. index()方法:

python的index方法是在字符串里查找子串第一次出现的位置,类似字符串的find方法,不过比find方法更好的是,如果查找不到子串,会抛出异常,而不是返回-1

info = 'abca'
print info.index('a')
print info.index('33')

字符串翻转

通过步进反转[::-1]

a = 'abcd'
b = a[::-1]##[::-1]通过步进反转
print b

Python列表

List(列表) 是 Python 中使用最频繁的数据类型。列表用[ ]标识
列表中的值得分割也可以用到变量[头下标:尾下标],就可以截取相应的列表,从左到右索引默认0开始的,从右到左索引默认-1开始,下标可以为空表示取到头或尾。加号(+)是列表连接运算符,星号(*)是重复操作。

Python元组

元组是另一个数据类型,类似于List(列表)。元组用( )标识
元组内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。

Python字典

字典(dictionary) 用{ }标识。字典由索引(key)和它对应的值value组成。
两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。列表是有序的对象结合,字典是无序的对象集合。

参考资料

  1. Python 基础教程
  2. Python Cookbook
  3. A Byte of Python
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2016新书,和另一个Learning Python(最新第5版)不是同一个,书面相同 Paperback: 140 pages Publisher: Packt Publishing - ebooks Account (January 1, 2016) Language: English ISBN-10: 178528424X ISBN-13: 978-1785284243 Key Features Find out how to interact with Jenkins from within Eclipse, NetBeans, and IntelliJ IDEA Develop custom solutions that act upon Jenkins information in real time A step-by-step, practical guide to help you learn about extension points in existing plugins and how to build your own plugin Book Description Jenkins CI is the leading open source continuous integration server. It is written in Java and has a wealth of plugins to support the building and testing of virtually any project. Jenkins supports multiple Software Configuration Management tools such as Git, Subversion, and Mercurial. This book explores and explains the many extension points and customizations that Jenkins offers its users, and teaches you how to develop your own Jenkins extensions and plugins. First, you will learn how to adapt Jenkins and leverage its abilities to empower DevOps, Continuous Integration, Continuous Deployment, and Agile projects. Next, you will find out how to reduce the cost of modern software development, increase the quality of deliveries, and thereby reduce the time to market. We will also teach you how to create your own custom plugins using Extension points. Finally, we will show you how to combine everything you learned over the course of the book into one real-world scenario.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值