数据类型与数据结构

1.变量

1.1 整数和浮点数

x=5

# x 是对象名字,5是value,即对象值,常见值得类型为整数,浮点数,字符串,布尔值等。

#输出函数print,获取当前变量取值类型为type()
print(type(x))
# 整数(int) - 通常被称为是整型或整数,是正或负整数,不带小数点。
<class 'int'>
f=5.1
# 浮点型(floating point real values) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
print(type(f))
<class 'float'>

1.2 字符串

s1="5hello,wolrd"
s2="hello"
print(type(s))
<class 'str'>
# 增加
s3=s1+s2
print(s3)
5hello,wolrdhello
# 查
s3[0:3]
'5he'

1.3布尔值

b=True
print(type(b))
<class 'bool'>

2.1数据结构列表

  • 2.1.1列表简介
    • 列表是一个有序的,可变的(changeable)值集合,这些值被“逗号分隔 ,”并用“方括号[]”括起来。 列表可以包含许多不同类型的变量(下面是带有整数,字符串和浮点数的列表)。

在这里插入图片描述

list1=[3,"hello",1.2]
print(list1)
[3, 'hello', 1.2]
print(type(list1))
<class 'list'>
# 查看列表长度
len(list1)
3
  • 2.1.2 列表得增删改查,增
#使用函数为append函数
# 两个数组合并 +
test=["dsd"]
list1.append(test)
print(list1)
[3, 'hello', 1.2, ['dsd']]
a=[212132,453]
b=[43434]
z=a+b
print(z)
[212132, 453, 43434]
  • 2.1.3 查(索引和切片)
    • 索引和从列表中切片允许我们在列表中检索特定的值。 注意,索引可以是正数(从0开始),也可以是负数(-1及以下,其中-1是列表中的最后一项)。
list1[0]
3
list1[0:2]
[3, 'hello']
t=[*range(100)]
# t[start:end:width]
t[1:100:2]

[1,3,5,7,9…99]

  • 2.1.4 改
list1[0]="first"
list1
['first', 'hello', 1.2]
  • 2.15 删除
list2=['first', 'hello', 1.2]

# pop()可删除列表末尾的元素
list2.pop()

list2
['first', 'hello']
#使用del语句删除指定位置的元素
list2=['first', 'hello', 1.2]
del list2[0:1]

list2
['hello', 1.2]
#使用remove语句删除指定位置的元素,只能删除一个
list2=['first', 'hello', 1.2]
list2.remove("first")
list2
['hello', 1.2]

2.2 元组-Tuples

  • 元组是有序的、不可变的集合。 您将使用它们来存储永远不会更改的值。使用得符号为()
x=(3,2,"hello")
## 2.2.1 增
x=x+(5.6,4)
x
(3, 2, 'hello', 5.6, 4)
print(type(x))
<class 'tuple'>

2.3 字典-Dictionaries

  • 字典是键值对的无序、可变和索引的集合。 可以根据键检索值,而字典不能有两个相同的键
person = {'name': 'Goku',
          'eye_color': 'brown',"age":18,"sex":"male"}
person["name"]
'Goku'
  • 2.3.1 改
person["name"]="baol"
person
{'name': 'baol', 'eye_color': 'brown', 'age': 18, 'sex': 'male'}
  • 2.3.2 增
person["high"]="165"
person
{'name': 'baol', 'eye_color': 'brown', 'age': 18, 'sex': 'male', 'high': '165'}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值