python学习

 

 

...
   Programing is fun
   when the work is done 
   if you wanna make you work also fun:
   use python
...

基础介绍:

http://woodpecker.org.cn/abyteofpython_cn/chinese/ch02.html

自学教程

http://www.python.org/download/

\\pathon官方网站

http://coolshell.cn/articles/4990.html

程序猿练级道路

pathon:

pathon 大小写敏感。

 

pathon2.0:     print’hello world!’

pathon3.0:     print(‘hello world!’)或者print(“hello world!”)

#!/usr/bin/python

Python至少应当有第一行那样的特殊形式的注释。它被称作 组织行 ——源文件的头两个字符是#!,后面跟着一个程序。这行告诉你的Linux/Unix系统当你 执行 你的程序的时候,它应该运行哪个解释器。

Liunx下的操作:

$ chmod a+x helloworld.py \\chmod 改变文档的操作权限
$ ./helloworld.py
Hello World

 

PATH=$PATH:/home/swaroop/mydir  \\liunx下添加path变量

$ env PYTHONDOCS=/usr/share/doc/python-docs-2.3.4/html/ python \\想要获取关于如print那样操作符的帮助,那么你需要正确的设置PYTHONDOCS环境变量。

运用cmd需要注意需要cd要目录才可以用python  *.py

字符串:

‘’   ,   “  ” ,   ‘’’   ‘’’

‘’   与  “  ”的意义相同,如比较:

print(“what’s your name?”)  what’s your name?

print(‘what”s your name?’)  what”s your name?

三引号是为了方便引用单引号和双引号。

转义符 :  \

反转义在字符串前加 Rr。这个也叫自然字符串。可以使某些正则表达式表示变的方便。

 

 

Unicode字符串:字符串前加上前缀uU

按字面意义级连字符串

如果你把两个字符串按字面意义相邻放着,他们会被Python自动级连。例如,'What\'s' 'your name?'会被自动转为"What'syour name?"。

 

一般不在pathon 语言中是用分号;是为了让程序跟了然。

在多个物理行编写一个逻辑行用转义符号连接如:

print  \

ii

print ii 效果是一样的。

 

缩进会引发错误!

       print(“ds”)   #error 错误缩进

print(‘ddf’)

同一层次的语句必须有相同的缩进。

 

 

基本控制语句:

例:已知某数X的原码为10110100B,试求X的补码和反码?

解: 有原码可知:x为负数。

其反码为:11001011b

补码为:  11001100b

 

补码把减法可以有效的转换为加法。

 

**

 

| 按位或

&按位与

// 取整余

22//9.4
#2.0

  

^按位异或

~ 按位翻转 -(x+1)

or 相当于 c中的 &&

and 相当于c中的 | |

not 相当于c中的 

 

循环语句:

Guess=int(raw_input(‘Enter an integer:’))#python3.2标准已经没有这个函数了,应该使用input.
if

if elif else 相当于两 if- else : if-else

 

for :

 

while:

真值:True #注意区分大小写。

假值:False

 

 

 

 

break:

 

注意缩进。

continue:

 

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

while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
        break
    if len(s) < 3:
        continue
    print 'Input is of sufficient length'
    # Do other kinds of processing here...

 

在这个程序中,我们从用户处取得输入,但是我们仅仅当它们有至少3个字符长的时候才处理它们。所以,我们使用内建的len函数来取得长度。如果长度小于3,我们将使用continue语句忽略块中的剩余的语句。否则,这个循环中的剩余语句将被执行,我们可以在这里做我们希望的任何处理。

注意,continue语句对于for循环也有效。

 

 

 

 


 

posted @ 2013-08-08 16:13  UCanBeFree  阅读(359)  评论(0)    收藏  举报