Python編程入門(一)

Python編程入門(一)

=========================================================================================

概述:


=========================================================================================


編程語言

1.腳本編程語言

腳本編程語言

  • 如php,perl,python,java等爲腳本編程語言,通常需要通過解釋器解釋運行。

python(java)程序的執行過程

  • source code(源碼 .py)--->conplier(編譯)--->bytecode(字節碼 .pyc)--->解釋器pvm或者jvm(運行在各自的虛擬機,也是運行時的真正所在位置)--->processor(CPU)

2.Python的實現(pvm:編譯器和解釋器)

CPython

  • 原始,標準的實現方式

Jython

  • 用於於java語言集成的實現

IronPython

  • 用於於.NET框架集成實現

Python安裝及數據類型

1.python:一切皆對象

python2 <--> python3

  • 過程式編程:指令+數據。以指令爲中心,數據服務於指令需要。

  • 對象式編程:以數據爲中心(對象),指令服務於數據。

類--->class有兩部分組成

  • 屬性:

  • 方法:

對象:向屬性賦值;python 當中一切皆對象!所以,一旦創建了一個對象,那麼它就跟某類操作綁定起來了)

     比如,對於數據類型“數值”來講,它的屬性就是:附一個數值,如,b=345。一旦對象類型確定了,那麼它所支持的方法也就確定了,所以,任何一個對象只要創建出來,它必須屬於某一個類型,也就必須跟這個類型支持的方法綁定在了一起(即:它所支持的方法也就確定了)

注意:

  如果需要大量調用系統命令(如,系統維護腳本)來完成某些操作,用bash shell腳本足以實現;只有寫一個完整的不依賴系統命令(如,複雜的程序)的情況下才有必要用到Python。

python是動態類型的編程語言

變量

數據類型

核心數據類型

  • 數值:

  • 字符串:

  • 列表:

  • 字典:

  • 元組:

  • 文件:

  • 其他類型:集合,類類型,None,布爾型

動態類型

  • 支持動態綁定

強類型

  • 嚴格區分數據類型

  • 可以顯示的將一種數據類型轉換爲另一種數據類型,如:str(),repr(),format()等

數字類型

  • 整數

  • 浮點數

  • 複數

