数据类型相关练习
数据类型相关练习
# str->int 字符串(str) Python的主要数值类型是int和float。int可以存储任意⼤的数
#Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型。
# type(dict)语法
X=int('ABCD',16)
print(X)
type(X)#区分大小写字母
# float -> str
a = 5.99
b = str(a)
print(b)
# int -> float
a = 520.11
d = float(a)
d
print(d)
# float-> int
a = 520.11
e = int(a)
e
print(e)
a_string='hello'+''+"Women Who Code!"
print(a_string)
print("str[0]:"+a_string[0])#0位置开始就是H
print("str[2:5]:" + a_string[2:5])#第二个位置开始是l,2~5位110
print("str[2:] :" + a_string[2:])#第二个位置开始后后面所有
43981
5.99
520.11
520
helloWomen Who Code!
str[0]:h
str[2:5]:llo
str[2:] :lloWomen Who Code!
列表操作
#列表是个框,什么都可以往里装
lis = [ "WWCode", 786 , 2.23, 'singapore', 70.2 ]
print(lis[0:3])
type(lis)
#列表的索引,先猜猜下面这个单元格能得到什么,再运行
lis = [ "WWCode", 786 , 2.23, 'singapore', 50 ]
print(lis[3][0:4])#0开始,列表的第四个字符,取其中四位数
print(lis[3][0:4])
lis[2] = 3.3
print(lis)#第二位替换成3.3
symbols = '$¢£¥€¤'
codes = [ord(symbol) for symbol in symbols]
codes
['WWCode', 786, 2.23]
sing
sing
['WWCode', 786, 3.3, 'singapore', 50]
[36, 162, 163, 165, 8364, 164]
元组操作
t1 = ( "WWCode", 100000 , 0.5 ) #
t2 = 'Singapore', 1160.5
t_singleton = ('We',)
t_empty = ()
print(type(t1)); print(type(t2))
print(t_singleton);
type(t_empty)
#元组不支持对象赋值,Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可
<class 'tuple'>
<class 'tuple'>
('We',)
tuple
字典操作
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中
dict1 = {'name':'Women Who Code Singapore',
'org':'WWCode',
'city':'Singapore',
'members':1260}
print(dict1['org'])
type(dict1)
# 增加元素
dict1['rank'] = 10
dict1['abcde']=10000
print(dict1)
# 获取元素
dict1['org']
print(dict1['org'])
# 获取不知是否存在的元素
dict1.get('org','不存在')
# 获取不知是否存在的元素
dict1.get('ord','不存在')#如果不存在则返回‘不存在’
WWCode
{'name': 'Women Who Code Singapore', 'org': 'WWCode', 'city': 'Singapore', 'members': 1260, 'rank': 10, 'abcde': 10000}
WWCode
'不存在'
Sets 集合
集合(set)是一个无序的不重复元素序列。
可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
创建格式:parame = {value01,value02,...}
或者
set(value)
wwcode_asia_networks = {'Bangalore','Beijing','Chennai','Delhi','Gujarat','Hong Kong','Kuala Lumpur'}
type(wwcode_asia_networks)
print(wwcode_asia_networks)
#移除元素
wwcode_asia_networks .remove('Delhi')
print(wwcode_asia_networks)
#随机删除元素
wwcode_asia_networks.pop()
print(wwcode_asia_networks)
#判断元素在不在里面
t =set(("Bangalore","Beijing"))
'Hong Kong' in t
#往集合里增加元素
wwcode_asia_networks.update({1,3})
wwcode_asia_networks=t
print(t)
len(wwcode_asia_networks)#计算元素的个数
#往集合里增加元素
wwcode_asia_networks=t
t.add(500)
print(t)
#清空集合
wwcode_asia_networks.clear()
print(wwcode_asia_networks)
运算和布尔运算
#运算符 描述 实例
= 简单的赋值运算符 c = a + b 将 a + b 的运算结果赋值为 c
+= 加法赋值运算符 c += a 等效于 c = c + a
-= 减法赋值运算符 c -= a 等效于 c = c - a
*= 乘法赋值运算符 c *= a 等效于 c = c * a
/= 除法赋值运算符 c /= a 等效于 c = c / a
%= 取模赋值运算符 c %= a 等效于 c = c % a
**= 幂赋值运算符 c **= a 等效于 c = c ** a
//= 取整除赋值运算符 c //= a 等效于 c = c // a
x = 1 + 2 # 加
y = 3 - 4 # 减
z = 5 * 6 # 乘
a = z / y # 除
b = z % x # 余数
c = y ** x # 指数X是y的指数
d = c // x # 取整除 - 向下取接近商的整数
print(b)
print("x:" + str(x) + " y:" + str(y) + " z:" + str(z) +
" a:" + str(a) + " b:" + str(b) + " c:" + str(c) + " d:" + str(d))
#指数
x=2
y=4
c=x**y
print(c)
#指数
x=9
y=2
c=x//y
print(c)# 取整除 - 向下取接近商的整数 9/2=4.5取整故4
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
0
x:3 y:-1 z:30 a:-30.0 b:0 c:-1 d:-1
16
4
False
True
False
True
False
True
#Logical Operators 逻辑运算符and or not
#数组,元组,列表,集合,赋值后都适用于and or not 逻辑
a_string = "Women Who Code"
print("Women" in a_string)
print("Men" not in a_string)
print(len("Women Who Code") is len(a_string))
print(len("Hello World!") is not len(a_string))
# 数组比较
#is 和 is not 运算符 与==以及!=的区别
#is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)
print(x is y)
print(id(x))
print(id(y))
# 元组比较
#id() 函数返回对象的唯一标识符,标识符是一个整数。
#Python 中 id() 函数用于获取对象的内存地址
x = (1, 2, 3)
y = (1, 2, 3)
print(x == y)
print(x is y)
print(id(x))
print(id(y))
# 字典比较
x = {"id": 1, "name": "Tom", "age": 18}
y = {"id": 1, "name": "Tom", "age": 18}
print(x == y)
print(x is y)
print(id(x))
print(id(y))
# 集合比较
x = set([1, 2, 3])
y = set([1, 2, 3])
print(x == y)
print(x is y)
print(id(x))
print(id(y))
# 赋值后比较
x = [1, 2, 3]
y = x
print(x == y)
print(x is y)#对象相等,所以是ture
print(id(x))
print(id(y))
#空值比较
none_type = None
none_type is None
True
True
True
True
True
False
1824923598400
1824927357440
True
False
1824934194112
1824923855360
True
False
1824931618560
1824923598208
True
False
1824925829376
1824935470688
True
True
1824927358016
1824927358016
True