python之路:进击的小白
1.hello world
print("hello world")
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']
3.字符编码-*-coding:utf-8-*-
在python2.0版本中,不声明字符编码则不支持中文
python3.0可以不声明默认是 -*-coding:utf-8-*-
4.注释:
单行注释: "#"
多行注释: " ''' "(3个单引号)
5.输入
name_input=input("用户名:")
password_input=input("密码")
- 密码可以密文(在linux环境中)
1 #!/usr/bin/env python 2 #-*-coding:utf-8-*- 3 4 import getpass 5 password_input=getpass.petpass("密码:")
6.模块
- os模块
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import os 5 6 os.system("df -h") #调用系统命令
- sys模块
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import sys 5 6 print(sys.argv) 7 8 9 #输出 10 $ python test.py helo world 11 ['test.py', 'helo', 'world'] #把执行脚本时传递的参数获取到了
- Tab模块
1 import sys 2 import readline 3 import rlcompleter 4 5 if sys.platform == 'darwin' and sys.version_info[0] == 2: 6 readline.parse_and_bind("bind ^I rl_complete") 7 else: 8 readline.parse_and_bind("tab: complete") # linux and python3 on mac 9 10 for mac
1 #!/usr/bin/env python 2 # python startup file 3 import sys 4 import readline 5 import rlcompleter 6 import atexit 7 import os 8 # tab completion 9 readline.parse_and_bind('tab: complete') 10 # history file 11 histfile = os.path.join(os.environ['HOME'], '.pythonhistory') 12 try: 13 readline.read_history_file(histfile) 14 except IOError: 15 pass 16 atexit.register(readline.write_history_file, histfile) 17 del os, histfile, readline, rlcompleter 18 19 for Linux