一、help
python中的帮助手册,对于初学者,多多使用help,多看看原生注释……
1、help的使用
1、命令需要使用双引号或者单引号括起来,不使用引号引起来会报错
2、类或者函数(方法)不需要
3、按q退出帮助
1 2 3 4 5 |
<span style="font-size: 16px;">In [1]: help(<span style="color: rgba(0, 0, 255, 1);">print</span><span style="color: rgba(0, 0, 0, 1);">) </span><span style="color: rgba(0, 0, 0, 1);"> In [</span>2]: help(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">print</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) In [</span>3]: help(input)</span> |
2、实例
In [5]: help(a.count) #查询a.count方法的使用
In [6]: help(a.index) #查询a.index方法的使用
In [7]: help(a.split) #查询a.split方法的使用
二、常见的字符串处理方法
1、a.isdigit() #判断a变量是否是数字
2、a.isalpha() #判断a变量是否是字母
3、a.isalnum #判断a变量是否是数字或者字母
4、a.split(“:”) #指定 : 作为分隔符(分隔符可以是任意字符)来分离a变量,缺省默认是空白
1 2 3 4 |
<span style="font-size: 16px;">In [8]: a=<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">xiaozhang@nan@22</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> In [</span>9]: a.split(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">@</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) Out[</span>9]: [<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">xiaozhang</span><span style="color: rgba(128, 0, 0, 1);">'</span>, <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">nan</span><span style="color: rgba(128, 0, 0, 1);">'</span>, <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">22</span><span style="color: rgba(128, 0, 0, 1);">'</span>]</span> |
5、a.lower() #将变量a转换为小写
6、a.upper() #将变量a转换为大写
7、a.count(“i”) #统计 i 字母在a变量里出现次数
8、a.index(“i”) #显示 i 字母在a变量里的序列下标(类似数组)
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 16px;">In [10]: zjc=<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">abcdefg</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> In [</span>11]: zjc.index(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">e</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) Out[</span>11]: 4<span style="color: rgba(0, 0, 0, 1);"> In [</span>12]: <span style="color: rgba(0, 0, 255, 1);">print</span><span style="color: rgba(0, 0, 0, 1);"> zjc abcdefg In [</span>13]: <span style="color: rgba(0, 0, 255, 1);">print</span> zjc[4<span style="color: rgba(0, 0, 0, 1);">] e</span></span> |
三、缩进(indented)
在Python中,行首的空白是非常重要的。
在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组(每一组这样的语句称为一个块)。同一层次的语句必须有相同的缩进。
缩进方案:
1、单个制表符(Tab键)-à推荐使用
2、两个空格、四个空格
四、常量
一个固定的值,不能改变的值
类型:数、字符串
1、数:5、1.23、67854
Python中有4种类型的数:整数、长整数、浮点数、复数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span style="font-size: 16px;">In [14]: a=123<span style="color: rgba(0, 0, 0, 1);"> In [</span>15<span style="color: rgba(0, 0, 0, 1);">]: type(a) Out[</span>15<span style="color: rgba(0, 0, 0, 1);">]: int In [</span>16]: b=111111111122222222222333333333333<span style="color: rgba(0, 0, 0, 1);"> In [</span>17<span style="color: rgba(0, 0, 0, 1);">]: type(b) Out[</span>17<span style="color: rgba(0, 0, 0, 1);">]: long In [</span>18]: c=1.23<span style="color: rgba(0, 0, 0, 1);"> In [</span>19<span style="color: rgba(0, 0, 0, 1);">]: type(c) Out[</span>19<span style="color: rgba(0, 0, 0, 1);">]: float In [</span>20]: d=<span style="color: rgba(0, 0, 0, 1);">True In [</span>21<span style="color: rgba(0, 0, 0, 1);">]: type(d) Out[21</span>]: bool</span> |
2、字符串:“This is a string”
字符串是字符的序列,基本上就是一组单词
1、引号:使用单引号(’)、使用双引号(”)
1 2 3 4 5 |
<span style="font-size: 16px;">In [22]: <span style="color: rgba(0, 0, 255, 1);">print</span> <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">hello world</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);"> hello world In [</span>23]: <span style="color: rgba(0, 0, 255, 1);">print</span> <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">hello world</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> hello world</span></span> |
使用三引号(”’或”””) #一般会用在多行注释里
2、转义符
1 2 3 4 5 6 7 8 |
<span style="font-size: 16px;">In [24]: <span style="color: rgba(0, 0, 255, 1);">print</span> <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">hellotworld!!</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> hello world!! In [</span>25]: <span style="color: rgba(0, 0, 255, 1);">print</span> r<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">hellotworld!!</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> hellotworld!! In [</span>26]: <span style="color: rgba(0, 0, 255, 1);">print</span> <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">hello\tworld!!</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> hellotworld!!</span></span> |
注意:
1、raw功能的作用:让t失去代表tab键的功能,返回到原始(原生)的字符
2、多加一个让后面的反斜杆失去意义
字符串中,行末反斜杆是续行作用。
五、变量
变量的值可以改变,可以使用变量存储任何东西
变量标识符的命名规则:
1、标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘ _ ’)。
2、标识符名称的其他部分可以由字母(大写或小写)、下划线(‘ _ ’)或数字(0-9)组成。
3、标识符名称区分大小写,例如:myname和myName不是一个标识符。
有效标识符名称示例:i、__my_name、name_23和a1b2_c3。
无效标识符名称示例:2things、this is spaced out和my-name。