本文使用的是jupyter notebook 这一工具
个人建议直接使用anaconda 会自动配置好环境,对于初学者来说也是非常大方便。网址如下,根据自己计算机在网页的最下端下载相应的版本。
anaconda下载链接
下载后jupyter notebook打开即可
通过下面的代码你就可以查看python的版本
import sys
sys.version
python 是一种面向对象的解释形语言,通过jupyter notebook我们可以简单的得到一些结果如下:
print('hello world!')
hello world!
python作为现在最流行的计算机语言,是因为其中的模块、包已经对象的类型十分多,所以我们不仅需要掌握python的基础语言,还需要掌握python中每个包的用法,下面几种方法就是我们常用的几种方法去查看一个包的帮助信息:
help(list)
主要用来查看函数、类、模块等的详细说明
Help on class list in module builtins:
class list(object)
| list(iterable=(), /)
|
| Built-in mutable sequence.
|
| If no argument is given, the constructor creates a new empty list.
| The argument must be an iterable if specified.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(self, /)
| Return a reverse iterator over the list.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(self, /)
| Return the size of the list in memory, in bytes.
|
| append(self, object, /)
| Append object to the end of the list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| extend(self, iterable, /)
| Extend list by appending elements from the iterable.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| insert(self, index, object, /)
| Insert object before index.
|
| pop(self, index=-1, /)
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range.
|
| remove(self, value, /)
| Remove first occurrence of value.
|
| Raises ValueError if the value is not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
|
| sort(self, /, *, key=None, reverse=False)
| Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
dir(list)
主要用来查看对象的属性和方法
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']
当我们声明了一个对象如:
a=[1,2,3]#a是一个list
b=a
print(id(a),id(b),a is b)
140511890198144 140511890198144 True
可以看到结果是两个相同的id 这说明,a与b的存储位置相同,那么大家判断一下如果改变a,b是否会变:
a=[1,2,3]#a是一个list
b=a
a.append(5)#list的一个添加数字在末尾的方法
print(b)
[1, 2, 3, 5]
可以看出他们是同一个对象的引用,如果香让b单独为一个变量怎么办?一般我们都常用copy 包实现:
from copy import copy
'''python 引用包的格式,一般为from ... import... 或者为 import ... as '''
a = [1,2,3,4,['boom','strange']]
b = copy(a)
a is b
False
此时如果我们改变第一维的list的a的时候b并不会变:
a.append(5)
print('a is :',a)
print('b is :',b)
这里是引用
a is : [1, 2, 3, 4, [‘boom’, ‘strange’], 5]
b is : [1, 2, 3, 4, [‘boom’, ‘strange’]]
但是如果我们改变第二维的list的时候,就会改变b:
a[4].append('shaklaka')
print(b)
[1, 2, 3, 4, [‘boom’, ‘strange’, ‘shaklaka’]]
所以这种方法我们通常称为浅拷贝,如果相让他完全不变,我们通常使用下面的方法,深拷贝:
from copy import deepcopy
a = [1,2,3,4,['boom','strange']]
b = deepcopy(a)
a[4].append('shaklaka')
print(a)
print(b)
[1, 2, 3, 4, [‘boom’, ‘strange’, ‘shaklaka’]]
[1, 2, 3, 4, [‘boom’, ‘strange’]]