显示标签为“python学习步骤”的博文。显示所有博文
显示标签为“python学习步骤”的博文。显示所有博文

2009/04/18

python学习2

本篇介绍一些定义

常量

例 

5、1.23、9.25e-3,  'This is a string'、"It's a string!"

这样的固定的字符串。因为不能改变它的值。


在Python中有4种类型的数——整数、长整数、浮点数和复数。 

●     2  整数的例子。 

●     长整数不过是大一些的整数。 

●     3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。 

●     (-5+4j)和(2.3-4.6j)是复数的例子。 


字符串 

用""或者''定义。例如:'中国'  就是一个字符串


包含单引符的情况,比如what's your name?的时候。这样写"What's your name?",即用双引号括下单引号。要在 双引号字符串中使用双引号本身的时候,可以借助于转义符\"

例:"what's your name?\"my name is linjie\""


或者三引号'''。例如:'''what's your name ?"my name is linjie."'''

另外,你可 以用转义符 \\来指示反斜杠本身。 


"This is the first sentence.\ 

This is the second sentence." 

等价于"This is the first sentence. This is the second sentence." 


(还有一些暂不说,以后遇到再说。原教程先说这些却用不到等于白说)


下面来几个最直白的例子


例2

#!/usr/bin/python 

i = 5 

print i 

i = i + 1 

print i 

s = '''This is a multi-line string. 

This is the second line.''' 

print s 


