本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
cs:programming:python:courses:gtx_cs1301x:week_2 [2024/01/14 13:59] – 移除 - 外部编辑 (未知日期) 127.0.0.1 | cs:programming:python:courses:gtx_cs1301x:week_2 [2024/01/14 13:59] (当前版本) – ↷ 页面cs:fundamental:gtx_cs1301x:week_2被移动至cs:programming:python:courses:gtx_cs1301x:week_2 codinghare | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ======Procedural Programming====== | ||
+ | //Week 2 notes// | ||
+ | ---- | ||
+ | ====Procedural Programming==== | ||
+ | ===What is Procedural Programming=== | ||
+ | * Functional Programming | ||
+ | * **Function**: | ||
+ | * **Method**: A function that is part of a class in object-oriented programming (but colloquially, | ||
+ | * Object-Oriented Programming: | ||
+ | |||
+ | * Event-Driven Programming: | ||
+ | ==Procedural Programming in Python== | ||
+ | * type & variables | ||
+ | * operators | ||
+ | * arithmetic op | ||
+ | * logical op | ||
+ | ==Comments and Documentation== | ||
+ | * Comments: Notes from the programmer supplied in-line alongside the code itself, designated in a way that prevents the computer from reading or attempting to execute them as code. | ||
+ | * Documentation: | ||
+ | * Self-Documenting Code: 使用变量名来解释程序的意义 | ||
+ | * python 使用 | ||
+ | * ''#'' | ||
+ | ==python IDLE== | ||
+ | * 使用 IDLE 直接可以进入 | ||
+ | * Immediate Mode:交互型模式(类似计算器) | ||
+ | * Scripting Mode | ||
+ | * '' | ||
+ | * '' | ||
+ | <code py> | ||
+ | user_input = input(" | ||
+ | print(" | ||
+ | </ | ||
+ | ====Variables==== | ||
+ | * 由 name 和 value 组成 | ||
+ | * name 不变, value 会变 | ||
+ | * variable 的命名:骆驼(camel case)和下划线 (under_score)(python 用后面) | ||
+ | ==variable in python== | ||
+ | * variable 的类型在赋值的时候决定。如果改变了赋值的类型,那么 variable 的类型会随之改变 | ||
+ | * 命名规则: | ||
+ | * rule | ||
+ | * variable 只能由数字和字符(还有下划线)组成 | ||
+ | * variable 只能从字母开始 | ||
+ | * varibale 不能使用 reserve word | ||
+ | * convention | ||
+ | * variable 名应该是有意义的,比如 '' | ||
+ | ===Assigning Variables=== | ||
+ | * order relation ship: '' | ||
+ | * 在 variables 使用之前需要进行赋值(name error) | ||
+ | * None:意味着**没有值**。但这种情况是可以打印的,而没有赋值的不行(没赋值意味着没定义) | ||
+ | ===Data Types=== | ||
+ | * basic types | ||
+ | * custom types | ||
+ | * 某些 data type 是不可比较的,如果需要比较,需要人为的指定规则。 | ||
+ | ==data type in python== | ||
+ | * python 是弱类型语言 | ||
+ | * python 变量的类型由其 value 决定(C++ 是由 name 决定) | ||
+ | * 类型检测函数: | ||
+ | * python 并没有严格意义上的 primitive data. | ||
+ | ==mixing types== | ||
+ | * 如果做没有意义的,不同 type 之间的运算,会导致 TypeError. | ||
+ | * 注意看错误输出,解释器会提示是什么导致了不兼容。 | ||
+ | ==None and NoneType== | ||
+ | * '' | ||
+ | * '' | ||
+ | * 创建 '' | ||
+ | * 直接赋值 '' | ||
+ | ==None and functions== | ||
+ | 这里的例子主要是 '' | ||
+ | <code py> | ||
+ | a = " | ||
+ | b = " | ||
+ | c = a + b | ||
+ | d = print(c) | ||
+ | print(d) | ||
+ | </ | ||
+ | '' | ||
+ | * '' | ||
+ | * '' | ||
+ | ===Type conversion in python=== | ||
+ | ==string 与 数字== | ||
+ | ''" | ||
+ | * ''" | ||
+ | * '' | ||
+ | ==string 转换函数== | ||
+ | * '' | ||
+ | <code py> | ||
+ | #convert int | ||
+ | my_int = 5 | ||
+ | my_string = str(my_int) | ||
+ | #convert date | ||
+ | from datetime import date | ||
+ | myDate = date.today() | ||
+ | myDateAsString = str(myDate) | ||
+ | </ | ||
+ | 以上转换多半可以通过隐式转换来进行。但需要注意的是,'' | ||
+ | <code py> | ||
+ | print(" | ||
+ | </ | ||
+ | 可选的做法: | ||
+ | <code py> | ||
+ | print(" | ||
+ | </ | ||
+ | 另外一种做法是使用 '','' | ||
+ | <code py> | ||
+ | print(" | ||
+ | </ | ||
+ | ==string 转换为其他类型== | ||
+ | 转换为 '' | ||
+ | <code py> | ||
+ | my_int_as_str = " | ||
+ | my_int = int(my_int_as_str) | ||
+ | </ | ||
+ | 转换为 '' | ||
+ | <code py> | ||
+ | my_float_as_str = " | ||
+ | my_float = float(my_float_as_str) | ||
+ | </ | ||
+ | 转换为 '' | ||
+ | <code py> | ||
+ | my_boolean_as_str = " | ||
+ | my_boolean = bool(my_boolean_as_str) | ||
+ | </ | ||
+ | |||
+ | * 如果转换的后的结果不能转化为指定的类型,那么结果是 ValueError,比如 ''" | ||
+ | * 即便存在对应的转换关系, literal 只能转化为对应的类型,比如 ''" | ||
+ | * 例外: | ||
+ | * **不丢失信息**的转换可以实现,比如 int 的 string 可以转换为 float,但反之不行 | ||
+ | * 绝大部分数据都可以转化为 boolean | ||
+ | ==User input== | ||
+ | * '' | ||
+ | <code py> | ||
+ | myUserInput = input(" | ||
+ | myInt = int(myUserinput) | ||
+ | print(myInt * myInt) | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | 不输入任何信息直接回车会得到一个空 string。该 string 无法转化为 float | ||
+ | </ | ||
+ | |||
+ | ===Reserved Keywords in Python=== | ||
+ | 查看 python 的 keywords: | ||
+ | <code py> | ||
+ | import keyword | ||
+ | print(keyword.kwlist) | ||
+ | </ | ||
+ | *** Importing Libraries**. import, from. Not covered explicitly, but you'll see these in a few places throughout our material, especially when we're dealing with dates, turtles, or random numbers. | ||
+ | * **Logical Operators**. and, is, not, or, False, True, None. Covered in Chapter 2.3. | ||
+ | * C**ontrol Structures**. as, break, continue, if, elif, else, for, in, while, pass, with. Covered throughout Unit 3. if, elif, and else are covered in 3.2; for, while, pass, continue, and break are covered in 3.3. as and with are not covered explicitly. in comes up in Chapters 2.3, 3.2, 3.3, and other places. | ||
+ | * **Functions**. def, return. Covered in Chapter 3.4. | ||
+ | * **Object-Oriented Programming Syntax**. class. Covered in Chapter 5.1. | ||
+ | * **Error Handling**. except, finally, raise, try. Covered in Chapter 3.5. else also comes up here. | ||
+ | <WRAP center round box 100%> | ||
+ | 使用 keywords 做变量名会导致 systax error. | ||
+ | </ | ||
+ | |||
+ | ==function 名不是 reserve keyword== | ||
+ | * 使用 function 名作为变量名会掩盖 function 原有的意思(也就是 name 的 bind 变了) | ||
+ | * 实际使用中应该避免使用 function 名作为变量名 | ||
+ | ==Dot Notation in Python== | ||
+ | '' | ||
+ | <code py> | ||
+ | #date is a class, today() is its member function | ||
+ | from datetime import date | ||
+ | myDate = date.today() | ||
+ | print(myDate.year) | ||
+ | print(myDate.month) | ||
+ | print(myDate.day) | ||
+ | </ | ||
+ | 成员函数取决于类(library)提供什么样的功能。比如 '' | ||
+ | <code py> | ||
+ | import datetime | ||
+ | currentTime = datetime.datetime.now() | ||
+ | print(crrentTime.hour) | ||
+ | print(crrentTime.minute) | ||
+ | print(crrentTime.second) | ||
+ | </ | ||
+ | ====Logical Operators==== | ||
+ | *** Mathematical Operators**:Operators that perform mathematical functions | ||
+ | * **Logical Operators**:Operators that perform logical operations, such as comparing relative values, checking equality, checking set membership, or evaluating combinations of other logical operators. | ||
+ | ===Relational Operators=== | ||
+ | *** Relational Operators**: | ||
+ | *** Numeric Comparison Operators**: | ||
+ | * **Boolean Operators** | ||
+ | * Non-Numeric Equality Comparisons:不比较大小,但比较是否相等。比如苹果和橘子可以定义为不相等。 | ||
+ | * Set Operators:Check to see if a value is a member of a set of multiple values. Most often this comes up in strings and lists. | ||
+ | ==Relational Operators in Python== | ||
+ | * '' | ||
+ | * '' | ||
+ | * 该运算返回 t/ | ||
+ | ===Boolean Operators=== | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | ===Ture Tables=== | ||
+ | * 可以通过真值表快速查询复杂逻辑表达式的真假 | ||
+ | ==逻辑运算符的性质== | ||
+ | * 交换律 | ||
+ | * 分配律 | ||
+ | * 德摩根律(not( A and B)-> not A or not B | ||
+ | ====Mathematical Operators==== | ||
+ | * Assignment Operator | ||
+ | * Mathematical Operators: add, sub, mul, div, mod | ||
+ | * Additional Operators: floor div '' | ||
+ | <WRAP center round box 100%> | ||
+ | - floor div 会往最小的数进行取整。比如 3 '' | ||
+ | - floor div 中,如果有浮点数参与运算,结果也是浮点数 | ||
+ | </ | ||
+ | ===self assignement=== | ||
+ | * self assignement | ||
+ | * incrementing | ||
+ | * 自增可以用 '' | ||
+ | ==sincermenting and loops== | ||
+ | * 自增一般与 loop 连用。 | ||
+ | ==Parentheses Errors== | ||
+ | * 括号需要匹配。 | ||
+ | * 括号不匹配的出错信息往往在别的行 | ||
+ | * |