page contents

解释器及基本编辑

Interpreter,意思是解释器,Python很重要的一块,可以理解为java中的JDK。 解释器 默认安装路径如下(可以更改): unix(mac,linux)/usr/local/bin/python3.6 windowsC:\Users\Administrator\...

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

解释器

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

  • unix(mac,linux)
    /usr/local/bin/python3.6
  • windows
    C:\Users\Administrator\AppData\Local\Programs\Python\Python36\

调用解释器

  • 可以通过在安装目录打开
  • 在终端通过命令打开

退出编辑模式

  • exit()
  • quit()
  • 快捷键
    • windows:ctrl+z
    • unix:ctrl+d(mac command)

参数传递和交互模式

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

  • 主提示符
    连续交互模式,出现>>>符号,可以连续输入

  • 次提示符
    对于连续的行,将提示使用次要提示符,默认为三个点...这个是根据语法自动变的,不需要通过手动输入

  • 注意点:
    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编辑模式

  • 运算:逻辑运算
  • 输出:字符串输出
  • 赋值:name = input(),输入值将对name赋值
  • 赋值提示:加提示语
  • 多字符串输出:打印多个字符串,逗号隔开,相当于空格
运算
>>> 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

640?wx_fmt=othersublime运行640?wx_fmt=other
  • 发表于 2020-03-04 10:11
  • 阅读 ( 558 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章