python入门

很久之前就听过 python,说机器学习都使用这门语言。这段时间对它感兴趣,开始学习,python 语法简洁清晰,逐渐被它吸引。

学习的时候,总是不由自主地想,卧槽,还可以这样!

mac 上自带 python 环境,可以直接使用。python 带有命令行工具,打开命令行,输入 python,就可以愉快地玩耍了。

➜  /Users/xiaocan >python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

python 自带强大的帮助文档,使用起来也非常方便。使用 dir() 查看对象包含哪些方法,使用 help() 查看怎么使用这个方法。比如说 python 有一种数据类型是 list,咱来看看它有哪些方法。

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

先不管那些带有下划线的方法,其它方法有 append, count, extend, index, insert, pop, remove, reverse, sort。是不是和你用过的其它语言的方法名有点类似。再来看看 append 怎么用。

>>> help(list.append) 

之后会进入帮助文档的界面,按 q 可以退出,显示的内容是:

Help on method_descriptor:

append(...)
    L.append(object) -- append object to end

现在知道 append 怎么用了吧,咱来试一试:

>>> a = ["1","2","3"]
>>> a.append("4")
>>> a
['1', '2', '3', '4']

是不是 so easy!python 还有更完善的帮助文档。比如说,我想知道 python 有哪些关键字,怎么办?除了百度,还有更好的办法。
直接在用 help() 进入帮助命令,然后用 keywords 就可以列出所有的关键字。

>>> help()

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.

and                 elif                if                  print
as                  else                import              raise
assert              except              in                  return
break               exec                is                  try
class               finally             lambda              while
continue            for                 not                 with
def                 from                or                  yield
del                 global              pass

我之前用的是 java,接触 python 之后,才发现查文档是这么方便,这么有趣。可以说,有了这些帮助文档,可以让学习 python 更轻松,更有趣。

再举个例子,感受一下 python 的简洁。比如说,a=5, b=6。我们要把 a,b 里面的内容替换。用其它语言可能需要增加一个中间变量,然后才能完成 a,b 的替换。使用 python 可以直接这样:

>>> a = 5
>>> b = 6
>>> a,b = b,a
>>> a
6
>>> b
5

a,b = b,a,就是这么简洁。随着学习的深入,python 的简洁可以随处可见。再来一个例子:

>>> l = [1,2,3,4,5]
>>> ll = [i+5 for i in l]
>>> ll
[6, 7, 8, 9, 10]

即便你没学过 python 也能看出这段代码的意思吧。就是这么简洁,就是这么直白。

最后,来看看著名的 python 之蝉。在 python 命令行下输入 import this,可以看到:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

不管懂不懂,反正 python 就是崇尚简洁,python 就是很强大。如果你没有学过其它语言, python 完全可以做为新手学习的第一门语言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值