Skip to content

Python 基本数据类型

Ken.Wong edited this page Jul 5, 2017 · 13 revisions

基本语法

1、在python中,用"#"注释代码

Number

2+2 => 4

2-2 => 3

2*2 => 4

1/3 => 0.33333333333

以上为python的四则运算,与其他语言一致

唯一需要注意的是,除法返回的结果永远为float类型

数的n次幂运算

2 ** 5 => 32 (即有5个2相乘)

结果返回的原则:

int + int = int

int + float = float

使用round函数来返回小数点位数

t = 1/3 => 0.33333

t = round(t , 2)

print(t); => 0.33


String

------2种定义字符串的方法----

一是单引号: 'aaaaa'

单引号包含的字符串中包含单引号,则需要转义

'aaaaa\'aaaa'

在单引号中可以包含双引号

'aaaaa"aaaa'

二是双引号: "aaaaa"

双引号包含的字符串中包含双引号,则需要转义

"aa"aaa"

在双引号中可以包含单引号

'aaaaa'aaaa'


-------多行字符串定义-------------- 使用 """...""" or '''...''' 包含起来

examplate:

print("""\

Usage: thingy [OPTIONS]

 -h                        Display this usage message

 -H hostname               Hostname to connect to

""")


----- 重复输出某个字符串 ----

"hello" * 3 => 'hellohellohello'


---- 字符串的连接 ----------

连接两个字符串,用 + 运算符

'a'+'n' => 'an'


List

Clone this wiki locally