02 基本数据类型(数字、字符串)

一、基本数据类型
1、数据类型
● Python3中有六个标准的数据类型
● Number(数字)
● String(字符串)
● List(列表)
● Tuple(元组)
● Dictionary(字典)
● Set(集合)
2、数字
2.1 数字类型分为:
● 整型(int):Python3整型没有限制大小,可以当作Long类型使用,Python3没有Python2的Long类型。
● 浮点型(float):浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 10² = 250)
2.2 数组类型转化
● int(x) 将x转为一个整数
● float(x) 将x转为浮点数
2.3 数字运算
print(2+2)
print(50-5*6)
print(50-5*6)/4
print(8/5)
在整数除法中,除法 / 总是返回一个浮点数,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 // :
17 / 3 # 整数除法返回浮点型,结果为5.666666666666667
17 // 3 # 整数除法返回向下取整后的结果 5
17 % 3 # %操作符返回除法的余数 2
// 得到的并不一定是整数类型的数,它与分母分子的数据类型有关系。
7//2
3
7.0//2
3.0
7//2.0
3.0
可以使用 \ 操作来进行幂运算:
5 ** 2 # 5 的平方
数学函数:
函数 返回值 ( 描述 )
abs(x) 返回数字的绝对值,如abs(-10) 返回 10
ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5
floor(x) 返回数字的下舍整数,如math.floor(4.9)返回 4
max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。
min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。
pow(x, y) x**y 运算后的值。
round(x,n) 返回浮点数 x 的四舍五入值,如给出 n 值,则代表舍入到
小数点后的位数。
随机数函数:
import random
print(random.choice([2,3,4,33,22,66,5,7,4])) #从列表(也可以是元组、字符串)中获取一个随机内容
print (random.randrange(1, 100, 2)) # 从 1-100 中选取一个奇数,2表示递增基数
print (random.randrange(100)) # 从 0-99 选取一个随机数
print(random.random()) #返回随机生成的一个实数,它在[0,1)范围内
list = [20, 16, 10, 5];
random.shuffle(list) #将序列的所有元素随机排序
print ("随机排序列表 : ", list)
print (random.uniform(5, 10)) #返回一个浮点数 N,取值范围为如果 x<y 则 x <= N <= y,如果 y<x 则y <= N <= x
三角函数:
数 描述
acos(x)
返回x的反余弦弧度值。
asin(x)
返回x的反正弦弧度值。
atan(x)
返回x的反正切弧度值。
atan2(y, x)
返回给定的 X 及 Y 坐标值的反正切值。
cos(x)
返回x的弧度的余弦值。
hypot(x, y)
返回欧几里德范数 sqrt(x*x + y*y)。
sin(x)
返回的x弧度的正弦值。
tan(x)
返回x弧度的正切值。
degrees(x)
将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
radians(x)
将角度转换为弧度
数学常量:
常量 描述
pi 数学常量 pi(圆周率,一般以π来表示)
e 数学常量 e,e即自然常数(自然常数)。
3、字符串
3.1 引号
我们可以使用引号( ' 或 " )来创建字符串。
print('hello world')
print("hello world")
print('hello "world"')
print("It's my dog")
3.2 访问字符串中的值
[image]
3.3 转义字符
在需要在字符中使用特殊字符时,python 用反斜杠 \ 转义字符。
print("\\") #结果\
print('\'') #结果'
print("\"") #结果"
print("\n") #换行
3.4 字符串运算符
print("a"+"b") #字符串连接
a="hello"
print(a*2) #hellohello
print(a[1:4]) #ell
print("h" in a) #True
print("h" not in a) #Flase
3.5 字符串格式化
%s:字符串
%d:整数
print ("我叫 %s 今年 %d 岁!" % ('小明', 10))
3.6 三引号
三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
para_str = """这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( \t )。
也可以使用换行符 [ \n ]。
"""
print (para_str)
3.7 f-string
f-string 是 python3.6 之后版本添加的,称之为字面量格式化字符串,是新的格式化字符串的语法,用了这种方式明显更简单了,
不用再去判断使用 %s,还是 %d。
name = 'Runoob'
f'Hello {name}' # 替换变量
'Hello Runoob'
f'{1+2}' # 使用表达式
'3'
w = {'name': 'Runoob', 'url': 'www.runoob.com'}
f'{w["name"]}: {w["url"]}'
'Runoob: www.runoob.com'
3.8 字符串内建函数
print("hello World".capitalize()) #将字符串的第一个字母变成大写,其他字母变小写
print("hello".center(20,"*")) #生成20个长度的字符串,hello居中,左右*补齐
print("hello".count("l",0,4)) #从位置0开始到位置4结束,l出现的次数
print("hello".encode('GBK','strict')) #以指定的编码格式编码字符串, 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError
print("hello China".startswith("he",0,10)) #从位置0到10,是否以he开头
print("hello China".endswith("he",0,10))
print("hello China".find("l",1,4)) #从位置1到4,从左到右,l出现的位置,返回索引值,不存在的话返回-1,rfind方法是从右到左
print("hello world".isalnum()) #如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
print("hello中国".isalpha()) #如果字符串至少有一个字符并且所有字符都是字母或文字则返回 True,否则返回 False。
print("aaa".isdigit()) #检测字符串是否只由数字组成
print("hello中国".islower()) #islower() 方法检测字符串是否由小写字母组成
print("hello中国".isupper()) #检测字符串中所有的字母是否都为大写
s1="hello"
print(s1.join("world")) #连接
print(len("hello")) #获取长度
print("hello".ljust(20,"*")) #获取20长度字符串,hello在左边,其余部分*补齐
print("Hello".lower()) #转小写
print("hello".upper()) #转大写
print(" hello".lstrip()) #结果"hello"
print("888hello".lstrip("8")) #结果hello
print(max("hello")) #获取最大字母
print(min("hello")) #获取最小字母
print("hello hello hello world".replace("hello","china"))
print("hello hello hello world".replace("hello","china",2)) #替换不超过 2 次
print("hello world ".rstrip()) #删除末尾空格
print("hello world....".rstrip(".")) #删除末尾.
print("hello world ".lstrip()) #删除末尾空格
print(" hello world ".strip()) #执行lstrip rstrip
print("hello world china".split(" ")) #["hello","world","china"]
print("hello,world,china".split(","))
print("abChina".swapcase()) #大写转小写,小写转大写
print("this is string".title()) #所有单词首字母大写

posted @   鸟择木栖  阅读(163)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示