文档详情

Python Introduction_1116.pdf

发布:2015-08-13约字共14页下载文档
文本预览下载声明
Python 简介 概述 动态,解释性语言 代码简短灵活易学 免费开源、可移植可扩展、丰富的库 缺少编译语言的代码检查的过程 运行Python代码 Python解释器:可以直接输入python语句运行 基本语法规则 变量等不需要事先声明 Python不用声明变量类型,所以变量直接赋值使用 Python对大小写敏感 强制缩进 一行的结束就是语句的结束,不需要分号等结束标志 #开头表示该行为注释 函数/方法的调用:函数名(参数列表) Code Sample $ python ## Run the Python interpreter Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type help, copyright, credits or license for more information. a = 6 ## set a variable in this interpreter session a ## entering an expression prints its value 6 a + 2 8 a = hi ## a can hold a string just as well a hi len(a) ## call the len() function on a string 2 foo(a) ## try something that doesnt work Traceback (most recent call last): File , line 1, in ? NameError: name foo is not defined ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal) Python编程简介 Python源文件 (.py) 运行方法 使用命令“Python 源文件.py [参数列表]” Code Sample 命令行运行: $ python hello.py Alice Hello there Alice #!/usr/bin/python # import modules used here -- sys is a very standard one import sys # Gather our code in a main() function def main(): print Hello there, sys.argv[1] # Command line args are in sys.argv[1], sys.argv[2] ... # sys.argv[0] is the script name itself and can be ignored # Standard boilerplate to call the main() function to begin # the program. if __name__ == __main__: main() 变量名 Python不会在代码里声明变量类型,所以建议命名变量时使用容易识别的、 有意义的名字,以避免出现忘记变量类型导致的错误 例如: file_name = textfile.txt result_file = open(textfile
显示全部
相似文档