(如果你现在还是迷惑这段代码写在哪里,那么跟我当初一样。这样写在新打开的bash里面:
cd projs
vim var.py
i
#!/usr/bin/python
i=5
print i
i=i+1
print i
s='''This is a multi-line string.
This is the second line.'''
print s

这个时候按键盘上的Esc键,接着输入

:w
:!python var.py


ok!计算机开始跑啦~~

运行结果:

This is a multi-line string. 

This is the second line. 


现在我插一个例子说明一个小符号的用法:


例:

s='This is a string.\

This continues the string.'

print s


运行后输出:


This is a string.This continues the string.


就是说\符号其实就是换行的


ok,说了很多,到python解释器里面转一会。


在bash里面输入  python


然后输入


>>>2+3

5

>>>3*5

15

>>>


其实就是运算的东东~~加减乘除的玩意儿~~


5+5 得到 10

'a' +'b' 得到 'ab'

2*3=6

'la'*3+'lalala'

3**4=81(即3*3*3*3)

4/3=1(因为是整数)

4.0/3.0=1.3333333333333(数不清多少个3)

4//3.0=1.0(取整除返回商)

-25.5%2.25=1.5(取余)


其他还有好多暂时用不上说了也是忘的定义。遇到再说,一说就记住了~`哈哈)


例:


#!/usr/bin/python 

# Filename: expression.py 

length = 5 

breadth = 2 

area = length * breadth 

print 'Area is', area 

print 'Perimeter is', 2 * (length + breadth) 


运行结果:

Area is 10 

Perimeter is 14




if、for和while语句 


if语句 


例 使用if语句 

#!/usr/bin/python 

# Filename: if.py 

number = 23 

guess = int(raw_input('Enter an integer : ')) 

if guess == number: 

    print 'Congratulations, you guessed it.' # New block starts here 

    print "(but you do not win any prizes!)" # New block ends here 

elif guess <>

    print 'No, it is a little higher than that' # Another block 

    # You can do whatever you want in a block ... 

else: 

    print 'No, it is a little lower than that' 

    # you must have guess > number to reach here 

print 'Done' 

# This last statement is always executed, after the if statement is exe- 

cuted 


输出:

Enter an integer : 50 

No, it is a little lower than that 

Done 


while语句

#!/usr/bin/python 

# Filename: while.py 

number = 23 

running = True 

while running: 

    guess = int(raw_input('Enter an integer : ')) 

    if guess == number: 

        print 'Congratulations, you guessed it.' 

        running = False # this causes the while loop to stop 

    elif guess <>

        print 'No, it is a little higher than that' 

    else: 

        print 'No, it is a little lower than that' 

else: 

    print 'The while loop is over.' 

    # Do anything else you want to do here 

print 'Done' 


输出:

Enter an integer : 50 

No, it is a little lower than that. 

Enter an integer : 22 

No, it is a little higher than that. 

Enter an integer : 23 

Congratulations, you guessed it. 

The while loop is over. 

Done 


两个例子都是猜,if就不如while好。只要你试了!!

补一句,学这玩意我头很疼~~

2009/04/16

python学习经验

本教程是自己根据Swaroop, C. H. 著沈洁元  译 www.byteofpython.info 版本:1.20 A Byte of Python 

Copyright © 2003-2005 Swaroop C H 简明 Python 教程 《简明 Python 教程》为 "A Byte of Python" 的唯一指定简体中文译本,版 © 2005 沈洁元 》这个教程学习过程自我总结而来,以后自己写  :D



学习python前,自己最好建立一个文件夹,专门存放py语句的文件夹。


比如在finder下面建立一个projs文件夹。下文所有的文件我自己都是存放在finder/projs下面~


>>>   是Python语句的提示符,不必关注~ :)


例1 使用带提示符的Python解释器〔在bash里面输入〕 


$ python   *不要写$这个符号!


输出:

Python 2.3.4 (#1, Oct 26 2004, 16:42:40) 


[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2 


Type "help", "copyright", "credits" or "license" for more information. 


*说明你的电脑里面已经安装有了python程序


>>> print 'hello world' 


hello world 


按Ctrl-d可以退出提示符,回到bash


在bash里面输入


cd 〔filename〕 *变更默认读写存储文件夹change 


vim 〔filename.py〕 *建立**.py文件并呈写入状态 


例:


cd projs *更改默认文件夹


vim hellowold.py *建立名为helloword.py的程序


i                          *代表insert,写入


#!/usr/bin/python *它们被称作注释,这一行必不可少。任何在#符号右面的内容都是注释。 


# Filename : helloworld.py *可有可无~~:D


print 'Hello World' 


:w   *写入保存〔write〕


:!python helloworld.py *运行该文件 


也可以在bash里面直接输入:


python helloworld.py


输出 


Hello World 


万一你得到一个错误,那么请确保你键入的程序准确无误 ,然后再运行一下程序。

注意Python是大小写敏感的,即print与Print不一样,确保在每一行的开始字符前没有空格或者制表符。


通过chmod命令给程序可执行许可〔在bash里面输入〕


$ chmod a+x helloworld.py 


$ ./helloworld.py 


输出


Hello World 


我们使用./来指示程序位于当前目录。你也可以把你的文件名改成仅仅helloworld,然后

运行./helloworld。只要知道程序的确切位置,你现在就可以运行程序了。


但是如果你希望你的程序能够从各个位置运行呢?那样的话,你可以把你的程序保存在

PATH环境变量中的目录之一。你只要简单地把这个源文件复制到PATH所列目录之一就

可以使你的程序在任何位置都可用了。 


$ echo $PATH 


/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin


$ cp helloworld.py /home/usr/bin/helloworld 


$ helloworld 


Hello World 


用echo命令来显示PATH变量,用$给变量名加前缀以向shell表示我们需要这个变量的值。usr是我的系统中使用的用户名。通常在你的系统中也会有一个相似的目录。


你也可以把你选择的目录添加到PATH变量中去


这可以通过运行PATH=$PATH:/home/swaroop/mydir完成,其中“/home/swaroop/mydir”是我想要添加到PATH变量中的目录。


当你想要在任何时间、任何地方运行你的程序的时候,这个方法十分有用。 


它就好像创造你自己的指令,如同cd或其他Linux终端或DOS提示符命令那样提示 


获取帮助 


如果你需要某个Python函数或语句的快速信息帮助,那么你可以使用内建的 


help功能。尤其在你使用带提示符的命令行的时候,它十分有用。比如,运行help

(str)——这会显示str类的帮 助。str类用于保存你的程序使用的各种文本(字符串)类将

在后面 面向对象编程的章节详细解释。 


注释 


按q退出帮助。 




类似地,你可以获取Python中几乎所有东西的信息。使用help()去学习更多 


关于help本身的东西!


如果你想要获取关于如print那样操作符的帮助,那么你需要正确的设置 


PYTHONDOCS环境变量。这可以在Linux/Unix中轻松地通过env命令完成。 


$ env PYTHONDOCS=/usr/share/doc/python-docs-2.3.4/html/ python 


Python 2.3.4 (#1, Oct 26 2004, 16:42:40) 


[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2 


Type "help", "copyright", "credits" or "license" for more information. 


>>> help('print') 


你应该注意到我特意在“print”上使用了引号,那样Python就可以理解我是希望获取关 


于“print”的帮助而不是想要它打印东西。 

2009/03/21

python问题解决之 2

在terminal书写python时候。废话不能写多了。

打开terminal

python

(运行的什么我也不知道,但是进入了python界面。在这里很多东西不能正常运行)

control+d 

(退回到bash界面)

cd projs

(确定根目录)

vim abc.py

(打开abc.py文件.如果在此之前你没有确定根目录。或者你的根目录里面没有abc.py文件。那么vim abc.py就是新建了一个abc.py的文件)

[注意一个问题,初学者容易忘记。比如我就是。

所有的vim命令里打开写代码的时候,命令行的开头要确保有这样的语

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

 我不知道Filename:helloworld.py有什么作用。

但是这两行在python里面却不可少。就像要先穿衣服再出门。]

i

insert的意思,输入代码。例如下面的例子。一个完整的小程序运行的示例。

cd projs

vim abc.py

i

#!/usr/bin/python
#Filename:helloworld.py
s = 'This is a string.'
print s
i = 3
print i
i=i*9
print i

Esc

:!python abc.py

return

:wq

示例完毕

我在vim里面写代码,光标不能自由移动。只能通过上下左右键盘单个字符的移动。

现在还没有解决这个问题。

在代码写完之后,可以不要退出,直接输入

:!python abc.py

可以检查命令的运行结果,回车键返回。

输入完毕后就

:wq

保存退出

2009/03/20

初次接触学习python问题解决 1

测试Python


打开shell程序(就类似Windows的 Dock或者Mac osx的terminal等终端程序)


输入    python  回车


terminal显示下列信息:


Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) 

[GCC 4.0.1 (Apple Inc. build 5465)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> 


那么,计算机里面已经有python程序了。

3.正式写python代码之前

在terminal里面写下面的东西。要是你不能明白就跳过黑体字。继续看下面。


mkdir projs            建立新的文件夹


cd projs                更改默认文件夹为目标文件夹


vim abc.py           预先设定好下面要写的代码的文件名称

 

i                        代表开始编写


输入代码完毕后  


Esc

:wq                    代表保存文件


python  abc.py     表示运行刚才写的代码


在终端(Dock或者terminal命令框)里输入 mkdir pythonfiel   回车

(注释:mkdir是make directory的意思。新建一个文件夹 pythonfiel 在 Finder里面。)

因为terminal在找文件的时候只从它默认的文件夹里找文件,所以前提是必须先确定好自己放所有在学习python时文件的存储位置。

(注释:终端里面开始的 $ 符号前面的 ~ 符号代表terminal的默认文件夹的地址。)

输入    cd pythonfiel  回车

(注释:cd 是change directory的意思。换掉默认文件夹到你刚才建的文件夹下)

输入  open   回车

(可以编辑文件夹)

4.写python语言代码。

python语言可以在任何一个编辑器里面写。记事本可以,word也可以。总之只要能写字的地方都可以。但是

在保存你写好的文件是注意一下:  文件扩展名必须为   .py   格式。。文件地址必须保存到 pythonfiel 文件夹下面,就是你自己建的那个。

比如写下面的代码:

#!/usr/bin/python 

# Filename : helloworld.py 

print 'Hello World' 


 (注释:教程里面的每一行分段,在terminal里面好像不行。就用空格分开就可以了。遇到 $ 符号是忽略不写。

$是shell的提示符。还有<<<   符号也不写。不是代码。是python的注释    )