Python初学笔记——琐碎知识,based on 2.7.3,持续更新中

--以下内容基本完全来源于 google 开发人员 教学材料,https://developers.google.com/edu/python?hl=zh-CN


--Python Set Up and Introduction


1,语句以断行结束,不推荐“;”结束,尽管“;”是可以的。


2,#表示注释,从标识位置开始一直到段末。


3,语句块不以花括号表示,使用缩进表示。


4,函数书写格式:

foo(para1, para2, ……):#注意这个冒号
[缩进][satement1]
[缩进][satement2]
[缩进]if var:#注意这个冒号
[缩进][缩进][statement]

5,运行时检查
·直到运行至这一行时,才检查这一行。这意味着,即便程序有错,如果这段代码没有得到运行,那么程序本身看起来是对的。

6,true无法识别,只能用 True 表示 真

7,每个python文件成为一个module,如 binky.py contains a def foo(),那么就可以使用 binky.foo()。另外一个例子,import sys 这个sys模块,就可以使用 sys.argv[1],sys.exit(0) 等。其他 python standard library,如
sys -- access to exit(), argv, stdin, stdout, ...
re -- regular expressions
os -- operating system interface, file system
……

--String

8,可以使用 help() 来获得帮助,括号内为 模块名,模块名.变量名/函数名-不加括号 等形式。

9,在 python 命令行中,如果使用 PSL 中的函数,也需要先 import。

10,python有一个string的内嵌类,通常写作 str,老得模块为 string。
使用 单引号(常用)或双引号。
可以使用 \ 对 ‘、 “ 进行转义,如 \',\"。
单引号中可以使用成对的双引号,双引号中可以使用成对的单引号。
string 常量可以断行写,但是上一行要以 \ 结尾。
python的 string 是 immutable 的,也就是说创建后就不能改变。类似于 Java 的 string。
string 中的字符,可以使用 [] 下标进行索引。如果索引越界,python 会提示错误。

11,python 如果没有指定如何做,会终止执行,不像 perl 为指定一个默认值。

12,[] 和 len() 实际上在任何序列类型(sequence type)上都可以使用,如 strings, lists, etc..

13,与 Java 不一样,+ 不能将数字的等转义为字符串,如 s=51  s1 = 'abcd' + s,出错,需要使用 str() 转换为 string。

14,对于数字,没有 ++,-- 操作符,但是有 +=,-= 等。

15,除法,使用 //,得到商,如 10//3 is 3,以前使用的是 /。

16,使用三个双引号或三个单引号,表示是 text,其间可以换行。

17, 常用 string 方法:
s.lower(), s.upper()    #returns the lowercase or uppercase version of the string
s.strip()    #returns a string with whitespace removed from the start and end
s.isalpha()/s.isdigit()/s.isspace()...    #tests if all the string chars are in the various character classes
s.startswith('other'), s.endswith('other')     #tests if the string starts or ends with the given other string
s.find('other')  #searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found
s.replace('old', 'new')    #returns a string where all occurrences of 'old' have been replaced by 'new'
s.split('delim')    #returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
s.join(list)    #opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. '---'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc



18,python 中没有 char 类型,直接使用 长度为 1 的 string即可。

19,string slices,s[start:end],表示 从 索引 start 开始,持续到 end,但不包括end,截取 s 的子串。支持 string,list 等。
h     e    l       l    o
1     2    3     4    5
-5  -4   -3    -2   -1
如果 start,或者 end 为空,表示一直持续到末端。
如果都没有,表示对原串的拷贝。
如果越界,则只会持续到末端。
如果是负数,则如上表所示,等同于正数形式。另外,可以单独使用 s[-4] 来索引某个字符 ‘e’。

20,可以像 printf 中的格式化输出一样使用 %,如 %d,%s,%f 等。
text = %s is %, and I love it. %d % ('today', 'sunny', 5)    
# 注意括号前的最后一个 %。
# 括号里面以逗号分隔,字符串需要加引号。
# 如果需要换行,可以在最后一个 % 后换行,同时,等号右边整个要加括号,如 (), [], {}  

21,python 默认不使用 Unicode 编码如果使用 unicode,需在字符串前加前缀 u,u’hello world‘

22,前缀 r 表示将字符串中的反斜线 \ 视作一个字符,而不是转义。

书签:if statement



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值