(1)Python模式和常用命令

Python有2种运行模式

  • 交互模式:命令行中单句执行,无缩进代码
  • 文件模式:有缩进代码,无法查看每行代码的执行结果

交互模式:

Mac环境:
>>>python  #进入python2交互模式
>>>python3  #进入python3交互模式
>>>exit() + enter / ctrl + D  #退出交互模式
    
Windows环境:
>>>py -2  #进入python2交互模式
>>>py -3  #进入python3交互模式
>>>exit() + enter / ctrl + D  #退出交互模式

文件模式:

test.py  #函数保存在文件中
def add(a,b):
    return a + b
print(add(1,2))

>>>python3 test.py  #命令行执行
3

常用命令

>>>dir(__builtins__)  #查看所有内置函数
>>>help("**")  #查看内置函数帮助 eg:print
>>>Q  #退出帮助

pip命令

Mac环境:
pip3 install **  #安装**包 eg:nose
pip3 install **==1.3.6 #安装指定版本的**包
pip3 uninstall **  #卸载**包
pip3 list #查看已安装的包
pip3 show ** #查看**包的详细信息
pip3 search "**" #查看包含**关键字的包
pip3 install --upgrade pip/** #升级pip或**包
pip3 install -U pip #升级pip包本身

windows环境:
py -3 -m pip install ** #安装**包 eg:nose
py -3 -m pip install **==1.3.6 #安装指定版本的**包
py -3 -m pip uninstall **  #卸载**包
py -3 -m pip list #查看已安装的包
py -3 -m pip show **  #查看**包的详细信息
py -3 - m pip search "**"  #查看包含**关键字的包
py -3 -m pip install --upgrade pip/**  #升级pip或**包
py -3 -m pip install -U pip  #升级pip本身

函数print()

python3:print()是一个函数,必须加括号(),不换行用end=""
>>>print("hello world")
hello world

>>>print("hello world", end="")  #打印不换行
hello world>>>

python2:print是一个语法结构,不用加括号,不换行用,
>>>print "hello world"
hello world

>>>print "hello world",;print "hi"  #打印不换行
hello world hi

你可能感兴趣的:((1)Python模式和常用命令)