阅读目录:
1、常见Python编辑器介绍
1、在linux下使用vim
编辑Python脚本时,推荐……没有道理……
2、Eclipse
3、notepad++:挺好用的一款编辑工具
4、IDLE (官方自带)交互式的开发语言集成环境
5、pycharm 第三方的工具:ipython
学习、测试等常用ipython,很方便的编辑工具
……
Q:ipython和python的区别?
A:
ipython集成了部分shell的功能,可以之间使用linux命令和tab补齐等小功能,记录输入命令的条数等人性化的功能,其实底层去解释python语句的时候,还是调用python的。
ipython相当于python的一个解释器,交互工具。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<span style="font-size: 16px;">[root@python ~]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> ipython</span> Python 2.7.5 (default, Jun 17 2014, 18:11:42<span style="color: rgba(0, 0, 0, 1);">) Type </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">copyright</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);">credits</span><span style="color: rgba(128, 0, 0, 1);">"</span> <span style="color: rgba(0, 0, 255, 1);">or</span> <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">license</span><span style="color: rgba(128, 0, 0, 1);">"</span> <span style="color: rgba(0, 0, 255, 1);">for</span><span style="color: rgba(0, 0, 0, 1);"> more information. IPython </span>3.2.1 --<span style="color: rgba(0, 0, 0, 1);"> An enhanced Interactive Python. ? </span>-> Introduction <span style="color: rgba(0, 0, 255, 1);">and</span> overview of IPython<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">s features.</span> %quickref -><span style="color: rgba(0, 0, 0, 1);"> Quick reference. help </span>-> Python<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">s own help system.</span> object? -> Details about <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">object</span><span style="color: rgba(128, 0, 0, 1);">'</span>, use <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">object??</span><span style="color: rgba(128, 0, 0, 1);">'</span> <span style="color: rgba(0, 0, 255, 1);">for</span><span style="color: rgba(0, 0, 0, 1);"> extra details. In [</span>1]: cd /<span style="color: rgba(0, 0, 0, 1);">etc </span>/<span style="color: rgba(0, 0, 0, 1);">etc In [</span>2<span style="color: rgba(0, 0, 0, 1);">]: pwd Out[</span>2]: u<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/etc</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);"> In [</span>3<span style="color: rgba(0, 0, 0, 1);">]: pri </span><span style="color: rgba(0, 0, 255, 1);">print</span><span style="color: rgba(0, 0, 0, 1);"> printcap In [</span>3]: <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> |
ipython……对比……python
1 2 3 4 5 6 7 8 9 10 11 |
<span style="font-size: 16px;">[root@python ~]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> python</span> Python 2.7.5 (default, Jun 17 2014, 18:11:42<span style="color: rgba(0, 0, 0, 1);">) [GCC </span>4.8.2 20140120 (Red Hat 4.8.2-16<span style="color: rgba(0, 0, 0, 1);">)] on linux2 Type </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">help</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);">copyright</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);">credits</span><span style="color: rgba(128, 0, 0, 1);">"</span> <span style="color: rgba(0, 0, 255, 1);">or</span> <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">license</span><span style="color: rgba(128, 0, 0, 1);">"</span> <span style="color: rgba(0, 0, 255, 1);">for</span><span style="color: rgba(0, 0, 0, 1);"> more information. </span>>>> <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>>>> cd /<span style="color: rgba(0, 0, 0, 1);">etc Traceback (most recent call last): File </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);"><stdin></span><span style="color: rgba(128, 0, 0, 1);">"</span>, line 1, <span style="color: rgba(0, 0, 255, 1);">in</span> <module><span style="color: rgba(0, 0, 0, 1);"> NameError: name </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">cd</span><span style="color: rgba(128, 0, 0, 1);">'</span> <span style="color: rgba(0, 0, 255, 1);">is</span> <span style="color: rgba(0, 0, 255, 1);">not</span><span style="color: rgba(0, 0, 0, 1);"> defined </span>>>></span> |
融合部分linux基础命令且具备补齐功能,这补齐功能对于初学者还是很关键的,所以墙裂推荐安装使用。
第一种方法:通过yum安装ipython
需要有网络yum进行安装
下载获取epel-release-7-5.noarch.rpm软件包
1 2 3 4 |
<span style="font-size: 16px;">[root<span style="color: rgba(128, 0, 128, 1);">@python</span> yum.repos.d]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> rpm -ivh epel-release-7-5.noarch.rpm</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> yum.repos.d]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> ls</span><span style="color: rgba(0, 0, 0, 1);"> bak epel</span>.repo local_yum.<span style="color: rgba(0, 0, 0, 1);">repo [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> yum.repos.d]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> yum install python-ipython-console</span></span> |
第二种方法:通过pip安装ipython
1、安装pip
下载获取pip-8.1.2.tar.gz#md5=87083c0b9867963b29f7aba3613e8f4a.gz压缩包
1 2 3 4 5 6 |
<span style="font-size: 16px;">[root<span style="color: rgba(128, 0, 128, 1);">@python</span> lianxi]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> tar -xf pip-8.1.2.tar.gz#md5=87083c0b9867963b29f7aba3613e8f4a.gz</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> lianxi]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> cd pip-8.1.2</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> ls |grep "py"</span><span style="color: rgba(0, 0, 0, 1);"> setup</span>.<span style="color: rgba(0, 0, 0, 1);">py [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> python setup.py build</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> python setup.py install</span></span> |
2、用pip安装ipython:pip需要连接网络进行安装ipython
使用Python里的pip命令安装ipython(pip软件相当于linux里的yum)
1 2 3 4 5 |
<span style="font-size: 16px;">[root<span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> pip uninstall -y ipython #卸载ipython(去除其他版本的ipython)</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> pip install ipython==3.2.1 #安装ipython的3.2.1版本</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> pip install psutil #安装psutil,psutil(python 里的工具,查看cpu 内存...)</span><span style="color: rgba(0, 0, 0, 1);"> [root</span><span style="color: rgba(128, 0, 128, 1);">@python</span> pip-<span style="color: rgba(128, 0, 0, 1);">8.1</span>.<span style="color: rgba(128, 0, 0, 1);">2</span>]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> python</span> >>> <span style="color: rgba(0, 0, 255, 1);">import</span> psutil <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">将安装了的psutil导入到python中</span></span> |
>>>:你键入Python语句的主要提示符。
…:Python语句的次要提示符。
:续行符号
#:注释符号,大段注释使用三个单引号’’’或者三个双引号”””
[root@python lianxi]# vim first.py
1 2 3 4 5 6 7 8 |
<span style="font-size: 16px;"><span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">!/usr/bin/python</span> <span style="color: rgba(128, 0, 0, 1);">'''</span><span style="color: rgba(128, 0, 0, 1);"> python ………… zhangjiacai </span><span style="color: rgba(128, 0, 0, 1);">'''</span> <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">对引号中间的内容进行了多行注释</span> <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">three sig_quotes to annotation</span> <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);">come on to python</span><span style="color: rgba(128, 0, 0, 1);">"</span></span> |
[root@python lianxi]# python first.py
come on to python
Python2:print “Hello World”
Python3:print(“Hello World”)
[root@python lianxi]# python
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="font-size: 16px;">>>> name = <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">xiaoge</span><span style="color: rgba(128, 0, 0, 1);">"</span> <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">若不使用引号,xiaoge会被解析为一个变量</span> >>> age=100 <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">定义数值变量可以不需要使用引号将内容引起来</span> >>><span style="color: rgba(0, 0, 0, 1);"> name </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">xiaoge</span><span style="color: rgba(128, 0, 0, 1);">'</span> >>> _ <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">上一次使用的变量的值</span> <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">xiaoge</span><span style="color: rgba(128, 0, 0, 1);">' </span> >>> <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, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">字符串需要用单(双)引号引起来</span><span style="color: rgba(0, 0, 0, 1);"> hello world </span>>>> <span style="color: rgba(0, 0, 255, 1);">print</span> <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">使用反斜杆续行</span><span style="color: rgba(0, 0, 0, 1);"> ... </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">hello</span><span style="color: rgba(128, 0, 0, 1);">'</span> <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">在续的行里使用了次要提示符</span><span style="color: rgba(0, 0, 0, 1);"> hello</span></span> |
#如同bash,已定义的变量可以直接调用使用
1 2 3 4 5 6 7 8 |
<span style="font-size: 16px;">>>> <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);">myname is</span><span style="color: rgba(128, 0, 0, 1);">"</span>,name,<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">my age is</span><span style="color: rgba(128, 0, 0, 1);">"</span>,age <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">需要是用逗号分隔符</span><span style="color: rgba(0, 0, 0, 1);"> myname </span><span style="color: rgba(0, 0, 255, 1);">is</span> xiaoge my age <span style="color: rgba(0, 0, 255, 1);">is</span> 100 >>> <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);">myname is %s,my age is %d</span><span style="color: rgba(128, 0, 0, 1);">"</span>%(name,age) <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">推荐语法</span><span style="color: rgba(0, 0, 0, 1);"> myname </span><span style="color: rgba(0, 0, 255, 1);">is</span> xiaoge,my age <span style="color: rgba(0, 0, 255, 1);">is</span> 100 >>> <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);">myname is %s,my age is %d</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);">xiaozhang</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">,age) myname </span><span style="color: rgba(0, 0, 255, 1);">is</span> xiaozhang,my age <span style="color: rgba(0, 0, 255, 1);">is</span> 100</span> |
解析:
%s、%d相当于指定格式的占位符,前者代表string,后者代表数值变量。从%()中以逗号分隔的顺序取,且必须一一对应。
1 2 3 4 |
<span style="font-size: 16px;">>>> <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);">myname is %s,my age is %d</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);">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);">jia</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) Traceback (most recent call last): File </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);"><stdin></span><span style="color: rgba(128, 0, 0, 1);">"</span>, line 1, <span style="color: rgba(0, 0, 255, 1);">in</span> <module><span style="color: rgba(0, 0, 0, 1);"> TypeError: </span>%d format: a number <span style="color: rgba(0, 0, 255, 1);">is</span> required, <span style="color: rgba(0, 0, 255, 1);">not</span> str</span> |
类型错误:%d格式必须是一个数值而不是字符串。(因为类型不一样,所以要注意变量的顺序)
1、type()查看变量类型的函数、len()统计string的长度
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 16px;">>>><span style="color: rgba(0, 0, 0, 1);"> type(name) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">str</span><span style="color: rgba(128, 0, 0, 1);">'</span>> >>><span style="color: rgba(0, 0, 0, 1);"> type(age) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">int</span><span style="color: rgba(128, 0, 0, 1);">'</span>> >>> name = <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">zhangjiacai</span><span style="color: rgba(128, 0, 0, 1);">"</span> >>><span style="color: rgba(0, 0, 0, 1);"> len(name) </span>11 >>> len(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">nihao</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) </span>5</span> |
注意:len()只能是用来查看字符串的长度
2、变量类型转换:int()转换为整型的函数、str()转换为字符串类型的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="font-size: 16px;">>>> a=<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">123</span><span style="color: rgba(128, 0, 0, 1);">"</span> >>><span style="color: rgba(0, 0, 0, 1);"> type(a) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">str</span><span style="color: rgba(128, 0, 0, 1);">'</span>> >>> A=<span style="color: rgba(0, 0, 0, 1);">int(a) </span>>>><span style="color: rgba(0, 0, 0, 1);"> type(A) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">int</span><span style="color: rgba(128, 0, 0, 1);">'</span>> >>> b=456 >>><span style="color: rgba(0, 0, 0, 1);"> type(b) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">int</span><span style="color: rgba(128, 0, 0, 1);">'</span>> >>> B=<span style="color: rgba(0, 0, 0, 1);">str(b) </span>>>><span style="color: rgba(0, 0, 0, 1);"> type(B) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">str</span><span style="color: rgba(128, 0, 0, 1);">'</span>></span> |
所谓内建函数,也就是一种内建的方法,方法就是实现某个功能的函数或者模块(一段实现某个功能的代码)
1 2 3 4 5 6 7 8 |
<span style="font-size: 16px;">>>> username = raw_input(<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">please input login name:</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) please input login name:</span>123 >>><span style="color: rgba(0, 0, 0, 1);"> type(username) </span><type <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">str</span><span style="color: rgba(128, 0, 0, 1);">'</span>> >>> <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);">Your login name is:</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">,username Your login name </span><span style="color: rgba(0, 0, 255, 1);">is</span>: 123</span> |
raw_input:属于内建函数(功能类似与shell中的read),会将接收到的内容作为字符串类型处理
Q:如何实现按任意键继续?
A:raw_input(‘please press anything key to continue’)
python 编写脚本实现:
1.提醒用户输入用户名和密码,然后显示出用户输入的用户名和密码
2.输出用户名和密码的长度,如果密码长度低于6位给予提醒,密码太弱
[root@python lianxi]# vim login.py
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 16px;"><span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">!/usr/bin/python</span><span style="color: rgba(0, 128, 0, 1);"> #</span><span style="color: rgba(0, 128, 0, 1);">coding:utf-8</span> u_name=raw_input(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">Please input username:</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) u_pin</span>=raw_input(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">Please input password:</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">) </span><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);">The length of username is</span><span style="color: rgba(128, 0, 0, 1);">"</span>,len(u_name),<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">the length of password is</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">,len(u_pin) </span><span style="color: rgba(0, 0, 255, 1);">if</span> len(u_pin)>6<span style="color: rgba(0, 0, 0, 1);">: </span><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);">The password is 安全</span><span style="color: rgba(128, 0, 0, 1);">"</span> <span style="color: rgba(0, 0, 255, 1);">else</span><span style="color: rgba(0, 0, 0, 1);">: </span><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);">The password is dangerous</span><span style="color: rgba(128, 0, 0, 1);">"</span></span> |
注意:
python脚本程序中使用中文需要添加声明注释,如下:
#-*-coding:UTF-8-*- 或者 #coding:utf-8
1 2 3 4 5 6 7 8 9 10 11 |
<span style="font-size: 16px;">[root@python lianxi]<span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> python login.py</span><span style="color: rgba(0, 0, 0, 1);"> Please input username:zhangjiacai Please input password:</span>123456789<span style="color: rgba(0, 0, 0, 1);"> The length of username </span><span style="color: rgba(0, 0, 255, 1);">is</span> 11 the length of password <span style="color: rgba(0, 0, 255, 1);">is</span> 9<span style="color: rgba(0, 0, 0, 1);"> The password </span><span style="color: rgba(0, 0, 255, 1);">is</span><span style="color: rgba(0, 0, 0, 1);"> 安全 [root@python lianxi]</span><span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);"> python login.py</span><span style="color: rgba(0, 0, 0, 1);"> Please input username:zhangjiacai Please input password:</span>123456<span style="color: rgba(0, 0, 0, 1);"> The length of username </span><span style="color: rgba(0, 0, 255, 1);">is</span> 11 the length of password <span style="color: rgba(0, 0, 255, 1);">is</span> 6<span style="color: rgba(0, 0, 0, 1);"> The password </span><span style="color: rgba(0, 0, 255, 1);">is</span> dangerous</span> |
1、简单算术实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span style="font-size: 16px;">>>> a=3 >>> b=4 >>> c=a*<span style="color: rgba(0, 0, 0, 1);">b </span>>>> <span style="color: rgba(0, 0, 255, 1);">print</span><span style="color: rgba(0, 0, 0, 1);"> c </span>12 >>> <span style="color: rgba(0, 0, 255, 1);">print</span> 12*4 48 >>> <span style="color: rgba(0, 0, 255, 1);">print</span> 23+25 48 >>> <span style="color: rgba(0, 0, 255, 1);">print</span> 13+45*2-22 81 >>> 5 > 6<span style="color: rgba(0, 0, 0, 1);"> False </span>>>> b><span style="color: rgba(0, 0, 0, 1);">a True </span>>>> 8<9<span style="color: rgba(0, 0, 0, 1);"> True</span></span> |
2、随机数产生
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span style="font-size: 16px;">1><span style="color: rgba(0, 0, 0, 1);"> 随机整数: </span>>>> <span style="color: rgba(0, 0, 255, 1);">import</span> random <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">导入模块(模块里包含了很多方法)</span> >>> random.randint(0,99<span style="color: rgba(0, 0, 0, 1);">) </span>22 >>> random.randint(0,99<span style="color: rgba(0, 0, 0, 1);">) </span>36 2><span style="color: rgba(0, 0, 0, 1);"> 随机选取0到10之间的偶数: </span>>>> <span style="color: rgba(0, 0, 255, 1);">import</span><span style="color: rgba(0, 0, 0, 1);"> random </span>>>> random.randrange(0,10,2) <span style="color: rgba(0, 128, 0, 1);">#</span><span style="color: rgba(0, 128, 0, 1);">0到10之间的数,步长为2</span> 2 >>> random.randrange(0,10,2<span style="color: rgba(0, 0, 0, 1);">) </span>6 3><span style="color: rgba(0, 0, 0, 1);"> 随机从指定的数中选取: </span>>>> <span style="color: rgba(0, 0, 255, 1);">import</span><span style="color: rgba(0, 0, 0, 1);"> random </span>>>> random.choice([1,2,3,4<span style="color: rgba(0, 0, 0, 1);">]) </span>3 >>> random.choice([1,2,3,4<span style="color: rgba(0, 0, 0, 1);">]) </span>4</span> |
一个.py的文件(实现某些功能的一个文件),里面有很多方法(函数)—>库(library)
因为模块的出现,使得python的使用变得轻松加愉快……
例:sys模块,是python系统的相关功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span style="font-size: 16px;">In [1]: <span style="color: rgba(0, 0, 255, 1);">import</span><span style="color: rgba(0, 0, 0, 1);"> sys In [</span>2<span style="color: rgba(0, 0, 0, 1);">]: sys.path Out[</span>2<span style="color: rgba(0, 0, 0, 1);">]: [</span><span style="color: rgba(128, 0, 0, 1);">''</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/bin</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python27.zip</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python2.7</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python2.7/plat-linux2</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python2.7/lib-tk</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python2.7/lib-old</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python2.7/lib-dynload</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib64/python2.7/site-packages</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib/python2.7/site-packages</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/usr/lib/python2.7/site-packages/IPython/extensions</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">, </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">/root/.ipython</span><span style="color: rgba(128, 0, 0, 1);">'</span>]</span> |
sys.path变量(≈PATH变量):显示python模块情况下所有的模块存放的路径目录。