nubmber 数字类型(整数(int),浮点数/小数(float)

>>> type(1)
<class 'int'>
>>>type(2/2)
<class 'float'>
>>>type(2//2) 
<class 'int'>

2进制,8进制,16进制

  • 十进制:10
  • 二进制: 0b10 (2)
  • 八进制:0O10 (8)
  • 十六进制: 0x1F(16)

进制转换:

  • 转换成二进制

       bin(10)
  • 转换成八进制

       oct(10)
  • 转换成十六进制

       hex(10)
    

bool 布尔类型:表示真和假的状态

True # 真 (非0非空)
False #  假 (0或none)
# Python中的True和False首字母必须大写

complex 复数

    36j # 表示复数

str 字符串

单引号,双引号,三引号表示字符串

'python'
"python"

三引号用来换行

'''
hello
hello
'''

"""
hello
hello """

转义字符 \
用来表示无法看见的字符,或者与语言本身冲突的字符

\n # 换行
\t # tab
\r # 回车

换行符 \

"hello\
world"

输出路径的方法

print(r'C:\windows\system32\bin')
# r用来申明原始字符串,大小写不敏感
print('C:\\windows\\system32\\bin')

字符串操作:

字符串运算

"hello"+"world" # 输出 hello world
"hello" * 3  # 输出 hello hello hello
"hello"[0] # 输入h 正序
"hello"[-1] # 输出 o 倒序
"hello world"[0:5] # 切片 输出 hello  
"hello world"[0:-1] # 切片 输出 hello worl
"hello python java #c javascript php"[6:] # 切片 输出 python java #c javascript php
"hello python java #c javascript php"[-3:] # 切片 输出 php

组/序列

表示组的方式有很多种:列表
列表list,元素可加入多种类型,可嵌套:

[1,2,3,4,5,6,'呵呵',True,['a','c']]

访问列表元素:

['烤包子','大盘鸡','抓饭','烤肉','过油肉拌面']
['烤包子','大盘鸡','抓饭','烤肉','过油肉拌面'][0] # 烤包子
['烤包子','大盘鸡','抓饭','烤肉','过油肉拌面'][0:2] # ['烤包子','大盘鸡'] 切片操作
['烤包子','大盘鸡','抓饭','烤肉','过油肉拌面'][-1:] # ['过油肉拌面']

列表追加:

['1']+['b'] # ['1','b']

元组tuple,可参考列表
当元组中只有唯一一个元素时,元组将转换成该元素的类型

(1) # int
("string") # str

申明单个元素的元组

(1,) # tuple
("string",) # tuple

空元组

() # tuple

* 字符串,列表,元组均可看作是序列 *

延申:

逻辑运算

3 in [1,2,4,5] # False
3 not in [1,2,4,5] # True

常用计算

len('python') # 6 
max([1,3,7]) # 7
min([1,3,7]) # 1
max('python') # y 按照ASII码值排序
min('python') # h 字符串用空格会输出空格
ord('w') # 119

集合 set

区别于序列,集合时无序的一组元素组成的对象,集合会自动剔除重复的元素

集合的表示:

{1,2,3,4}
{1,2,3,4}[0] # 报错!!!
{1,1,3,4,6,6} # {1,3,4,6}

集合的操作:

len{1,2,3} # 3 获取长度
3 in {1,2,3} # True 判断集合中是否存在某元素
{1,2,3,4,5} - {3,4} # {1,2,5} 求集合的差集
{1,2,3,4,5} & {3,4} # {3,4} 求集合的交集
{1,2,3,4,5} | {3,4,7} # {1,2,3,4,5,7} 求集合的并集
set() //定义空集合

字典 dict

由很多键值对组成的集合, 字典中的键不能重复,键值必须时不可变的类型,字典可以嵌套

{key1:vaule1,key2:value2}
{'title':'内容标题','tag':{'mind':'心理学','development':'编程开发'},'content':'这是内容'}
joh = {'name':'Joh','age':29}
joh['name] # Joh

标签: none

分享到: