Lecture 2
基本数据类型
Primitivedata - numbers & strings
- value&type
Combine in expressions表达式: operands & operators操作数和运算符
Interpreter - evaluates & prints解释器先运算后返回结果
Script -no print unless explict
在脚本中执行时,如果没有显示的打印的话,每个表达式执行后是不会打印返回值的。
这一点与在控制台直接输入不同。
类型检查
Typeconvertor: str(3)+'ab'正确的
Thesyntax is OK but it is a static semantic error. Because the operator wasexpecting a particular kind of structurethere.语法没有问题,但这是一个静态语义错误。因为运算符要求一种特定的结构。
Type checking
-weak vs. strong typing 强弱的类型检查
Typediscipline类型规范
>>>'a' < 3
False
>>>4 < '3'
True
>>>'4' < '3'
False
字符与数字按照ASCII码比较。
表达式规则
operatorsprecedence 运算符优先级
when in doubt,use parens用括号
变量赋值声明
Variablevalues Assignment
that's done using an assignment statement赋值声明
x = 3*5
y = 15
z = x
When Icreate a binding, I'm taking a variable name, in this case x, stored somewherein a table, and I'm creating a link or a pointer from that name to thatvalue.
赋值就相当于取某个变量名,这里是x,存在某处,然后创建一个变量名与值之间的连接或指针。
This is a nuance细微之处. It's going to make a lot more senselater on, when we introduce mutation into our language. Don't think of it as aspecific box into which we're putting things, think of it as a link to a value.
注意这里的差别。这种思想在以后非常有用,特别是引入“可变性”的概念后。千万不要将赋值考虑为往盒子里装东西。
Type ofvariable - It inherits it from its value.
变量的类型是什么?答案是:变量的类型继承于其值。
Dynamictypes动态类型
x = 3
x = 'abc'
Itchanges depending on what the current value is. Or said a different way, ifsomewhere later on in the program I do this, x now has changed its type fromINT to string.
变量的类型随着当前值的类型而改变。换言之,如果之后程序里出现这样的语句,x就会从整型变为字符串型。
Don'tchange types arbitrarily.
不要随意改变变量类型。
Statements= legal commands that Python can interpret.
-print, assignment
代码风格
do comments 认真做注释
Variable names - important to document, keywords excluded 28
分支程序
a straight-line program
Abranching program is that can change the order of instructions based on sometest. And that test is usually a value of a variable.
if(x/2)*2 == x:
print 'Even'
else:print 'Odd'
Syntax:colon is important, it identifies a block of instructions. The colon is start,and the carriage return is the end.
分支程序是执行过程中,可以根据判别结果改变指令顺序。而判别通常是对某变量值进行的。
冒号表示开始,回车表示结束。
双等号,区别于一个等号赋值,是为左右两边值的比较
布尔组合
Boolean combination
'and or not' make a complex expression
循环
iteration or loops
设定好输入的量的类型范围
来自