[python]Learn to program (持续更新)
2013-09-18 08:56
363 查看
Coursera 上的Learn to program课程的笔记。
Week1:
/ : 得到 float
//: integer division
pothole_case:?????????
variable:
![](http://img.blog.csdn.net/20130829231108765?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRFNiYXRpZ29s/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
几种表述:
variable refers to address
variable contains address
variable points to values
dir(__builtins__)
Week2:
01.Visualizing_Assignment_Statements
Python中的变量指向。
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fline3.png)
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fline4.png)
http://www.pythontutor.com/:很好的网站,可以可视化编程时的内存信息。
02.Type str
The escape sequence
that the second quote is simply a quote, not the end of the string:
An alternative approach is to use a double-quoted string when including a a single-quote within it, or vice-versa. Single- and double-quoted strings are equivalent. For example, when we used double-quotes
to indicate the beginning and end of the string, the single-quote in
Note: concatenate means to join together
The
All other mathematical operators and operands result in a
03.Input/Output and str Formatting
return vs. print
When a
What is passed back to the caller?
That memory address is passed back to the caller.
What is displayed?
Nothing!
When a
What is passed back to the caller?
Nothing!
What is displayed?
The values at those memory address(es) are displayed on the screen.
An example of
normal. The escape character together with the character that follows it is an escape sequence. The table below contains some of Python's commonly used escape sequences.
04.Docstrings and Function help
为function写docstrings,即函数的帮助信息。
05.Function Design Recipe
function编写规范
![](http://img.blog.csdn.net/20130829223328687?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRFNiYXRpZ29s/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
What should your function do?
Type a couple of example calls.
Pick a name (often a verb or verb phrase): What is a short answer to "What does your function do"?
Type Contract
What are the parameter types?
What type of value is returned?
Header
Pick meaningful parameter names.
Description
Mention every parameter in your description.
Describe the return value.
Body
Write the body of your function.
Test
Run the examples.
06.Function Reuse
stack:
heap:
http://tinyurl.com/l7hqbwm
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fcallstack1.png)
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fcallstack2.png)
week3:
1.Functions, Variables, and the Call Stack
继续介绍python中函数调用时的内存情况,stack,scope等概念。
![](https://d396qusza40orc.cloudfront.net/programming1/lecture_summaries/week3/5.png)
2.3介绍了基本的bool变量以及string变量转换什么的
4.介绍了import自己的函数
5.6.7全都是简单的if,没劲
4.1
介绍了字符之间以及字符串之间的关系运算符,比如 < > == 。和 + 但是不支持 -
介绍了 in 关键字 len
4.2
介绍了字符转的index,str = "what the f*ck!"
str[0]代表第一个字符 w
同时,类似Matlab ,str[0:3] = "what"
str·:」== str
![](http://img.blog.csdn.net/20130917210616406?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRFNiYXRpZ29s/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
注意 str·x:y「指的是 x x+1 x+2 …… y-1 一共 y-x+1个字符。
注意负数作为index时的情况,以及不能修改字符串的值,但是可以将字符串另附给一个新的字符串,达到修改、插入、删除的目的。
4.3
function 和 method的区别:method是包含在object中的function
import math : math其实是一个object或者说是variable,而math.sqrt()则是在该obj中的method
当新建一个string类型的variable,这个variable也就自动拥有了string类型的method
使用dir(variable_name)来查阅variable_name refer的类型的method
dir(str)
help(str.lower)
都是可以的
str.count(sub) 字串出现次数
str.find(sub) 字串第一次出现的位置,如不存在,返回-1
str.find(sub)字串最后一次出现的位置
str.strip() 消除字符串左右的空格
4.4
6.1-6.3 略
6.4 Nested Loops 中那一段 用python visualizer 的很不错
![](http://img.blog.csdn.net/20131008220311062?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRFNiYXRpZ29s/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
6.5
处理文件,
open(file_path,mode),mode 有'r','w','a'分别是读,写,附加
有四种方法
1.readline()每调用一次就读取新的一行
2.也可以直接使用
file = open()
for line in file:
pass
处理每一行
3.也可以使用
file.read(),直接得到所有内容,作为一个字符串
4.也可以
file.readlines(),得到一个list,里面是所有的行
print函数会自动在输出的字符串处,加一个'\n',使用print(str,end= ' ')阻止
6.6
write
和print类似,但是不输出换行符
other:
1.
import numpy
numpy.random.rand(3,3)
from numpy import *
random.rand(3,3)
Week1:
/ : 得到 float
//: integer division
pothole_case:?????????
variable:
几种表述:
variable refers to address
variable contains address
variable points to values
dir(__builtins__)
Week2:
01.Visualizing_Assignment_Statements
Python中的变量指向。
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fline3.png)
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fline4.png)
http://www.pythontutor.com/:很好的网站,可以可视化编程时的内存信息。
02.Type str
>>> storm_greeting = 'wow, you're dripping wet.' SyntaxError: invalid syntax
The escape sequence
\'indicates
that the second quote is simply a quote, not the end of the string:
>>> storm_greeting = 'Wow, you\'re dripping wet.' "Wow, you're dripping wet."
An alternative approach is to use a double-quoted string when including a a single-quote within it, or vice-versa. Single- and double-quoted strings are equivalent. For example, when we used double-quotes
to indicate the beginning and end of the string, the single-quote in
you'reno longer causes an error:
>>> storm_greeting = "Wow, you're dripping wet." "Wow, you're dripping wet."
String Operators
Expression | Description | Example | Output |
str1 + str2 | concatenate str1and str1 | print('ab' + 'c') | abc |
str1 * int1 | concatenate int1copies of str1 | print('a' * 5) | aaaaa |
int1 * str1 | concatenate int1copies of str1 | print(4 * 'bc') | bcbcbcbc |
The
*and
+operands obey by the standard precedence rules when used with strings.
All other mathematical operators and operands result in a
TypeError.
03.Input/Output and str Formatting
return vs. print
return expression
When a
returnstatement executes, the expression is evaluated to produce a memory address.
What is passed back to the caller?
That memory address is passed back to the caller.
What is displayed?
Nothing!
print(arguments)
When a
What is passed back to the caller?
Nothing!
What is displayed?
The values at those memory address(es) are displayed on the screen.
An example of
>>> def square_print(num): print("The square of num is", num ** 2) >>> answer_print = square_print(4) The square num is 16 >>> answer_print >>>
Triple-quoted strings
We have used single- and double- quotes to represent strings. The third string format uses triple-quotes and a triple-quoted string cab span multiple lines. For example:>>> print(''' How are you?''') How are you?
Escape Sequences
Python has a special character called an escape character:\. When the escape character is used in a string, the character following the escape character is treated differently from
normal. The escape character together with the character that follows it is an escape sequence. The table below contains some of Python's commonly used escape sequences.
Escape Sequence | Name | Example | Output |
\n | newline (ASCII linefeed - LF) | print('''How are you?''') | How are you? |
\t | tab (ASCII horizontal tab - TAB) | print('3\t4\t5') | 3 4 5 |
\\ | backslash (\) | print('\\') | \ |
\' | single quote (') | print('don\'t') | don't |
\" | double quote (") | print("He says, \"hi\".") | He says, "hi". |
为function写docstrings,即函数的帮助信息。
05.Function Design Recipe
function编写规范
The Six Steps
ExamplesWhat should your function do?
Type a couple of example calls.
Pick a name (often a verb or verb phrase): What is a short answer to "What does your function do"?
Type Contract
What are the parameter types?
What type of value is returned?
Header
Pick meaningful parameter names.
Description
Mention every parameter in your description.
Describe the return value.
Body
Write the body of your function.
Test
Run the examples.
06.Function Reuse
Calling functions within other function definitions
07.Visualizing Function Callsstack:
heap:
http://tinyurl.com/l7hqbwm
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fcallstack1.png)
![](https://d396qusza40orc.cloudfront.net/programming1%2Fimages%2Fcallstack2.png)
week3:
1.Functions, Variables, and the Call Stack
继续介绍python中函数调用时的内存情况,stack,scope等概念。
![](https://d396qusza40orc.cloudfront.net/programming1/lecture_summaries/week3/5.png)
2.3介绍了基本的bool变量以及string变量转换什么的
4.介绍了import自己的函数
5.6.7全都是简单的if,没劲
4.1
介绍了字符之间以及字符串之间的关系运算符,比如 < > == 。和 + 但是不支持 -
介绍了 in 关键字 len
4.2
介绍了字符转的index,str = "what the f*ck!"
str[0]代表第一个字符 w
同时,类似Matlab ,str[0:3] = "what"
str·:」== str
注意 str·x:y「指的是 x x+1 x+2 …… y-1 一共 y-x+1个字符。
注意负数作为index时的情况,以及不能修改字符串的值,但是可以将字符串另附给一个新的字符串,达到修改、插入、删除的目的。
4.3
function 和 method的区别:method是包含在object中的function
import math : math其实是一个object或者说是variable,而math.sqrt()则是在该obj中的method
当新建一个string类型的variable,这个variable也就自动拥有了string类型的method
使用dir(variable_name)来查阅variable_name refer的类型的method
dir(str)
help(str.lower)
都是可以的
str.count(sub) 字串出现次数
str.find(sub) 字串第一次出现的位置,如不存在,返回-1
str.find(sub)字串最后一次出现的位置
str.strip() 消除字符串左右的空格
4.4
6.1-6.3 略
6.4 Nested Loops 中那一段 用python visualizer 的很不错
6.5
处理文件,
open(file_path,mode),mode 有'r','w','a'分别是读,写,附加
有四种方法
1.readline()每调用一次就读取新的一行
2.也可以直接使用
file = open()
for line in file:
pass
处理每一行
3.也可以使用
file.read(),直接得到所有内容,作为一个字符串
4.也可以
file.readlines(),得到一个list,里面是所有的行
print函数会自动在输出的字符串处,加一个'\n',使用print(str,end= ' ')阻止
6.6
write
和print类似,但是不输出换行符
other:
1.
import numpy
numpy.random.rand(3,3)
from numpy import *
random.rand(3,3)
相关文章推荐
- python 模块知识(持续更新......)
- Learn python the hard way. python test program 2016.04.27
- Python学习—持续更新
- Python学习网站集合[持续更新...]
- python2与python3的区别(持续更新)
- Hard way to learn python 笔记15-18
- Python:Python中较使用的一些方法(持续更新)
- python+TensorFlow常见错误汇总,持续更新中......
- Python 面试练习题(持续更新)
- MSDN文档翻译 Learn to Program for Windows in C++ 001 前言
- Python 多线程 入门学习 (二) 持续更新中
- python 可迭代对象之(列表解析,迭代器迭代) =>持续更新
- 关于Python2与Python3的不同之处(持续更新)
- 实用的Python库(会一直持续更新。。。)
- Begin to learn python
- Python初学笔记——琐碎知识,based on 2.7.3,持续更新中
- 解决多个版本的python共存时的问题 => 持续更新
- 如何面试Python 后端工程师(持续更新)
- Hard way to learn python 笔记5
- Hard way to learn python 笔记8