Python‘s data type

Number

# 数据类型————数字
num_int = 1
num_float = 1.0
num_bool = True
num_complex = 3j

print("num_int的数据类型为{0},num_float的数据类型为{1},\
num_bool的数据类型为{2},num_complex的数据类型为{3}".format(type(num_int), type(num_float), type(num_bool),
                                               type(num_complex)))
# ************************************************************* #
num_int的数据类型为<class 'int'>,num_float的数据类型为<class 'float'>,num_bool的数据类型为<class 'bool'>,num_complex的数据类型为<class 'complex'>

String

# 字符串可以用‘’  “”  ‘’‘’‘’  “”“”“”四种格式来表示,后两者可以跨行
str1 = "ILovePython"
print(len(str1))  # 字符串长度
print(str1[0])  # 输出字符串中第一个字符
print(str1[-1])  # 输出字符串中倒数第一个字符
print(str1[0:3])  # 输出字符串中0 1 2三个字符

str2 = "AndILoveJava"
str3 = str1 + str2  # 合并两个字符串
print(str3)

# 字符串的大小写表示
str4 = "Study makes me happy"
print(str4.title())  # 每个单词的第一个字母大写
print(str4.upper())  # 所有字母大写
print((str4.lower()))  # 所有字母小写

# 抑制转义字符 r
location = r"D:\Python\code\a"
#*****************************************************************
11
I
n
ILo
ILovePythonAndILoveJava
Study Makes Me Happy
STUDY MAKES ME HAPPY
study makes me happy

List

players = ["Messi", "Ronaldo", "Harland", "DeBruyne"]
sports = ["football"]

# add
print("add******************************************************")
players.append("Mbappé")  # add an element at the end of the list
print(players)
players.extend(sports)  # add a list
print(players)  # before inserting "Azar"
players.insert(2, "Azar")  # insert an element in the list
print(players)  # after inserting "Azar"

# delete
print("delete******************************************************")
players = ["Messi", "Ronaldo", "Harland", "DeBruyne"]
del players[0]  # delete the element at the specified location
print(players)

players.pop()  # delete the element at the specified location,The default value is -1
print(players)

players.remove("Ronaldo")  # # delete the element at the specified name
print(players)

# check
print("check******************************************************")
players = ["Messi", "Ronaldo", "Harland", "DeBruyne"]
print(players[0])

# revise
players = ["Messi", "Ronaldo", "Harland", "DeBruyne"]
players[1] = "Enzo"
print(players)

# sort
print("sort******************************************************")
num_list = [4, 3, 5, 7, 48, 100]
num_list_sorted = sorted(num_list)  # temporary sort
print(num_list_sorted)
print(num_list)

# permanently sort
num_list.sort()
print(num_list)  # permanently sort

num_list.sort(reverse=True)  # reverse
print(num_list)
add******************************************************
['Messi', 'Ronaldo', 'Harland', 'DeBruyne', 'Mbappé']
['Messi', 'Ronaldo', 'Harland', 'DeBruyne', 'Mbappé', 'football']
['Messi', 'Ronaldo', 'Azar', 'Harland', 'DeBruyne', 'Mbappé', 'football']
delete******************************************************
['Ronaldo', 'Harland', 'DeBruyne']
['Ronaldo', 'Harland']
['Harland']
check******************************************************
Messi
['Messi', 'Enzo', 'Harland', 'DeBruyne']
sort******************************************************
[3, 4, 5, 7, 48, 100]
[4, 3, 5, 7, 48, 100]
[3, 4, 5, 7, 48, 100]
[100, 48, 7, 5, 4, 3]

Tuple

tuple1 = ("Java", "Python", "C#", "C++", "C", "Matlab")
# Once a tuple is created, it cannot be changed

Dict

alien_0 = {"color": "red", "point": "5"}
# add
alien_0["height"] = 150  # add a pair of key-value
print(alien_0)

# delete
del alien_0["point"]
alien_0.pop("color")
print(alien_0)

# check
print(alien_0["height"])
{'color': 'red', 'point': '5', 'height': 150}
{'height': 150}
150

Set

# Unordered and not repeated
set1 = {1, 2, 5, 7}

# add
set1.add(8)
print(set1)  # add a item

set1.update([1, 4, 5, 10])
print(set1)

# delete
set1.remove(1)
print(set1)

# an important function----Remove duplicate elements from the list
list1 = [1, 1, 1, 4, 5]
list1 = set(list1)
list1 = list(list1)
print(list1)
{1, 2, 5, 7, 8}
{1, 2, 4, 5, 7, 8, 10}
{2, 4, 5, 7, 8, 10}
[1, 4, 5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值