Python 基础篇:编码、变量、模块

1. 编码

python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ASCII)。

image_1aucdo61dpav191k1ohq11451kuh9.png-6.2kB

2. 变量

变量定义的规则:

  • 变量名只能是 字母、数字或下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 以下关键字不能声明为变量名
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

变量的赋值:

name="kaliy"

3. 用户输入

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

name = input("Please input your name ?")
print(name)

输入密码时,如果想要不可见,利用getpass模块中的getpass方法

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

import getpass

pwd = getpass.getpass("Please input your password:")
print(pwd)

4. 模块

Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持。

sys

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

import sys

print(sys.argv)

# 输出获得传递的参数
$ python test.py hello world
['test.py','hello','world']

OS

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

import os

# 调用系统命令
os.system("df -h")

结合使用

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

import sys,os

os.system('',join(sys.argv[1:]))

小结
学会定义变量和使用模块

posted @ 2016-10-08 22:14  warkly  阅读(143)  评论(0编辑  收藏  举报