Procedural Programming
Procedural Programming
What is Procedural Programming
Functional Programming
Function: A segment of code that performs a specific task, sometimes taking some input and sometimes returning some output
Method: A function that is part of a class in object-oriented programming (but colloquially, often used interchangeably with function).
Procedural Programming in Python
type & variables
operators
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: Collected and set-aside descriptions and instructions for a body of code.
Self-Documenting Code: 使用变量名来解释程序的意义
python 使用
python IDLE
user_input = input("Enter some text: ")
print("You entered:", user_input)
Variables
variable in python
Assigning Variables
order relation ship: x = 5
,而不是 5=x
(systax error)
在 variables 使用之前需要进行赋值(name error)
None:意味着没有值。但这种情况是可以打印的,而没有赋值的不行(没赋值意味着没定义)
Data Types
data type in python
mixing types
None and NoneType
None and functions
这里的例子主要是 print()
。print()
函数实际上并没有返回值(也可以说返回类型是 NoneType
),以此以下程序中:
a = "Hello"
b = "world"
c = a + b
d = print(c)
print(d)
d = print©
实际上做了两步:
Type conversion in python
string 与 数字
“123”
与 123
是两种不同的数据:
“123”
以文本形式存储
123
是以整型的形式存储
string 转换函数
#convert int
my_int = 5
my_string = str(my_int)
#convert date
from datetime import date
myDate = date.today()
myDateAsString = str(myDate)
以上转换多半可以通过隐式转换来进行。但需要注意的是,
print()
内部无法进行隐式转换。比如下面的例子会造成 TypeError
print("Today's date: " + myDate)
可选的做法:
print("Today's date: " + str(myDate))
另外一种做法是使用
,
运算符,
print()
会将
,
两边的分别衡量,因此也不会产生因
+
带来的类型不匹配了:
print("Today's date:", date.today())
string 转换为其他类型
转换为 int
my_int_as_str = "5"
my_int = int(my_int_as_str)
转换为
float
my_float_as_str = "5.1"
my_float = float(my_float_as_str)
转换为
bool
my_boolean_as_str = "True"
my_boolean = bool(my_boolean_as_str)
myUserInput = input("Enter an int")
myInt = int(myUserinput)
print(myInt * myInt)
不输入任何信息直接回车会得到一个空 string。该 string 无法转化为 float
Reserved Keywords in Python
查看 python 的 keywords:
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.
Control 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.
使用 keywords 做变量名会导致 systax error.
function 名不是 reserve keyword
Dot Notation in Python
.
运算符是成员运算符,用于访问类中的成员;比如:
#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)提供什么样的功能。比如
datetime
类就能提供与
date
类不一样的功能:
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: Operators that check the relationships between multiple variables
Numeric Comparison Operators: Operators that facilitate numeric comparison between values.
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
Boolean Operators
Ture Tables
逻辑运算符的性质
Mathematical Operators
Assignment Operator
Mathematical Operators: add, sub, mul, div, mod
Additional Operators: floor div //
, exponent(base **
exp)
floor div 会往最小的数进行取整。比如 3 //
-2, 浮点结果是 -1.5
,最小可以取到的整数是 -2
,因此结果是 -2
floor div 中,如果有浮点数参与运算,结果也是浮点数
self assignement
sincermenting and loops
Parentheses Errors