int 数值
str 字符串
bool 布尔值
list 列表
tuple 元组
dict 字典
\t 是Tab键
1、整数,int
a.
n1 = 123
n2 = 456
print(n1 + n2)
print(n1.add(n2))
b.
获取可表示的二进制最短位数
n1 = 4 # 00000100
ret = n1.bit_length()
print(ret)
2、字符串
join 连接
#!usr/bin/env python
#-*- coding utf-8 -*-
a = ["abc","def","ghi","jkl"]
ret = "-".join(a)
print(ret)
结果:abc-def-ghi-jkl
列表和元组都支持
len(s) 获取s变量字符长度
capitalize 首字母变大写
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "abcdefgh"
ret = a.capitalize()
print(ret)
结果:“Abcdefgh"
center 内容居中,width:总长度;fillchar:空白处填充内容,默认无
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "abcdefgh"
ret = a.center(20,"*")
print(ret)
结果:abcdefgh
count 子序列个数
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "abcdefgh"
ret = a.count("b")
print(ret)
结果:1
endswith 是否以 xxx 结束
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "abcdefgh"
ret = a.endswith("g")
ret1 = a.endswith("h")
print(ret)
print(ret1)
结果:False
True
expandtabs 将tab转换成空格,默认一个tab转换成8个空格
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "abcdefgh\t hello"
b = "abcdefgh hello"
ret = a.expandtabs()
ret1 = b.expandtabs()
print(ret)
print(ret1)
结果:abcdefgh hello
abcdefgh hello
find 寻找子序列位置,如果没找到,返回 -1
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "abcdefgh hello"
ret = a.find("h",0,9)
print(ret)
结果:7
format 字符串格式化,动态参数
#!usr/bin/env python
#-*- coding utf-8 -*-
a = "name:{0},age:{1}"
ret = a.format("your","20")
print(ret)
结果:name:your,age:20
for循环,continue,break 也适用
方法一:
#!usr/bin/env python
#-*- coding utf-8 -*-
s = "12345678900987654321"
for a in s:
print(a)
方法二
#!usr/bin/env python
#-*- coding utf-8 -*-
s = "abcdefghijklggopqrstuvwxyz"
for a in s:
if a =="g":
continue
print(a)
方法三:
#!usr/bin/env python
#-*- coding utf-8 -*-
s = "abcdefghijklggopqrstuvwxyz"
for a in s:
if a =="g":
break
print(a)
###列表###
name_list = [“abc”,“efg”,“hjk”]
#索引
print(name_list[0])
#切片
print(name_list[0:2])
#len
print(name_list[2:len(name_list)])
#For
a = [“abc”,“def”,“dess”]
for i in a:
print(a)
###列表内部提供的其他功能
# append 追加
name_list.append("dir")
name_list.append("dir")
name_list.append("dir")
print(name_list)
# count 元素统计出现次数
print(name_list.count('dir'))
# iterable 可迭代的
temp = ['22','33','44']
# 扩展,批量添加
name_list.extend(temp)
print(name_list)
# index 获取指定元素的索引位置
print(name_list.index('dess'))
# insert向指定索引位置插入数据
name_list.insert(2,'567')
print(name_list)
# pop移除,在原列表中移除掉最后一个元素,并将值赋值给b
b = name_list.pop()
print(name_list)
print(b)
# remove 移除某个元素
name_list.remove('33')
print(name_list)
# reverse 反转倒置
name_list.reverse()
print(name_list)
#删除指定索引位置
del name_list[1:3]
print(name_list)
######## 元组 #######
#!usr/bin/env python
#-*- coding utf-8-*-#
####### 元组 #######
name_tuple= ("abc","dfg","efc")
### 索引 ###
print(name_tuple[1])
### len ###
print(name_tuple[len(name_tuple)-1])
### 切片 ###
print(name_tuple[0:2])
### for ###
for i in name_tuple:
print(i)
### 删除 ###
del name_tuple[1] ##不支持
### count ### 计算元素出现个数
print(name_tuple.count('efc'))
### index ### 获取指定元素的索引位置
print(name_tuple.index('efc'))
#!usr/bin/env python
#-*- coding utf-8 -*-
#字典的每一个元素,键值对
user_info = {
'name': 'guest',
'age': 36,
'home': 'ZJ'
}
# 索引
print(user_info['age'])
# 循环,默认值输出key
for i in user_info:
print(i)
# 获取所有键
print(user_info.keys())
# 获取所有值
print(user_info.values())
#获取所有键值对
print(user_info.items())
for a,b in user_info.items():
print(a)
print(b)
# clear 清楚所有内容
# user_info.clear()
# print(user_info)
# get根据Key获取值,如果key不存在,可以指定一个默认值
val = user_info.get('age')
print(val)
val = user_info.get('age111','很抱歉找不到你要的')
print(val)
# has_key 检查字典中指定key是否存在(python 3中无)
ret = 'age' in user_info.keys()
print(ret)
# update
print(user_info)
gu = {
"asd": 456,
"rfv": 789
}
user_info.update(gu)
print(user_info)
# 删除指定索引的键值对
del gu['rfv']
print(gu)