学习笔记,仅供参考,觉得哪里不错就记哪里
使用工具:Jupyter Notebook(IPython notebook)
学习书目:《python编程从数据分析到数据编程》–朝乐门;
文章目录
isinstance方法
isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
-
isinstance() 与 type() 区别
-
type() 不会认为子类是一种父类类型,不考虑继承关系。
-
isinstance() 会认为子类是一种父类类型,考虑继承关系。
-
如果要判断两个类型是否相同推荐使用 isinstance()。
-
- 举个例子
输入:
list1 = [1, 2, 3]
isinstance(list1, list)
int1 = 100
isinstance(int1, int)
输出:
True
True
科学计数法
- 举个例子
输入:
1.23e2
输出:
123.0
数据类型
查看python关键字
- 举个例子
输入:
import keyword
keyword.kwlist
输出:
['False',
'None',
'True',
'and',
'as',
'assert',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield']
查看已定义的所有变量
- 举个例子
输入:
dir()
输出:
['In',
'InteractiveShell',
'Out',
'_',
'_1',
'_10',
'_11',
'_12',
'_13',
'_14',
'_15',
'_16',
'_19',
'_2',
'_20',
'_21',
'_22',
'_3',
'_4',
'_5',
'_6',
'_7',
'_8',
'_9',
'__',
'___',
'__builtin__',
'__builtins__',
'__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_dh',
'_i',
'_i1',
'_i10',
'_i11',
'_i12',
'_i13',
'_i14',
'_i15',
'_i16',
'_i17',
'_i18',
'_i19',
'_i2',
'_i20',
'_i21',
'_i22',
'_i23',
'_i3',
'_i4',
'_i5',
'_i6',
'_i7',
'_i8',
'_i9',
'_ih',
'_ii',
'_iii',
'_oh',
'_sh',
'exit',
'get_ipython',
'int1',
'keyword',
'list1',
'quit',
'test']
删除变量
我们可以用del删除变量x,但是需要注意的是,del是语句,而不是函数,写成del(x)会报错。
- 举个例子
删除变量x:
del x
if与三元运算符
语法:
item = "变量1" if a > b else "变量2"
上面这段代码表达的意思是,如果a>b的结果为真,则item被赋值为"变量1",如果为假,则item被赋值为"变量2"
count方法
字符串中的应用
count()方法用于统计字符串里某个字符出现的次数.
语法:
str.count(sub, start= 0,end=len(string))
#sub:搜索的子字符串
#start:字符串开始搜索的位置,默认为第一个字符,第一个字符索引值为0。
#end:字符串中结束搜索的位置,字符中第一个字符的索引为 0,默认为字符串的最后一个位置。
- 举个例子
输入:
"aaabbc".count("a")
输出:
3
元祖中的应用
在python元组中,count() 方法用于统计某个元素在元祖中出现的次数。
语法:
T.count(obj)
#obj:元祖中统计的对象
- 举个例子
输入:
T = (10, 'Google', 10, 'Google', 10);
T.count('Google')
输出:
2
列表中的应用
在python列表中,count() 方法用于统计某个元素在列表中出现的次数。
语法:
list.count(obj)
#obj:列表中统计的对象
- 举个例子
输入:
list1 = list("AnhuiCaiJingDaXue")
list1.count("i")
输出:
3
列表中的sort应用
在列表中,我们可以按照字符串首字母顺顺序排序,也可以利用lambda表达式,指定字符串长度或者字符串中不同元素的数量作为衡量排序的标准。
- 举个例子
In [22]: str1 = ["abbee", "bbbbffff", "python", "c"]
In [23]: str1.sort()
In [24]: str1
Out[24]: ['abbee', 'bbbbffff', 'c', 'python']
In [25]: str1.sort(key = lambda x : len(x))
In [26]: str1
Out[26]: ['c', 'abbee', 'python', 'bbbbffff']
In [29]: str1.sort(key = lambda x : len(set(x)))
In [30]: str1
Out[30]: ['c', 'bbbbffff', 'abbee', 'python']
定义字典时出现重复的键
如果在我们定义字典时,两个values出现相同的key,会发生什么事情呢?情看下面的代码:
In [31]: {"a":3, "a":2, 10:"hhh"}
Out[31]: {'a': 2, 10: 'hhh'}
数据科学中,常用的基础包
在数据分析和数据科学项目中,常用的基础包包括如下几种: