1. 程式人生 > >Python學習筆記(一)Python基礎

Python學習筆記(一)Python基礎

1.1  Python 簡介

  • Python是一門跨平臺、開源、免費的解釋型高階動態程式設計語音
  • Python支援偽編譯將原始碼轉換為位元組碼來優化程式提高執行速度和對原始碼進行編譯
  • Python支援使用py2exe、pyinstaller、cx_Freeze或其他類似工具將Python程式及其所有依賴庫打包為副檔名為.exe的可執行程式,從而可以脫離Python直譯器環境和相關依賴庫而在Windows平臺上獨立執行
  • Python支援指令式程式設計、函數語言程式設計,完全支援面向物件程式設計

1.2  Python開發環境之IDLE的簡單使用

1.2.1  IDLE的常用快捷鍵

 1.2.2 自定義IDLE清屏快捷鍵

1)下載ClearWindow.py檔案;

這裡附有該檔案原始碼,下載後以 .py 格式儲存即可

"""
Clear Window Extension

It provides "Clear Shell Window" under "Options"
with ability to undo.

Add these lines to config-extensions.def

[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-l>


"""

class ClearWindow:

    menudefs = [
        ('options', [None,
               ('Clear Shell Window', '<<clear-window>>'),
       ]),]
		 
    def __init__(self, editwin):
        self.editwin = editwin
        self.text = self.editwin.text
        self.text.bind("<<clear-window>>", self.clear_window2)

        self.text.bind("<<undo>>", self.undo_event)  # add="+" doesn't work

    def undo_event(self, event):
        text = self.text
        
        text.mark_set("iomark2", "iomark")
        text.mark_set("insert2", "insert")
        self.editwin.undo.undo_event(event)

        # fix iomark and insert
        text.mark_set("iomark", "iomark2")
        text.mark_set("insert", "insert2")
        text.mark_unset("iomark2")
        text.mark_unset("insert2")
        

    def clear_window2(self, event): # Alternative method
        # work around the ModifiedUndoDelegator
        text = self.text
        text.undo_block_start()
        text.mark_set("iomark2", "iomark")
        text.mark_set("iomark", 1.0)
        text.delete(1.0, "iomark2 linestart")
        text.mark_set("iomark", "iomark2")
        text.mark_unset("iomark2")
        text.undo_block_stop()
        if self.text.compare('insert', '<', 'iomark'):
            self.text.mark_set('insert', 'end-1c')
        self.editwin.set_line_and_column()

    def clear_window(self, event):
        # remove undo delegator
        undo = self.editwin.undo
        self.editwin.per.removefilter(undo)

        # clear the window, but preserve current command
        self.text.delete(1.0, "iomark linestart")
        if self.text.compare('insert', '<', 'iomark'):
            self.text.mark_set('insert', 'end-1c')
        self.editwin.set_line_and_column()
 
        # restore undo delegator
        self.editwin.per.insertfilter(undo)

2)將這個檔案放在Python X\Lib\idlelib目錄下(X為你的python版本);

3)在這個目錄下找到config-extensions.def這個檔案(idle擴充套件的配置檔案),以記事本的方式開啟它;

4)開啟config-extensions.def 後在句末加上這樣幾句:

[ClearWindow] enable=1 enable_editor=0 enable_shell=1 [ClearWindow_cfgBindings] clear-window=<Control-Key-l>

 5)儲存config-extensions.def檔案後重啟IDLE,會發現選單Options下面多了一個可以清屏的選單項,如圖所示。另外,也可以隨時使用“Ctrl + l”快捷鍵實現清屏。

6)這個快捷鍵是可以任意修改的,修改的地方就在上面配置程式碼的最後一行“ clear-window=<Control-Key-l>”,將其中的 “I” 改為其他字元就行了。

1.2.3  使用pip管理第三方包

1.3  變數、運算子與表示式

 1.3.1  變數與內建函式型別

Python內建物件

 1.3.2  Python變數

在Python中,不需要事先宣告變數及其型別,直接賦值即可建立各種型別的物件變數,並且變數的型別是可以隨時發生改變的。

1.3.3  數字

1.整數

2.浮點數

1.5、0.3、-1.2、1.2e2、314.15e-2

3.複數

 1.3.4  字串

 1.3.5  運算子和表示式

Python運算子與功能 ​​​​

 1.3.6  常用內建函式