[Python Crash Course] Bisics_Type of Data
程序员文章站
2022-07-14 12:46:24
...
目录
-
String
# About String #
name = "this is a string."
print("name:", name)
# 输出格式 #
print("title():", name.title()) # 以标题形式输出,首字母大写
print("upper():", name.upper()) # 全大写
print("lower():", name.lower()) # 全小写
# 字符串连接 #
print(name + " this is the additional part.")
# 空格;换行 #
print("tab:hello\tworld")
print("换行:hello\nworld")
# 删除多余空格 #
Extra = " Hello World "
print("删右侧空格:", Extra.rstrip())
print("删左侧空格:", Extra.lstrip())
print("删两侧空格:", Extra.strip())
# 强制类型转换 #
number = 12
print("number->string:", str(number))
-
Number
# About Number #
print("乘方**:", 3**2)
print("求余%:", 5 % 2)
# About Float #
print("奇妙的多余部分:", 0.2+0.1)
-
List
# About List #
names = ['happy', 'each', 'day']
print("names:", names)
# 提取元素 #
print("正数:", names[0])
print("倒数:", names[-1]) # -number:倒数第几个
# 添加 #
names.append('self')
print("append():", names)
# 插入 #
names.insert(1, 'life')
print("insert():", names)
# 删除 #
del names[-1] # 知位置
print("del:", names)
names.remove('day') # 知元素;有相同时仅删第一个
print("remove():", names)
# 出栈 #
print("pop():", names.pop()) # 出栈后该元素在list中删除
# 出入栈 #
popName = []
while names:
name = names.pop()
popName.append(name)
print("倒序入栈:", popName)
# 排列 #
names.sort() # 永久改变
print("sort()正序:", names)
names.sort(reverse=True)
print("sort()倒序:", names)
print("sorted()正序:", sorted(names)) # 表面改变
print("sorted()倒序:", sorted(names, reverse=True))
print("reverse()完全倒序:", names.reverse()) # 在原基础上倒序;永久改变
# 长度 #
print("len():", len(names))
# 循环输出 #
for name in names:
print(name) # 换行输出
# 检测 #
print("WhetherInList:")
if 'happy' in names: # not in
print("Yes")
else: # elif
print("No")
-
Part of List
# About Part of a List #
names = ['happy', 'each', 'day', 'with', 'friends']
print("[0:2]:", names[0:2])
print("[-3:]:", names[-3:])
# 复制 #
copy = names[:]
print("CopyList:", copy) # copy = names -> 两个名称指向同一list
-
List of Number
# About List of Numbers #
# 范围 #
for value in range(1, 5): # 左闭右开区间
print(value)
# 构造list #
numbers = list(range(1, 10, 2)) # (low,high,skip)
print("RangeList:", numbers)
# 更新list #
# *function one
squares = []
for value in numbers:
square = value**2
squares.append(square) # OR square.append(value**2)
print("SquareList:", squares)
# *function two
cubes = [value**3 for value in numbers]
print("CubeList:", cubes)
# 简单运算 #
print("min():", min(numbers))
print("max():", max(numbers))
print("sum():", sum(numbers))
-
Tuple
# About Tuple #
dimensions = (1, 2, 3)
print("tuple:", dimensions) # tuple不支持元素单独赋值,只支持整个覆盖
-
Dictionary
# About Dictionary #
person = {'hair': 'red', 'color': 'blue'}
print("person", person)
print("hair:", person['hair'])
# 添加 #
person['height'] = 'high'
print("AfterAdd:", person)
# 修改 #
person['height'] = 'low'
print("AfterModify:", person)
# 删除 #
del person['height']
print("AfterDelete:", person)
# 循环 #
print("# KeyAndValue:")
for key, value in person.items(): # for k,v in person.item()
print(key, ":", value)
print("# Name:")
for factor in person.keys():
print(factor)
# 排序 #
print("# KeySorted:")
for factor in sorted(person.keys()):
print(factor)
print("# ValueSorted:")
for feature in sorted(person.values()):
print(feature)
# 集合 #
print("# set():")
for feature in set(person.values()): # 建立集合:元素各不相同,有相同只出一个
print(feature)
# 嵌套 #
print("# dictionaries -> list:")
dic_1 = {'num': '1', 'name': 'a'}
dic_2 = {'num': '2', 'name': 'b'}
dic_3 = {'num': '3', 'name': 'c'}
lists = [dic_1, dic_2, dic_3]
for dic in lists:
print(dic)
# 新增 #
print(" # AfterAdd")
for dic in range(2): # range(n) 新增n个相同的dic
dic_4 = {'num': '4', 'name': 'd'}
lists.append(dic_4)
print(lists)
# 计数 #
print("TotalNumber:", str(len(lists))) # Notice:str
# 定向修改 #
print(" # SearchModify")
for dic in lists[0:3]:
if dic['num'] == '1':
dic['name'] = 'x'
print(dic_1)
print("# list -> dictionary")
food = {'taste': 'sweet', 'fruits': ['melon', 'grape', 'peach']}
print(" # element in dic:")
for fruit in food['fruits']:
print(fruit)
print("# dictionary -> dictionary:")
colors = {'warm': {'light': 'pink', 'dark': 'red'}}
print("colors:", colors)
print(" # item:")
for color in colors['warm']:
print(color)
# 嵌套 #
print("# dictionaries -> list:")
dic_1 = {'num': '1', 'name': 'a'}
dic_2 = {'num': '2', 'name': 'b'}
dic_3 = {'num': '3', 'name': 'c'}
lists = [dic_1, dic_2, dic_3]
for dic in lists:
print(dic)
# 新增 #
print(" # AfterAdd")
for dic in range(2): # range(n) 新增n个相同的dic
dic_4 = {'num': '4', 'name': 'd'}
lists.append(dic_4)
print(lists)
# 计数 #
print("TotalNumber:", str(len(lists))) # Notice:str
# 定向修改 #
print(" # SearchModify")
for dic in lists[0:3]:
if dic['num'] == '1':
dic['name'] = 'x'
print(dic_1)
print("# list -> dictionary")
food = {'taste': 'sweet', 'fruits': ['melon', 'grape', 'peach']}
print(" # element in dic:")
for fruit in food['fruits']:
print(fruit)
print("# dictionary -> dictionary:")
colors = {'warm': {'light': 'pink', 'dark': 'red'}}
print("colors:", colors)
print(" # item:")
for color in colors['warm']:
print(color)
上一篇: 【Week 15】Sklearn练习题
下一篇: Android的布局管理--帧布局
推荐阅读
-
[Python Crash Course] DataVisual_RandomWalk
-
Python学习笔记之Python Crash Course
-
Python Crash Course读书笔记 - 第20章:STYLING AND DEPLOYING AN APP
-
[Python Crash Course] Bisics_Type of Data
-
Python Crash Course读书笔记 - 第13章:ALIENS!
-
Python Crash Course读书笔记 - 第15章:GENERATING DATA
-
Python Crash Course读书笔记 - 第16章:DOWNLOADING DATA