[course] 02 —— Python基础

Python 基础

Print

print("hello world!")

Comments and pond characters

EX02

两种注释方法,#注释和"""注释

#用于单行注释

"""用于三行注释

# A comment, this is so you can read your program later. # Anything after the # is ignored by python.

print("I could have code like this.") # and the comment after is ignored

# You can also use a comment to "disable" or comment out code: # print("This won't run.")

print("This will run.")

Numbers and Math

(EX03)

除法

/// python3 对于两个整数相除,默认使用会变为浮点型

X / Y类型:

在Python2.6或者之前,这个操作对于整数运算会省去小数部分,而对于浮点数运算会保持小数部分;在Python3.0中变成真除法(无论任何类型都会保持小数部分,即使整除也会表示为浮点数形式)。

X // Y 类型:

Floor除法:在Python 2.2中新增的操作,在Python2.6和Python3.0中均能使用,这个操作不考虑操作对象的类型,总是省略小数部分,剩下最小的能整除的整数部分。 Floor除法:效果等同于math模块中的floor函数: math.floor(x) :返回不大于x的整数 所以当运算数是负数时:结果会向下取整。

The Modulus or Remainder Operator (%) 余数/模

浮点数请参考

浮点数计算相等

浮点数近似相等计算方式:

Importing Modules

导入math模块计算计算阶层

Builtin Types 内置类型

基本数据类型和container类型

Builtin Constants 内置常量

Builtin Operators 内置运算符

Category

Operators

Arithmetic

+, -, , /, //, *, %, - (unary), + (unary)

Relational

<, <=, >=, >, ==, !=

Assignment

+=, -=, =, /=, //=, *=, %=, <<=, >>=

Logical

and, or, not

类型将会影响计算方式

为什么我们要强调python中的Types数据类型,因为不同的类型使用方法不同,将会决定我们的计算方式。

Last updated

Was this helpful?