1、python安装目录:在dos界面命令行输入:where python,可返回本机的python的安装路径。
2、C:\Users\Administrator>python --version 或者python -V 查看python版本
3、print(‘Hello‘, end=‘‘) #输出不换行
x=5
x??
. %timeit
多次执行一条语句,并返回平均时间,%%timeit->多条语句
. %time
返回执行一条语句的时间,%%time->多条语句
. %rest
删除当前空间的全部变量
. %run *.py
在IPython中执行Python脚本
. %魔术命令+(?)显示文档
如:%time?
%timeit [x for x in range(10)]
1.96 μs ± 267 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%time l = [] for x in range(100000): l.append(x)
Wall time: 120 ms
tup1= 1, 2, 3 print(tup1) #嵌套元组 tup2 = (1, 2, 3), (4, 5) print(tup2)
(1, 2, 3)
((1, 2, 3), (4, 5))
l = [1, 2, 3] print(tuple(l)) str = "Hello World" print(tuple(str))
(1, 2, 3)
(‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘W‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘)
tup3 = tuple(str) print(tup3[4])
o
tup1 + tup3
(1, 2, 3, ‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘W‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘)
a, b, c = tup1 print(b)
2
# 函数返回多个值 def return_multiple(): t = (1, 2, 3) return t a, _, c = return_multiple() #可以不需要接受参数,_仅仅是作为占位符 print(c)
3
# 元组列表迭代 tuple_list = [(1, 2), (3, 4), (5, 6)] for x, y in tuple_list: print(x+y)
3
7
11
tup3.count('o')
2
lst_1 = [1, 2, 3, 'a', 'b', (4, 5)] print(lst_1) lst_2 = list(range(1, 9)) print(lst_2)
[1, 2, 3, ‘a‘, ‘b‘, (4, 5)]
[1, 2, 3, 4, 5, 6, 7, 8]
lst_3 = list(tup3) print(lst_3)
[‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘W‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘]
lst_4 = list(range(10)) #末尾添加 lst_4.append(11) print(lst_4) # 指定位置插入 lst_4.insert(5, 12) print(lst_4)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
[0, 1, 2, 3, 4, 12, 5, 6, 7, 8, 9, 11]
#删除指定位置的元素并返回 item = lst_4.pop(6) print(item) print(lst_4)
5
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 11]
#删除指定的值,注意12在这里是“值”,不是“位置” lst_4.remove(12) print(lst_4)
[0, 1, 2, 3, 4, 6, 7, 8, 9, 11]
lst_3 = lst_1 + lst_2 print(lst_3) lst_1.extend(lst_2) print(lst_1)
[1, 2, 3, ‘a‘, ‘b‘, (4, 5), 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, ‘a‘, ‘b‘, (4, 5), 1, 2, 3, 4, 5, 6, 7, 8]
import random lst_5 = list(range(10)) random.shuffle(lst_5) print(lst_5)
[8, 4, 3, 7, 6, 9, 1, 5, 0, 2]
lst_5.sort() print(lst_5) lst_5.sort(reverse=True) print(lst_5)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course'] lst_6.sort() print(lst_6)
[‘Analysis‘, ‘Course‘, ‘Data‘, ‘Python‘, ‘Welcome‘, ‘to‘]
lst_6.sort(key = len, reverse=True) #key = len:指定排序方式按照元素的长度;reverse=True:逆序,元素长度从长到短排序。 print(lst_6)
[‘Analysis‘, ‘Welcome‘, ‘Course‘, ‘Python‘, ‘Data‘, ‘to‘]
print(lst_5) print(lst_5[1:5]) print(lst_5[5:]) print(lst_5[:5]) print(lst_5[-5:]) print(lst_5[-5:-2]) print(lst_5[::2]) print(lst_5[::-1])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]
[5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[5, 6, 7]
[0, 2, 4, 6, 8]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
. enumerate,for循环时记录索引,逐个返回元组(i, item),i是元素的索引号,item是元素
. sorted 返回新的有序列表,区别:list中的sort()是就地排序
. zip "压缩"将多个序列的对应位置的元素组成元组
. zip(*元组列表) "解压缩",zip的逆操作
. reversed 逆序迭代,可配合list返回逆序列表
lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course'] #(0, 'Welcome') for i, item in enumerate(lst_6): print("%i-%s" %(i, item))
0-Welcome
1-to
2-Python
3-Data
4-Analysis
5-Course
str_dict = dict((i, item) for i, item in enumerate(lst_6)) print(str_dict)
{0: ‘Welcome‘, 1: ‘to‘, 2: ‘Python‘, 3: ‘Data‘, 4: ‘Analysis‘, 5: ‘Course‘}
import random lst_5 = list(range(10)) random.shuffle(lst_5) print(lst_5) #sorted lst_5_sorted = sorted(lst_5) print(lst_5) print(lst_5_sorted)
[5, 4, 2, 8, 3, 9, 7, 1, 6, 0]
[5, 4, 2, 8, 3, 9, 7, 1, 6, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course'] lst_7 = list(range(5)) #[0, 1, 2, 3, 4] lst_8 = ['a', 'b', 'c'] zip_lst = zip(lst_6, lst_8, lst_7) print(list(zip_lst))
[(‘Welcome‘, ‘a‘, 0), (‘to‘, ‘b‘, 1), (‘Python‘, ‘c‘, 2)]
lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course'] lst_7 = list(range(5)) #[0, 1, 2, 3, 4] lst_8 = ['a', 'b', 'c'] zip_lst = zip(lst_6, lst_8, lst_7) print(list(zip(*zip_lst)))
[(‘Welcome‘, ‘to‘, ‘Python‘), (‘a‘, ‘b‘, ‘c‘), (0, 1, 2)]
list(reversed(lst_6))
[‘Course‘, ‘Analysis‘, ‘Data‘, ‘Python‘, ‘to‘, ‘Welcome‘]
原文:https://www.cnblogs.com/pencil2001/p/13226287.html