核心Python知识(一)

使用解释的动态类型语言创建美丽的代码

Python是一种解释的动态类型语言。Python使用缩进来创建可读,甚至漂亮的代码。使用Python的大量内置库,它可以处理许多作业而无需其他库,从而允许您几乎立即编写有用的代码。但是,Python广泛的外部库网络可以根据您的需求轻松添加功能。

 

介绍

Python是一种解释动态类型的语言。Python使用缩进来创建可读,甚至漂亮的代码。Python附带了如此多的库,您可以处理许多作业而无需其他库。Python适合你,并尽量不让你惊讶,这意味着你几乎可以立即编写有用的代码。

Python由Guido van Rossum于1990年创建。虽然Python被用作语言和社区的图腾,但该名称实际上源自Monty Python,并且对Monty Python skits的引用在代码示例和库名称中很常见。还有其他几种流行的Python实现,包括PyPy(JIT编译器),Jython(JVM集成)和IronPython(.NET CLR集成)。

Python 2.x与Python 3.x

Python现在有两种基本版本 - Python 2.x(目前为2.7)和Python 3.x(目前为3.7)。这是一个重要的区别 - 一种方式的编码不能兼容运行。但是,大多数代码都是可以互换的。以下是一些主要差异:

 

有一个名为2to3.py的实用程序可用于将Python 2.x代码转换为3.x,而2.x中的“-3”命令行开关可以为自动转换器无法处理的情况启用其他弃用警告。python-modernize和'six'支持包等第三方工具可以轻松地为支持2.x和3.x的库和应用程序定位两个变体的大型公共子集。

 

语言特色

Guido对编程的意图...

Python中的缩进规则。没有花括号,没有开始和结束关键字,在行的末尾不需要分号 - 将代码组织成块,函数或类的唯一方法是缩进。如果某些东西是缩进的,它会形成一个块,所有内容都缩进到同一级别,直到文件末尾或缩进较少的行。

虽然有几种缩进选项,但通用标准是每级4个空格:

 

def function_block():
  # first block stuff
  # second block within first block:
      # second block stuff
      for x in an_iterator:
          # this is the block for the for loop
          print x
      # back out to this level ends the for loop
  # and this ends the second block...
# more first block stuff
def another_function_block():

  

评论和Docstrings

要将评论从当前位置标记到行尾,请使用井号“#”。

# this is a comment on a line by itself
x = 3    # this is a partial line comment after some code

  

对于更长的注释和更完整的文档,尤其是在模块或函数或类的开头,请使用三引号字符串。您可以使用3个单引号或3个双引号。三引号字符串可以覆盖多行,并忽略Python程序中任何未分配的字符串。这些字符串通常用于模块,函数,类和方法的文档。按照惯例,“docstring”是其封闭范围中的第一个语句。遵循此约定允许使用pydoc模块自动生成文档。

通常,从试图理解代码本身的开发人员的角度来看,您使用一行注释来评论代码。Docstrings更适合用于记录代码的作用,更多的是从将要使用代码的人的角度来看

Python是您可以深入研究的一种语言,所以让我们深入研究这个示例Python脚本:

#! /usr/bin/env python
""" An example Python script
   Note that triple quotes allow multiline strings
"""
# single line comments are indicated with a "#"
import sys         # loads the sys (system) library
def main_function(parameter):
   """ This is the docstring for the function """
   print " here is where we do stuff with the parameter"
               print parameter
   return a_result     # this could also be multiples
if __name__ == "__main__":
   """ this will only be true if the script is called 
       as the main program """
   # command line parameters are numbered from 0 
   # sys.argv[0] is the script name
   param = sys.argv[1]    # first param after script name
   # the line below calls the main_function and 
   # puts the result into function_result
   function_result = main_function(param)

  

分支,循环和异常

分支

Python有一组非常简单的if / else语句:

if something_is_true:
  do this
elif something_else_is_true:
  do that
else:
  do the other thing

  

作为if和elif语句一部分的表达式可以是比较(==,<,>,<=,> =等),也可以是任何python对象。通常,零序列和空序列都是False,其他所有序列都是True。Python没有switch语句。

循环

Python有两个循环。for循环遍历序列,例如列表,文件或其他一些系列:

for item in ['spam', 'spam', 'spam', 'spam']:
  print item

  

作为if和elif语句一部分的表达式可以是比较(==,<,>,<=,> =等),也可以是任何python对象。通常,零序列和空序列都是False,其他所有序列都是True。Python没有switch语句。

循环

Python有两个循环。for循环遍历序列,例如列表,文件或其他一些系列:

for item in ['spam', 'spam', 'spam', 'spam']:
  print item

  
上面的代码将打印“垃圾邮件”四次。在条件为真时执行while循环:

counter = 5
while counter > 0:
   counter -= 1

  

每次迭代时,计数器变量减1。此代码执行直到表达式为False,在这种情况下是“counter”达到零时。

迭代连续数字的更好方法是使用“range”函数返回从0到n-1的迭代器:

for i in range(5):
  print i  # Prints numbers from 0 to 4

  “范围”功能支持其他允许指定起始值和步长值的模式:

range(4, 10)    # Returns values: 4, 5, 6, 7, 8, 9
range(4, 10, 2) # Returns values: 4, 6, 8

  

猜你喜欢

转载自www.cnblogs.com/daniumiqi/p/12186188.html
今日推荐