Python—基本数据类型
2024-09-05 08:55:54
一、核心数据类型:
- 数字(int整型、float浮点型、complex复数、bool布尔型)
- 字符串 str
- 列表(List)
- 元组(Tuple)
- 字典(Dictionary)
- 集合()
二、可变数据类型 和 不可变数据类型
可变数据类型(不可哈希):列表、字典、集合
- 可变有序:列表(通过下标可以找到)
- 可变无序:字典、集合
不可变数据类型(可哈希):字符串、元组、固定集合、bool、int
- 不可变有序:字符串、元组(通过下标可以找到)
- 不可变无序:固定集合
三、数据类型之 None 详解
与C不同,在python中是没有NULL,但存在相近意义的None。
print type(None) # <type 'NoneType'>
print dir(None) # ['__class__', '__doc__', '__init__', '__new__', '__repr__', '__setattr__', '__str__']
None是没有像len,size等属性的,要判断一个变量是否为None,直接使用
print None == 0 # False
print None == ' ' # False
print None == None # True
print None == False # False a = None
if a:
print "a is True" # 不打印
if a is None:
print "a is None" # 打印
if a == None:
print "a == None" # 打印
四、is 和 == 的区别
https://www.cnblogs.com/mika-blogs/p/10981239.html
数字
整数,浮点型数、字符串互相转换
# str(num) 将整型、浮点型转换成 字符串。
num1 = 66
num2 = 66.88
str1 = str(num1)
str2 = str(num2) # int(str) 函数将 符合整数的规范的字符串转换成 int 型。
str1 = '66'
num1 = int(str1) # float(str) 函数将 符合浮点型的规范的字符串转换成 float 型。
str2 = '66.88'
num2 = float(str2)
浮点数转换整数
from math import * print int(3.22) # 3 <type 'int'>
print int(3.66) # 3
print int(-3.22) # -3
print int(-3.66) # -3 print floor(3.22) # 3.0 <type 'float'>
print floor(3.66) # 3.0
print floor(-3.22) # -4.0
print floor(-3.66) # -4.0 print round(3.22) # 3.0 <type 'float'>
print round(3.66) # 4.0
print round(-3.22) # -3.0
print round(-3.66) # -4.0
bool布尔型
列表
创建列表
L = [] # 创建空列表的字面值:
L = [1, 2, 3] # 创建非空列表的字面值
列表的构造函数(括号里的是可迭代对象)
list() # 生成一个空的列表,等同于[]
list(iterable) # 用可迭代对象创建一个列表 arr1 = list("hello")
arr2 = list(range(1,10,2))
print arr1, arr2 # ['h', 'e', 'l', 'l', 'o'] [1, 3, 5, 7, 9]
最新文章
- ASP.NET Core 中文文档 第四章 MVC(01)ASP.NET Core MVC 概览
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- python——进程基础
- JS高级程序设计2nd部分知识要点2
- cisco 路由配置
- jquery 属性与css操作
- 《python基础教程》笔记之 字典
- HttpServletRequest 报错 myeclipese支持tomcat
- libvirt命令行文档
- Mybatis拦截器实现分页
- 【Harmony】概述
- 对于JSONObject,我只是临时抱佛脚
- QT 操作 excel 教程
- MyBatis笔记(一) 最简单的select
- python学习笔记(10)--组合数据类型(字典类型)
- Turbine——Hystrix集群监控
- CentOS乱码解决方法
- 终极解决方案:org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
- Java获取系统时间的四种方法
- Umbraco中使用Related Links显示内部链接和外部链接