Python学习日志(不断更新)

上一篇 / 下一篇  2011-02-05 13:10:53 / 个人分类:Python

前两天安装好了Fedora12,接下来开始在Linux上来学习Python。参考书籍《Python基础教程(第2版)》

python中除法默认是整除,如1/2等于0,如果要改变这种特性,方法如下:
方法一:
[hxc@localhost ~]$ python -Qnew
方法二:进入python后执行from __future__ import division(注:future前后都是两个下划线)

在命令行直接运行python程序
在程序文件中加入#!/usr/bin/env python。例如:
[hxc@localhost ~]$ vi hello.py (内容如下:)
#!/usr/bin/env python
name = raw_input("What is your name?")
print "Hello, "+name+"!"

(保存退出)
[hxc@localhost ~]$ chmod a+x hello.py
[hxc@localhost ~]$ ./hello.py

字符串:
>>> "Hello, World!"
'Hello, World!'
>>>
>>> 'Hello, World!'
'Hello, World!'
>>> "Let's go!"
"Let's go!"
>>> 'Let's go!'
  File "<stdin>", line 1
    'Let's go!'
         ^
SyntaxError: invalid syntax
>>> 'Let\'s go!'
"Let's go!"
>>>
str函数、repr函数与反引号(不是单引号)
>>> temp=42
>>> print "The temperature is "+temp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is "+str(temp)
The temperature is 42
>>> print "The temperature is "+temp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is "+'temp'
The temperature is temp
>>> print "The temperature is "+`temp`
The temperature is 42
>>> print repr(100000L)
100000L
>>> print str(100000L)
100000
>>> print str("Hello, world!")
Hello, world!
>>> print repr("Hello, world!")
'Hello, world!'

原始字符串
>>> path="C:\there"
>>> print path
C:    here
>>> path=r"C:\there"
>>> print path
C:\there
>>> path=r"C:\there\"
  File "<stdin>", line 1
    path=r"C:\there\"
                    ^
SyntaxError: EOL while scanning string literal
>>> path=r"C:\there\\"
>>> path=r"C:\there\\"
>>> path=r"C:\there" "\\"
>>> print path
C:\there\
>>>





TAG: Fedora Python python

花满路的个人空间 引用 删除 花满路   /   2011-03-04 21:16:39
马上也需要了 希望能继续更新
花满路的个人空间 引用 删除 花满路   /   2011-03-04 21:16:03
5
 

评分:0

我来说两句

日历

« 2024-04-26  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 28919
  • 日志数: 24
  • 建立时间: 2010-07-07
  • 更新时间: 2011-02-11

RSS订阅

Open Toolbar