linux python

vim
dw 删除单词
d0 删除前面所有内容

ipython程序可以支持linux命令

注释

用“#”来表示单行注释
用“'''test'''”或“"""test"""”表示多行注释
python2默认不支持非英文编码,需要在文件开头加上#coding=utf-8或#-- coding:utf-8 --

变量

python2和python3中
input有区别
2:将键盘输出的字符当作代码执行
3:将键盘输入的字符当作字符串

变量类型

为了充分利用内存空间以及更有效率的管理内存,变量是有不同的类型的
input获取的数据都当作字符串类型 test--->"20"
可以通过强制类型转换进行不同类型之间的转换,如 age_num = int(age)

字符串切片###

[起始位置:终止位置:步长] name="12345678" name[2👎2] ==>'357' name[1::2] ==>'2468'
倒叙 name[-1::-1] > '87654321' name[::-1]>'87654321'

字符串常见操作###

myStr="hello world itcast and itcastxxxcpp"
myStr=find("world") >6
myStr=find("itcast")
>12 myStr=rfind("itcast") ==>23
myStr=index("3") >抛出异常 myStr=rindex("itcast")>23
myStr=count("hello") ==>1
myStr=count("itcast") ==>2

In [50]: myStr
Out[50]: 'hello world itcast and itcastxxxcpp'

In [51]: myStr.replace("itcast","xxx")
Out[51]: 'hello world xxx and xxxxxxcpp'

In [52]: myStr.replace("itcast","xxx",1)
Out[52]: 'hello world xxx and itcastxxxcpp'

myStr.split(" ")
Out[53]: ['hello', 'world', 'itcast', 'and', 'itcastxxxcpp']

字典

字典

In [18]: #info = {键:值,键:值}

In [19]: info = {"name":"班长", "addr":"山东.....", "age":18}

In [20]: print("%s %d %s"%(info["name"], info["age"] ,info["addr"]))
班长 18 山东.....

In [21]: info
Out[21]: {'age': 18, 'addr': '山东.....', 'name': '班长'}

In [1]: info = [{"name":"aa", "age":18},{"name":"bb", "ageOF":20}]

In [2]: info
Out[2]: [{'name': 'aa', 'age': 18}, {'ageOF': 20, 'name': 'bb'}]

In [3]: for temp in info:
...: print(temp)
...:
{'name': 'aa', 'age': 18}

In [4]: for temp in info:
print(temp["name"])
...:
aa
bb

In [13]: a = (11,22)

In [14]: b = a

In [15]: b
Out[15]: (11, 22)

In [16]: c,d = a

In [17]: c
Out[17]: 11

In [18]: d
Out[18]: 22

posted @ 2021-02-15 14:17  阿伦·艾弗森  阅读(181)  评论(0编辑  收藏  举报