程序员Android开发经验谈Python 运维

Python笔记2:解释器及基本编辑

2017-10-27  本文已影响5人  世外大帝

Interpreter,意思是解释器,Python很重要的一块,可以理解为java中的JDK。

解释器

默认安装路径如下(可以更改):

调用解释器

退出编辑模式

参数传递和交互模式

在命令行中输入python,首先会打印出一个欢迎信息,说明其版本号和版权通知等

没有缩进会报错
>>> the_flag = True
>>> if the_flag:
... print("Hello!!")
  File "<stdin>", line 2
    print("Hello!!")
        ^
内部错误:给老子加个缩进
IndentationError: expected an indented block

好的
>>> the_flag = True
>>> if the_flag:
...     print("Hello!!")
...
Hello!!

解释器环境

python源文件默认使用UTF-8编码。

虽然标准库用的是 ASCII码,但是目前我接触到的大多用UTF-8

也可以在文件头中声明编码:
# -*- coding: encoding -*-

但必须是Python支持的编码,可以参考Python codecs

//编码
codecs.encode(obj, encoding='utf-8', errors='strict')
//解码
codecs.decode(obj, encoding='utf-8', errors='strict')
//查询
codecs.lookup(encoding)
//详细查询
codecs.CodecInfo(encode, decode, streamreader=None, streamwriter=None, incrementalencoder=None, incrementaldecoder=None, name=None)

//自定义编解码器(至少我目前用不到- -!)
codecs.register(search_function)
//打开一个编解码器
codecs.open(filename, mode='r', encoding=None, errors='strict', buffering=1)

//文件编码
codecs.EncodedFile(file, data_encoding, file_encoding=None, errors='strict')
//看名字也知道是迭代器生成编解码
codecs.iterencode(iterator, encoding, errors='strict', **kwargs)
codecs.iterdecode(iterator, encoding, errors='strict', **kwargs)

主要就以上这些,另外还有一些错误提示,无状态编解码,常量,增量编解码,流编解码等可自行查询。

当代码从UNIX“shebang”行开始时,应该将编码声明添加为文件的第二行。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

基本编辑

终端编辑模式:

输入python,进入python编辑模式

运算
>>> 1+2
3

字符串输出
>>> print("hello world")
hello world

赋值
>>> name = input()
TaoYuan

>>> name
'TaoYuan'

赋值提示
>>> age = input("enter your age please:")
enter your age please:28

>>> print(name,age)
TaoYuan 28

>>> print("hello",name)
hello TaoYuan

文本编辑模式:

这个感觉用起来效率不是太高,还不如赋值粘贴直接算的快

打开文本编辑器,推荐sublime,我用的3.0版本

3143之前版本注册码:
http://blog.csdn.net/lftaoyuan/article/details/53759877

3143注册码
http://blog.csdn.net/lftaoyuan/article/details/77980822

sublime

运行


image.png
上一篇 下一篇

猜你喜欢

热点阅读