int(x) 将x转换为一个整数。
float(x) 将x转换到一个浮点数。
complex(x) 将x转换到一个复数,实数部分为 x,虚数部分为 0。
complex(x, y) 将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y。x 和 y 是数字表达式。
# -*- coding: utf-8 -*-
data = '123'
print(int(data)+4)
#round() 方法返回浮点数 x 的四舍五入值,准确的说保留值将保留到离上一位更近的一端(四舍六入)。
#round( x [, n] )
#x -- 数字表达式。
#n -- 表示从小数点位数,其中 x 需要四舍五入,默认值为 0。
print ("round(80.264, 2) : ", round(80.264, 2))
ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5
floor(x) 返回数字的下舍整数,如math.floor(4.9)返回 4
max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。
min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。
random() 随机生成下一个实数,它在[0,1)范围内。
假设随机数范围:1000-9999
# -*- coding: utf-8 -*-
import random
import math
# data = '123'
# print(int(data)+4)
code = math.floor(random.random()*9000)+1000
print(code)
# [0,1) => [0,9000) =>最终想要的 [1000,9999]
注意:random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
shuffle(lst) 将序列的所有元素随机排序
#!/usr/bin/python3
import random
list = [20, 16, 10, 5];
random.shuffle(list)
print ("随机排序列表 : ", list)
random.shuffle(list)
print ("随机排序列表 : ", list)
从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。
#choice 四位随机数
print(random.choice(range(1000,10000)))
random.randrange ([start,] stop [,step])
start -- 指定范围内的开始值,包含在范围内。
stop -- 指定范围内的结束值,不包含在范围内。
step -- 指定递增基数。
random.randrange (1000,10000, 1)
print(math.pi)
print(math.e)
var1 = 'Hello World!'
print(var1[1]) 取第二位
print(var1[1:]) 取第二位到结尾
print(var1[1:5]) 取第二位值第五位
print(var1[:5]) 取第五位前面的所有
print("\"")
print('\'')
print("'")
print('"')
print('\n')
a = "hello"
b = 'world'
print(a + b)
print(a*2)
if 'he' in a:
print("he存在a中")
print(r'\n') // 转义字符不被转义
print(b'aaa') // 字节编码
u'input\n' # unicode编码字符,python3默认字符串编码方式。
f/format():格式化操作 py3.6+
name = 'Runoob'
f'Hello {name}' # 替换变量
age = 19
print("年龄={:}".format(age))
#!/usr/bin/python3
print ("我叫 %s 今年 %d 岁!" % ('小明', 10))
#!/usr/bin/python3
para_str = """这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( \t )。
也可以使用换行符 [ \n ]。
"""
print (para_str)
#!/usr/bin/python3
str = "this is string example from runoob....wow!!!"
print ("str.capitalize() : ", str.capitalize())
count(str, beg= 0,end=len(string))
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
#!/usr/bin/python3
str="www.runoob.com"
sub='o'
print ("str.count('o') : ", str.count(sub))
sub='run'
print ("str.count('run', 0, 10) : ", str.count(sub,0,10))
index(str, beg=0, end=len(string))
a = "hello"
print(a.index('o'))
#!/usr/bin/python3
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))
a = " hello"
b = 'world'
print(a.index('o'))
print(a.lstrip())
print(b.lstrip('wo'))
a = " hello"
b = 'world'
print(a.index('o'))
print(a.rstrip())
print(b.rstrip('d'))
把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次。
str.replace(old, new[, max])
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
#!/usr/bin/python3
str = "www.w3cschool.cc"
print ("菜鸟教程旧地址:", str)
print ("菜鸟教程新地址:", str.replace("w3cschool.cc", "runoob.com"))
str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))
#!/usr/bin/python3
str = "this is string example from runoob....wow!!!";
print ("str.upper() : ", str.upper())
#!/usr/bin/python3
str = "Runoob EXAMPLE....WOW!!!"
print( str.lower()
数组,存放多个数据的,一队,一列一组,支持n维数组
#!/usr/bin/python3
list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])
对某个索引的值进行重新赋值
#!/usr/bin/python3
list = ['Google', 'Runoob', 1997, 2000]
print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])
#!/usr/bin/python3
list = ['Google', 'Runoob', 1997, 2000]
print ("原始列表 : ", list)
del list[2]
print ("删除第三个元素 : ", list)
[1, 2, 3] + [4, 5, 6] => [1, 2, 3, 4, 5, 6] 组合
['Hi!'] * 4 =>['Hi!', 'Hi!', 'Hi!', 'Hi!'] 重复
3 in [1, 2, 3]=> True 元素是否存在于列表中
for x in [1, 2, 3]: print(x, end=" ") =>1 2 3 迭代
len(list)
list(seq)
list.append(obj)
重点在末尾添加
list.count(obj)
#!/usr/bin/python3
list1 = ['Google', 'Runoob', 'Taobao']
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2) # 扩展列表
print ("扩展后的列表:", list1)
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
类似于出栈
#!/usr/bin/python3
list1 = ['Google', 'Runoob', 'Taobao']
list1.pop()
print ("列表现在为 : ", list1)
list1.pop(1)
print ("列表现在为 : ", list1)
#!/usr/bin/python3
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.remove('Taobao')
print ("列表现在为 : ", list1)
list1.remove('Baidu')
print ("列表现在为 : ", list1)
#!/usr/bin/python3
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.reverse()
print ("列表反转后: ", list1)
Python 的元组与列表类似,不同之处在于元组的元素不能修改。 业务需求,某变量 不让更改的话,可以定义为元组
字典是另一种可变容器模型,且可存储任意类型对象。 键值对
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
#!/usr/bin/python3
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])
#!/usr/bin/python3
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
del dict['Name'] # 删除键 'Name'
dict.clear() # 清空字典
del dict # 删除字典
print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住, 2)键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行
person = {
'name':'zhangsan',
'gender':'female',
'age':18
}
# 判断键是否存在
if 'address' in person:
print(person['address'])
else:
print(' 键address 不存在')
## 循环
dict = {'Name': 'Runoob', 'Age': 7}
print("Value : %s" % dict.items())
for var in dict.items():
print(var)
for var in dict.keys():
print(dict[var])
for var in dict.values():
print(var)
集合(set)是一个无序的不重复元素序列。 场景:用于过滤重复保证一组里面唯一。
list = [1,2,1,2,2,3]
#空的集合
set_empty = set()
#有内容的集合
set_content = {1,2,3,4}
print(set_content)
dump_set = {1,1,1,2,2,2,3}
print(dump_set)
len(set)
s.add(x)
s.remove( x )
s.clear()
END