初試Python

kangyz發表於2020-10-17

01 Python簡介

Python是一種跨平臺的計算機程式設計語言。於1989年開發的語言,創始人範羅蘇姆(Guido van Rossum),別稱:龜叔(Guido)。

python具有非常多並且強大的第三方庫,使得程式開發起來得心應手。

Python“信條”:人生苦短,我用python!(Life is short,you need Python。)

02 Python 版本

  • python 2.x 版本,官方在 2020 年停止支援,原碼不規範,重複較多

  • python 3.x 版本,功能更加強大且修復了很多bug,原碼清晰,簡單

Let's not play games with semantics. The way I see the situation for 2.7 is that EOL is January 1st, 2020, and there will be no updates, not even source-only security patches, after that date. Support (from the core devs, the PSF, and python.org) stops completely on that date. If you want support for 2.7 beyond that day you will have to pay a commercial vendor. Of course it's open source so people are also welcome to fork it. But the core devs have toiled long enough, and the 2020 EOL date (an extension from the originally annouced 2015 EOL!) was announced with sufficient lead time and fanfare that I don't feel bad about stopping to support it at all.【原文連結

03 Python擅長的領域

  • Web開發:Django、pyramid、Tornado、Bottle、Flask、WebPy

  • 網路程式設計:Twisted、Requests、Scrapy、Paramiko

  • 科學運算:SciPy、Pandas、lpython

  • GUI圖形開發:wxPython、PyQT、Kivy

  • 運維自動化:OpenStack、SaltStack、Ansible、騰訊藍鯨

04 Python直譯器(部分):

  • (1)Cpython(官方推薦):把python轉化成c語言能識別的二進位制碼

  • (2)Jpython:把python轉化成java語言能識別的二進位制碼

  • (3)其他語言直譯器:把python轉化成其他語言能識別的二進位制碼

  • (4)PyPy:將所有程式碼一次性編譯成二進位制碼,加快執行效率(模仿編譯型語言的一款python直譯器)

05 2020年10月TIOBE指數

TIOBE指數

06 Python環境的安裝

Windows

下載安裝包:https://www.python.org/downloads/

配置環境變數:【右鍵計算機】-->【屬性】-->【高階系統設定】-->【高階】-->【環境變數】-->【在系統變數中找到 Path,雙擊或編輯】-->【新增Python安裝目錄,如;C:\python38,切記前面有英文標點分號(win10系統變數與win7大體一致)】

新增環境變數

Linux、Mac

無需安裝,系統自帶Python環境,但是可選擇升級版本。

07 編寫第一個Python程式

print("Hello World!")

08 編譯型與解釋型語言區別(補充)

  • 編譯型:一次性把所有程式碼編譯成機器能識別的二進位制碼再執行
    • 代表語言:c,c++
    • 優點:執行速度快
    • 缺點:開發速度慢,除錯周期長
  • 解釋型:程式碼從上到下一行一行解釋並執行:
    • 代表語言:python,php
    • 優點:開發效率快,除錯周期短
    • 缺點:執行速度相對慢

09 與其它程式語言的對比

C++

#include <iostream>
using namespace std;
int main()
{
   cout << "Hello World";
   return 0;
}

C

#include <stdio.h>
int main()
{
   /* 我的第一個 C 程式 */
   printf("Hello, World! \n"); 
   return 0;
}

C#

using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* 我的第一個 C# 程式*/
         Console.WriteLine("Hello World!");
         Console.ReadKey();
      }
   }
}

Java

public class HelloWorld {
    public static void main(String []args) {
       System.out.println("Hello World!");
    }
}

PHP

<?php
echo 'Hello World!';
?>

Ruby

puts "Hello World!";

Go

package main
import "fmt"
func main() {
   fmt.Println("Hello, World!")
}

Python

print("Hello, World!");

10 Python之禪(擴充套件)

import this

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!

相關文章