目录
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、设置格式
format()什么情况下用:如果想在字符串中使用变量的值可以在引号前面加一个f,再将想要引用的值放在引号里面,这样Python在显示字符串的时候就会把每个变量转换为变量的值。
>>> a='我在'
>>> b
>>> b='学习'
>>> print(f"{a}{b}Pyhon")
我在学习Pyhon
>>>
二、序列间转换
1.序列:列表,元组,字符串
1.可通过索引得到每一个值
2.默认索引值从0开始
3.可通过分片得到一个范围内的元素的集合
4.有很多共同操作符(重复操作符,拼接操作符,成员关系操作符)
2.BIF和运算符
is运算符:判断俩元素是否是同一个对象
in运算符:判断某元素是否包含在序列中
del 删除序列中指定对象
'str' 对象不支持删除项目
>>> x="multiple statements found while compiling a"
>>> del x[:9]
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
del x[:9]
TypeError: 'str' object does not support item deletion
>>> b= list(x)
>>> x
'multiple statements found while compiling a'
>>> b
['m', 'u', 'l', 't', 'i', 'p', 'l', 'e', ' ', 's', 't', 'a', 't', 'e', 'm', 'e', 'n', 't', 's', ' ', 'f', 'o', 'u', 'n', 'd', ' ', 'w', 'h', 'i', 'l', 'e', ' ', 'c', 'o', 'm', 'p', 'i', 'l', 'i', 'n', 'g', ' ', 'a']
>>> del b[:9]
>>> b
['s', 't', 'a', 't', 'e', 'm', 'e', 'n', 't', 's', ' ', 'f', 'o', 'u', 'n', 'd', ' ', 'w', 'h', 'i', 'l', 'e', ' ', 'c', 'o', 'm', 'p', 'i', 'l', 'i', 'n', 'g', ' ', 'a']
>>> del b
>>> b
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
b
NameError: name 'b' is not defined
>>>
list(可迭代元素):可迭代元素转换为列表
>>> a='我在'
>>> list(a)
['我', '在']
>>>
tuple(可迭代元素):可迭代元素转换为元组
str(元素):可迭代元素转换为字符串
>>> b= list(a)
>>> b
['我', '在']
>>> str(b)
"['我', '在']"
>>> str(4)
'4'
>>>
max()最大值
min()最小值
sum()求和
>>> x
'multiple statements found while compiling a'
>>> max(x)
'w'
>>> x
'multiple statements found while compiling a'
>>> b={}
>>> min(b,default="空啥也没")
'空啥也没'
>>> min(b)
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
min(b)
ValueError: min() arg is an empty sequence
>>> min(x,default="空啥也没")
' '
>>> s=[1,23,4,5,6,7,8,9,]
>>> sum(s)
63
>>> sum(s,start=3)#从start=3开始加
66
>>>
sorted()原地排序不改原值(sort会改原值,永久性排序)
>>> b
'qwertyuiop'
>>> sorted(b)
['e', 'i', 'o', 'p', 'q', 'r', 't', 'u', 'w', 'y']
>>> c=list(b)
>>> b
'qwertyuiop'
>>> c
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
>>> sorted(c)
['e', 'i', 'o', 'p', 'q', 'r', 't', 'u', 'w', 'y']
>>>
>>> reversed(b)
<reversed object at 0x0000020F07CCCFD0>
>>> list(reversed(b))
['p', 'o', 'i', 'u', 'y', 't', 'r', 'e', 'w', 'q']
>>> a="123456"
>>> list(reversed(a))
['6', '5', '4', '3', '2', '1']
>>>
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。