字符類型

  • 字符串字面量:用於引用一個字符序列,由特定次序的字符組成的字符序列。

  • 支持3中引號:‘’,"","""(表示多行引用)

演示:

1.python3的安裝及位置查看

#安裝python3
[root@CentOS6 ~]# yum install python34 python34-devel python34-libs python34-tools

#查看安裝的位置
[root@CentOS6 ~]# rpm -ql python34
/usr/bin/pydoc3
/usr/bin/pydoc3.4
/usr/bin/python3
/usr/bin/python3.4
/usr/bin/python3.4m
/usr/bin/pyvenv
/usr/bin/pyvenv-3.4
/usr/share/doc/python34-3.4.5
/usr/share/doc/python34-3.4.5/LICENSE
/usr/share/doc/python34-3.4.5/README
/usr/share/man/man1/python3.1.gz
/usr/share/man/man1/python3.4.1.gz

[root@CentOS6 ~]# cd /usr/bin/
[root@CentOS6 bin]# ll python*
-rwxr-xr-x  2 root root 9032 Jul 24  2015 python
lrwxrwxrwx. 1 root root    6 Nov  6  2016 python2 -> python
-rwxr-xr-x  2 root root 9032 Jul 24  2015 python2.6
lrwxrwxrwx  1 root root    9 Jan 16 20:02 python3 -> python3.4
-rwxr-xr-x  2 root root 6088 Dec 12 00:59 python3.4
lrwxrwxrwx  1 root root   17 Jan 16 20:02 python3.4-config -> python3.4m-config
-rwxr-xr-x  2 root root 6088 Dec 12 00:59 python3.4m
-rwxr-xr-x  1 root root  173 Dec 12 00:58 python3.4m-config
-rwxr-xr-x  1 root root 3285 Dec 12 00:57 python3.4m-x86_64-config
lrwxrwxrwx  1 root root   16 Jan 16 20:02 python3-config -> python3.4-config

[root@CentOS6 ~]# python3
Python 3.4.5 (default, Dec 11 2017, 16:57:19) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello,world")     #在python3中,print爲函數
Hello,world
>>> exit()

2.字符串

[root@CentOS6 ~]# python3
Python 3.4.5 (default, Dec 11 2017, 16:57:19) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> str.  #字符竄的常用操作
str.__add__(            str.__getattribute__(   str.__name__            str.__text_signature__  str.isdigit(            str.rfind(
str.__base__(           str.__getitem__(        str.__ne__(             str.__weakrefoffset__   str.isidentifier(       str.rindex(
str.__bases__           str.__getnewargs__(     str.__new__(            str.capitalize(         str.islower(            str.rjust(
str.__basicsize__       str.__gt__(             str.__prepare__(        str.casefold(           str.isnumeric(          str.rpartition(
str.__call__(           str.__hash__(           str.__qualname__        str.center(             str.isprintable(        str.rsplit(
str.__class__(          str.__init__(           str.__reduce__(         str.count(              str.isspace(            str.rstrip(
str.__contains__(       str.__instancecheck__(  str.__reduce_ex__(      str.encode(             str.istitle(            str.split(
str.__delattr__(        str.__itemsize__        str.__repr__(           str.endswith(           str.isupper(            str.splitlines(
str.__dict__            str.__iter__(           str.__rmod__(           str.expandtabs(         str.join(               str.startswith(
str.__dictoffset__      str.__le__(             str.__rmul__(           str.find(               str.ljust(              str.strip(
str.__dir__(            str.__len__(            str.__setattr__(        str.format(             str.lower(              str.swapcase(
str.__doc__             str.__lt__(             str.__sizeof__(         str.format_map(         str.lstrip(             str.title(
str.__eq__(             str.__mod__(            str.__str__(            str.index(              str.maketrans(          str.translate(
str.__flags__           str.__module__          str.__subclasscheck__(  str.isalnum(            str.mro(                str.upper(
str.__format__(         str.__mro__             str.__subclasses__(     str.isalpha(            str.partition(          str.zfill(
str.__ge__(             str.__mul__(            str.__subclasshook__(   str.isdecimal(          str.replace(   

>>> mystr="Hello World"
>>> mystr1="""abc         #支持"""或者''' 3引號的多行引用
... efg"""
>>> print(mystr)
Hello World
>>> print(mystr1)
abc
efg

>>> s='Hello'
>>> s*5      #字符串可進行乘法運算
'HelloHelloHelloHelloHello'
>>> w=' world'
>>> s+w      #字符串相加
'Hello world'
>>> len(s)   #取字符串的長度
5
>>> len(w)
6
>>> 'he' in s  #判斷字符串的成員關係
False
>>> 'He' in s
True

>>> s.lower()  #轉換爲小寫
'hello'
>>> s.upper()   #轉換爲大寫
'HELLO'
>>> help(str.replace)  #查看幫助

>>> print(s)
Hello
>>> s.replace("H","h")
'hello'

Python過程型程序設計介紹

1.數據結構

數據結構

  • 通過某種方式(例如對元素進行編號)組織在一起的數據元素的集合,這些數據元素可以是數字或者字符,甚至可以是其他數據結構;

  • Python的最基本數據結構是序列(有序的元素集合)

  • 序列中的每個元素被分配一個序號——即元素的位置,也稱爲索引(索引從0開始編號);

  • Python 包含6種內建的數據序列:列表,元祖,字符串,Unicode字符串,buffer對象和xrange對象。

2.Python的關鍵要素

Python的關鍵要素

  • 基本數據類型;

  • 對象引用;

  • 組合數據類型;

  • 邏輯操作符;

  • 控制流語句;

  • 算數操作符;

  • 輸入/輸出;

  • 函數的創建與調用。

要素1:基本數據類型

  • 任何程序語言都必須能夠表示基本數據項

Python的基本數據類型有:

  • Integral 類型

     整型:不可變類型(如:-257,201624583337114373395836)

     布爾型:True,False

  • 浮點類型

     浮點數:3.141592

     複數:3+6j

     十進制數:

  • 字符串

     如:'GNU is Not Unix',"hello","world"

要素2:對象引用(變量)

  • Python將所有數據存爲內存對象

  • Python中,變量事實上是指內存對象的引用;

  • 動態類型:在任何時刻,只要需要,某個對象引用都可以重新引用一個不同的對象(可以是不同的數據類型);

  • 內建函數type()用於返回給定數數據項的數據類型;

  • “=”用於將變量名於內存中的某對象綁定:如果對象事先存在,就直接進行綁定,否則,則由“=”創建引用的對象

2018-05-22_221524.png

變量命名規則

  • 只能包含字母,數字和下劃線,且不能以數字開頭;

  • 區分字母大小寫;

  • 禁止使用保留字(Python2於Python3的保留字有所不同)

命名慣例:

  • 以單一下劃線開頭變量名(_x)不會被 from module import * 語句導入;

  • 前後有下劃線的變量名(_x_)是系統定義的變量名,對Python解釋器有特殊意義;

  • 以兩個下劃線開頭但結尾沒有下劃線的變量名(__x)是類的本地變量;

  • 交互式模式下,變量名"_"用於保存最後表達式的結果

注意:變量名沒有類型,對象纔有

要素3:組合數據類型



編寫,執行Python代碼

1.交互式解釋器

直接啓動python,其顯示信息取決於程序版本及操作系統等

2018-05-21_163456.png

2.python程序文件

交互式模式下的程序執行完成後難以再次運行;

將編寫的程序文件保存至文件(.py)中方便多次運行

  • python的此類包含了一系列預編寫好的語句的程序文件稱作“模塊”;

  • 能夠直接運行的模塊文件通常稱作腳本(即程序的頂層文件)

python源程序文件通常以 .py 爲擴展名

2018-05-21_164410.png

  • 第一行爲shebang,即執行腳本時,通知內容要啓動的解釋器;

  • 第二行通過 import 導入一個python模塊 platform

  • 第三行打印platform模塊的 platform 方法的執行結果

給予此腳本執行權限,並執行即可

2018-05-21_165222.png

Python程序可以分解成模塊,語句,表達式和對象

程序由模塊組成;

模塊包含語句;

語句包含表達式

表達式建立並處理對象

  • 表達式是“某事”,而語句是“做某事(即指令)”;例如:3+4是某事,而 print(3+4) 是做某事;

  • 語句的特性:它改變了事物,例如,賦值語句改變了變量,print 語句改變了屏幕輸出等

演示:

1.platform模塊

[root@centos7 ~]# python3.4
Python 3.4.8 (default, Mar 23 2018, 10:04:27) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.
platform.DEV_NULL                          platform.__spec__                          platform.architecture(
platform._UNIXCONFDIR                      platform.__str__(                          platform.collections
platform._WIN32_CLIENT_RELEASES            platform.__subclasshook__(                 platform.dist(
platform._WIN32_SERVER_RELEASES            platform.__version__                       platform.java_ver(
platform.__cached__                        platform._default_architecture             platform.libc_ver(
platform.__class__(                        platform._dist_try_harder(                 platform.linux_distribution(
platform.__copyright__                     platform._follow_symlinks(                 platform.mac_ver(
platform.__delattr__(                      platform._get_real_winver(                 platform.machine(
platform.__dict__                          platform._ironpython26_sys_version_parser  platform.node(
platform.__dir__(                          platform._ironpython_sys_version_parser    platform.os
platform.__doc__                           platform._java_getprop(                    platform.platform(
platform.__eq__(                           platform._libc_search                      platform.popen(
platform.__file__                          platform._lsb_release_version              platform.processor(
platform.__format__(                       platform._mac_ver_xml(                     platform.python_branch(
platform.__ge__(                           platform._node(                            platform.python_build(
platform.__getattribute__(                 platform._norm_version(                    platform.python_compiler(
platform.__gt__(                           platform._parse_release_file(              platform.python_implementation(
platform.__hash__(                         platform._platform(                        platform.python_revision(
platform.__init__(                         platform._platform_cache                   platform.python_version(
platform.__le__(                           platform._pypy_sys_version_parser          platform.python_version_tuple(
platform.__loader__                        platform._release_filename                 platform.re
platform.__lt__(                           platform._release_version                  platform.release(
platform.__name__                          platform._supported_dists                  platform.subprocess
platform.__ne__(                           platform._sys_version(                     platform.sys
platform.__new__(                          platform._sys_version_cache                platform.system(
platform.__package__                       platform._sys_version_parser               platform.system_alias(
platform.__reduce__(                       platform._syscmd_file(                     platform.uname(
platform.__reduce_ex__(                    platform._syscmd_uname(                    platform.uname_result(
platform.__repr__(                         platform._syscmd_ver(                      platform.version(
platform.__setattr__(                      platform._uname_cache                      platform.win32_ver(
platform.__sizeof__(                       platform._ver_output                       

>>> platform.platform()
'Linux-3.10.0-327.el7.x86_64-x86_64-with-centos-7.2.1511-Core'
>>> platform.uname()
uname_result(system='Linux', node='centos7', release='3.10.0-327.el7.x86_64', version='#1 SMP Thu Nov 19 22:10:57 UTC 2015', machine='x86_64', processor='x86_64')
>>> platform.system()
'Linux'
>>> platform.dist()
('centos', '7.2.1511', 'Core')
>>> platform.python_version()
'3.4.8'

3.Python IDE

IDLE

  • 標準 Python 環境提供

Eclipse和PyDev

PythonWin

Komodo

Wingware

PyCharm
























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章