Python之路:函数和变量
2013-09-20 19:52
225 查看
原文来自:http://www.yuanyong.org/blog/python/way-to-python-function-variable
我知道我写的一些代码至今仍然在运行,我觉得这是一个令人欣慰的贡献。
最近发觉windows下一个非常不错的python IDE,自带的IDLE实在是太烂了,用着真心崩溃,pycharm真心不错,windows下python IDE不二选择。
python函数是用来执行一个单一的,有关行动的有组织的,可重用代码块。功能提供了更好地为您的应用程序和代码重用的高度模块化。
Python为提供了许多内置功能,如print()等,但用户也可以创建自己的函数,这些功能被称为用户定义的函数。
Python定义函数的简单规则为:
①功能块函数名和括号
②关键字def开始
③函数的第一个语句可以是一个可选的声明 - 文档字符串的函数或的docstring
④在每个函数的代码块开始用冒号(: ) 和缩进
⑤语句返回
语法:
强调一下:函数里边的变量和脚本里边的变量之间是没有连接的。
文件保存为ex20.py,假设file为ex20_example.txt,内容为:
Toall the people out there.
I say I don't like my hair.
I need to shave it off.
cmd后进入相应目录,运行“python ex20.py ex20_example.txt”,得下面结果:
人生苦短,我用python~
我知道我写的一些代码至今仍然在运行,我觉得这是一个令人欣慰的贡献。
最近发觉windows下一个非常不错的python IDE,自带的IDLE实在是太烂了,用着真心崩溃,pycharm真心不错,windows下python IDE不二选择。
python函数是用来执行一个单一的,有关行动的有组织的,可重用代码块。功能提供了更好地为您的应用程序和代码重用的高度模块化。
Python为提供了许多内置功能,如print()等,但用户也可以创建自己的函数,这些功能被称为用户定义的函数。
Python定义函数的简单规则为:
①功能块函数名和括号
②关键字def开始
③函数的第一个语句可以是一个可选的声明 - 文档字符串的函数或的docstring
④在每个函数的代码块开始用冒号(: ) 和缩进
⑤语句返回
语法:
1 | def functionname( parameters ): |
2 | function_docstring" |
3 | function_suite |
4 | return [expression] |
1 | from sys import argv |
2 |
3 | script, input_file = argv |
4 |
5 | def print_all(f): |
6 | f.read() #读入整个文件,并打印出来 |
7 |
8 | def rewind (f): |
9 | f.seek( 0 ) # 指针指向文件开始的位置 |
10 |
11 | def print_a_line(line_count, f): |
12 | line_count, f.readline() #读入行,并打印出来 |
13 |
14 | current_file = open (input_file) |
15 |
16 | "First let's print the whole file: \n" |
17 |
18 | print_all(current_file) |
19 |
20 | "Now let's rewind, kind of like a tape." |
21 |
22 | rewind(current_file) # 经过print_all后指针指向文件末尾,故要重新设定到文件开始位置 |
23 |
24 | "let's print three lines:" |
25 |
26 | current_line = 1 |
27 | print_a_line(current_line, current_file) |
28 |
29 | current_line = current_line + 1 |
30 | print_a_line(current_line, current_file) |
31 |
32 | current_line = current_line + 1 |
33 | print_a_line(current_line, current_file) |
Toall the people out there.
I say I don't like my hair.
I need to shave it off.
cmd后进入相应目录,运行“python ex20.py ex20_example.txt”,得下面结果:
1 | First let's the whole file: |
2 |
3 | To all the people out there. |
4 | I say I don't like my hair. |
5 | I need to shave it off. |
6 |
7 | Now let's rewind, kind of like a tape. |
8 | Let's three lines: |
9 | 1 To all the people out there. |
10 |
11 | 2 I say I don't like my hair. |
12 |
13 | 3 I need to shave it off. |
相关文章推荐
- Python学习之路-变量、字符串、函数
- python函数局部变量用法实例分析
- python之函数的变量【进阶篇】
- python中那些双下划线开头得函数和变量
- python函数参数是值传递还是引用传递(以及变量间复制后是否保持一致):取决于对象内容可变不可变
- Learn Python The Hard Way学习(19) - 函数和变量
- Python学习之路day3-函数
- python 函数、函数式编程、变量作用域、函数__doc__属性
- 关于python中带下划线的变量和函数的意义
- Python函数基础---参数、变量
- python基础知识之变量、语句、函数、模块
- python 函数 本地变量 函数的命名空间 变量的作用域
- 【Python】PYTHON 函数局部变量和全局变量
- Python3基础 global关键字 使函数的局部变量升格为全局变量
- Python os 模块中一些重要函数和变量
- Python3学习笔记4-函数,全局变量&局部变量,内置函数
- python学习笔记三:函数及变量作用域
- python 笔记 函数和变量 《笨办法学Python》习题19 ——12.27
- Python之路——numpy各函数简介之生成数组函数(Array creation routines)
- Python变量函数:函数名存在变量中