Coursera 上的Learn to program课程的笔记。
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中的变量指向。
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're
no longer causes an error:
>>> storm_greeting = "Wow, you're dripping wet." "Wow, you're dripping wet."
Expression | Description | Example | Output |
str1 + str2 |
concatenate str1 and str1 |
print('ab' + 'c') |
abc |
str1 * int1 |
concatenate int1 copies of str1 |
print('a' * 5) |
aaaaa |
int1 * str1 |
concatenate int1 copies of str1 |
print(4 * 'bc') |
bcbcbcbc |
Note: concatenate means to join together
The *
and +
operands obey by the standard precedence rules when used with strings.
All other mathematical operators and operands result in a TypeError
.
return expressionWhen a
return
statement executes, the expression is evaluated to produce a memory address.
print(arguments)When a
print
function call is executed, the argument(s) are evaluated to produce memory address(es).
An example of print
:
>>> def square_print(num): print("The square of num is", num ** 2) >>> answer_print = square_print(4) The square num is 16 >>> answer_print >>>
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?
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 |
How |
\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". |
04.Docstrings and Function help
为function写docstrings,即函数的帮助信息。
05.Function Design Recipe
function编写规范
stack:
heap:
http://tinyurl.com/l7hqbwm
week3:
1.Functions, Variables, and the Call Stack
继续介绍python中函数调用时的内存情况,stack,scope等概念。
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)