您的位置:首页 > 编程语言 > Python开发

python 脚本学习记录(1)

2014-03-27 15:39 344 查看
Python是一种代表简单主义思想的语言。阅读一个良好的Python程序就感觉像是在读英语一样,尽管这个英语的要求非常严格!Python的这种伪代码本质是它最大的优点之一。它使你能够专注于解决问题而不是去搞明白语言本身。

python的强大已经不言而喻,我之前自学过但没用忘得差不多了,难得现在有空再来学一遍。

Windows下安装了python3.1 IDLE.

1.print('hello world'); #分号已经可有可无,回车是主要判断语句结束符

2.每个脚本最好之前写有第一句

#!/bin/python

#coding=gb10830 (若有中文格式的时候加这句)

3.python没有多行注释,但有多行字符串(三引号)"""和‘’‘等价

“”“

ABC

这里放注释

"""

4.缩进有讲究,建议每一层次统一用一个tab进行缩进

5交互式验证:我们可以在命令行中运行python语句,不过windows要加环境变量

6.and or not 取代了&& || !

7.print("%s=%d" %(a,b))

8.输入:str=input(’请输入数字‘)

9.定义函数

#!/usr/bin/python

# Filename:function1.py


def
sayHello
():


print
'Hello
World!'
# block belonging to the function


sayHello()
# call the function


10.运行函数


$python function1.py

Hello World!


11.x是函数的局部变量。所以,当我们在函数内改变x的值的时候,在主块中定义的x不受影响。


12.你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。我们使用global声明
完成这一功能。没有
global
语句,是不可能为定义在函数外的变量赋值的。

13.关键参数


def
func
(a,
b=
5
,c=
10
):


print
(a is'
,a,
'and
b is'
,b,
'and c is'
,c)


func([/code]
3
,
7
)


func(
25
,c=
24
)


func(c=
50
,a=
100
)


14
pass 表示空语句块


15
DocStrings

16.模块

#!/usr/bin/python
# Filename:using_sys.py

importsys

print'The command line arguments are:'
fori insys.argv:
printi

print'\n\nThe PYTHONPATH is',sys.path,'\n'
sys.argv
变量是一个字符串的 列表 (列表会在后面的章节详细解释)。特别地,
sys.argv
包含了 命令行参数 的列表,即使用命令行传递给你的程序的参数。

当我们执行
python using_sys.py we are arguments
的时候,我们使用python命令运行
using_sys.py
模块,后面跟着的内容被作为参数传递给程序。Python为我们把它存储在
sys.argv
变量中。

记住,脚本的名称总是
sys.argv
列表的第一个参数。所以,在这里,
'using_sys.py'
sys.argv[0]
'we'
sys.argv[1]


'are'
sys.argv[2]
以及
'arguments'
sys.argv[3]


注意,Python从0开始计数,而非从1开始。
17.字节编译的.pyc文件

输入一个模块相对来说是一个比较费时的事情,所以Python做了一些技巧,以便使输入模块更加快一些。一种方法是创建 字节编译的文件 ,这些文件以
.pyc
作为扩展名。字节编译的文件与Python变换程序的中间状态有关(是否还记得Python如何工作的介绍?)。当你在下次从别的程序输入这个模块的时候,
.pyc
文件是十分有用的——它会快得多,因为一部分输入模块所需的处理已经完成了。另外,这些字节编译的文件也是与平台无关的。

18.__name__

#!/usr/bin/python
# Filename:using_name.py

if__name__ == '__main__':
print'This program is being run by itself'
else:
print'I am being imported fromanother module'


每个Python模块都有它的
__name__
,如果它是
'__main__'
,这说明这个模块被用户单独运行,我们可以进行相应的恰当操作。

19.制造自己的模块(这个模块应该被放置在我们输入它的程序的同一个目录中,或者在
sys.path
所列目录之一。)

#!/usr/bin/python
# Filename:mymodule.py

defsayhi():
print'Hi,this is mymodulespeaking.'

version = '0.1'

# End of mymodule.py
使用模块
#!/usr/bin/python

# Filename:mymodule_demo.py


import
mymodule


mymodule.sayhi()


print
'Version'
,
mymodule.version


