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

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

sublime
运行
image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值