20、类似命名空间的用法

下面是一个使用
from..import
语法的版本。

#!/usr/bin/python

# Filename:mymodule_demo2.py


from
mymodule
import
sayhi,
version


# Alternative:

# frommymoduleimport*


sayhi()


print
'Version'
,
version

21.

dir()函数

你可以使用内建的
dir
函数来列出模块定义的标识符。标识符有函数、类和变量。

当你为
dir()
提供一个模块名的时候,它返回模块定义的名称列表。如果不提供参数,它返回当前模块中定义的名称列表。

22.数据结构:help(list) 可以获取完整知识
列表(中括号)(list,数组),len()求长度 append()增加元素
list
下标访问 sort()排序

元组(园括号)元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。

字典

#!/usr/bin/python

# Filename:using_dict.py

# 'ab' is short for'a'ddress'b'ook


ab = {
'Swaroop'
:
'swaroopch@byteofpython.info'
,


'Larry'
:
'larry@wall.org'
,


'Matsumoto'
:
'matz@ruby-lang.org'
,


'Spammer'
:
'spammer@hotmail.com'


}


print
"Swaroop's address is %s"
%
ab[
'Swaroop'
]


# Adding a key/value pair


ab[
'Guido'
]=
'guido@python.org'


# Deleting a key/value pair


del
ab[
'Spammer'
]


print
'\nThere are %d contacts inthe address-book\n'
%
len
(ab)


for
name,address 
in
ab.items():


print
'Contact %s at %s'
%
(name,address)


if
'Guido'
in
ab:
#
OR ab.has_key('Guido')


print
"\nGuido's address is %s"
%
ab[
'Guido'
]


序列

列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

#!/usr/bin/python

# Filename:seq.py


shoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]


# Indexing or 'Subscription' operation


print
'Item 0 is'
,
shoplist[
0
]


print
'Item 1 is'
,
shoplist[
1
]


print
'Item 2 is'
,
shoplist[
2
]


print
'Item 3 is'
,
shoplist[
3
]


print
'Item -1 is'
,
shoplist[
-1
]


print
'Item -2 is'
,
shoplist[
-2
]


# Slicing on a list


print
'Item 1 to 3 is'
,
shoplist[
1
:
3
]


print
'Item 2 to end is'
,
shoplist[
2
:
]


print
'Item 1 to -1 is'
,
shoplist[
1
:
-1
]


print
'Item start to end is'
,
shoplist[:]


# Slicing on a string


name =
'swaroop'


print
'characters 1 to 3 is'
,
name[
1
:
3
]


print
'characters 2 to end is'
,
name[
2
:]


print
'characters 1 to -1 is'
,
name[
1
:
-1
]


print
'characters start to end is'
,
name[:]

输出

$ python seq.py

Item 0 is apple

Item 1 is mango

Item 2 is carrot

Item 3 is banana

Item -1 is banana

Item -2 is carrot

Item 1 to 3 is ['mango','carrot']

Item 2 to end is ['carrot','banana']

Item 1 to -1 is ['mango','carrot']

Item start to end is ['apple','mango','carrot','banana']

characters 1 to 3 is wa

characters 2 to end is aroop

characters 1 to -1 is waroo

characters start to end is swaroop


mylist
= shoplist 指针而已

mylist
= shoplist[:]
# make a copy by doing a full slice
深复制


del
mylist[
0
]
#
remove first item


23.字符串方法:




#!/usr/bin/python

# Filename:str_methods.py


name =
'Swaroop'
#
This is a string object


if
name.startswith(
'Swa'
):


print
'Yes,the string starts with "Swa"'


if
'a'
in
name:


print
'Yes,it contains the string "a"'


if
name.find(
'war'
)
!=
-1
:


print
'Yes,it contains the string "war"'


delimiter =
'_*_'


mylist = [
'Brazil'
,
'Russia'
,
'India'
,
'China'
]


print
delimiter.join(mylist)


(源文件:code/str_methods.py

输出

$ python str_methods.py

Yes,the string starts with "Swa"

Yes,it contains the string "a"

Yes,it contains the string "war"

Brazil_*_Russia_*_India_*_China
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: