python脚本式编程_Python编程入门(一)
Python編程入門(一)
=========================================================================================
概述:
=========================================================================================
編程語言
1.腳本編程語言★腳本編程語言如php,perl,python,java等為腳本編程語言,通常需要通過解釋器解釋運(yùn)行。
★python(java)程序的執(zhí)行過程source code(源碼 .py)--->conplier(編譯)--->bytecode(字節(jié)碼 .pyc)--->解釋器pvm或者jvm(運(yùn)行在各自的虛擬機(jī),也是運(yùn)行時(shí)的真正所在位置)--->processor(CPU)
2.Python的實(shí)現(xiàn)(pvm:編譯器和解釋器)★CPython原始,標(biāo)準(zhǔn)的實(shí)現(xiàn)方式
★Jython用于于java語言集成的實(shí)現(xiàn)
★IronPython用于于.NET框架集成實(shí)現(xiàn)
Python安裝及數(shù)據(jù)類型
1.python:一切皆對(duì)象★python2 <--> python3過程式編程:指令+數(shù)據(jù)。以指令為中心,數(shù)據(jù)服務(wù)于指令需要。
對(duì)象式編程:以數(shù)據(jù)為中心(對(duì)象),指令服務(wù)于數(shù)據(jù)。
☉類--->class有兩部分組成屬性:
方法:
對(duì)象:向?qū)傩再x值;(python 當(dāng)中一切皆對(duì)象!所以,一旦創(chuàng)建了一個(gè)對(duì)象,那么它就跟某類操作綁定起來了)
比如,對(duì)于數(shù)據(jù)類型“數(shù)值”來講,它的屬性就是:附一個(gè)數(shù)值,如,b=345。一旦對(duì)象類型確定了,那么它所支持的方法也就確定了,所以,任何一個(gè)對(duì)象只要?jiǎng)?chuàng)建出來,它必須屬于某一個(gè)類型,也就必須跟這個(gè)類型支持的方法綁定在了一起(即:它所支持的方法也就確定了)
注意:
如果需要大量調(diào)用系統(tǒng)命令(如,系統(tǒng)維護(hù)腳本)來完成某些操作,用bash shell腳本足以實(shí)現(xiàn);只有寫一個(gè)完整的不依賴系統(tǒng)命令(如,復(fù)雜的程序)的情況下才有必要用到Python。
★python是動(dòng)態(tài)類型的編程語言
☉變量
☉數(shù)據(jù)類型
◆核心數(shù)據(jù)類型數(shù)值:
字符串:
列表:
字典:
元組:
文件:
其他類型:集合,類類型,None,布爾型
◆動(dòng)態(tài)類型支持動(dòng)態(tài)綁定
◆強(qiáng)類型嚴(yán)格區(qū)分?jǐn)?shù)據(jù)類型
可以顯示的將一種數(shù)據(jù)類型轉(zhuǎn)換為另一種數(shù)據(jù)類型,如:str(),repr(),format()等
★數(shù)字類型整數(shù)
浮點(diǎn)數(shù)
復(fù)數(shù)
★字符類型字符串字面量:用于引用一個(gè)字符序列,由特定次序的字符組成的字符序列。
支持3中引號(hào):‘’,"","""(表示多行引用)
演示:
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為函數(shù)
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引號(hào)的多行引用
... efg"""
>>> print(mystr)
Hello World
>>> print(mystr1)
abc
efg
>>> s='Hello'
>>> s*5 #字符串可進(jìn)行乘法運(yùn)算
'HelloHelloHelloHelloHello'
>>> w=' world'
>>> s+w #字符串相加
'Hello world'
>>> len(s) #取字符串的長(zhǎng)度
5
>>> len(w)
6
>>> 'he' in s #判斷字符串的成員關(guān)系
False
>>> 'He' in s
True
>>> s.lower() #轉(zhuǎn)換為小寫
'hello'
>>> s.upper() #轉(zhuǎn)換為大寫
'HELLO'
>>> help(str.replace) #查看幫助
>>> print(s)
Hello
>>> s.replace("H","h")
'hello'
Python過程型程序設(shè)計(jì)介紹
1.數(shù)據(jù)結(jié)構(gòu)★數(shù)據(jù)結(jié)構(gòu)通過某種方式(例如對(duì)元素進(jìn)行編號(hào))組織在一起的數(shù)據(jù)元素的集合,這些數(shù)據(jù)元素可以是數(shù)字或者字符,甚至可以是其他數(shù)據(jù)結(jié)構(gòu);
Python的最基本數(shù)據(jù)結(jié)構(gòu)是序列(有序的元素集合);
序列中的每個(gè)元素被分配一個(gè)序號(hào)——即元素的位置,也稱為索引(索引從0開始編號(hào));
Python 包含6種內(nèi)建的數(shù)據(jù)序列:列表,元祖,字符串,Unicode字符串,buffer對(duì)象和xrange對(duì)象。
2.Python的關(guān)鍵要素★Python的關(guān)鍵要素基本數(shù)據(jù)類型;
對(duì)象引用;
組合數(shù)據(jù)類型;
邏輯操作符;
控制流語句;
算數(shù)操作符;
輸入/輸出;
函數(shù)的創(chuàng)建與調(diào)用。
☉要素1:基本數(shù)據(jù)類型任何程序語言都必須能夠表示基本數(shù)據(jù)項(xiàng)
◆Python的基本數(shù)據(jù)類型有:Integral 類型
整型:不可變類型(如:-257,201624583337114373395836)
布爾型:True,False
浮點(diǎn)類型
浮點(diǎn)數(shù):3.141592
復(fù)數(shù):3+6j
十進(jìn)制數(shù):
字符串
如:'GNU is Not Unix',"hello","world"
☉要素2:對(duì)象引用(變量)Python將所有數(shù)據(jù)存為內(nèi)存對(duì)象
Python中,變量事實(shí)上是指內(nèi)存對(duì)象的引用;
動(dòng)態(tài)類型:在任何時(shí)刻,只要需要,某個(gè)對(duì)象引用都可以重新引用一個(gè)不同的對(duì)象(可以是不同的數(shù)據(jù)類型);
內(nèi)建函數(shù)type()用于返回給定數(shù)數(shù)據(jù)項(xiàng)的數(shù)據(jù)類型;
“=”用于將變量名于內(nèi)存中的某對(duì)象綁定:如果對(duì)象事先存在,就直接進(jìn)行綁定,否則,則由“=”創(chuàng)建引用的對(duì)象
◆變量命名規(guī)則只能包含字母,數(shù)字和下劃線,且不能以數(shù)字開頭;
區(qū)分字母大小寫;
禁止使用保留字(Python2于Python3的保留字有所不同)
◆命名慣例:以單一下劃線開頭變量名(_x)不會(huì)被 from module import * 語句導(dǎo)入;
前后有下劃線的變量名(_x_)是系統(tǒng)定義的變量名,對(duì)Python解釋器有特殊意義;
以兩個(gè)下劃線開頭但結(jié)尾沒有下劃線的變量名(__x)是類的本地變量;
交互式模式下,變量名"_"用于保存最后表達(dá)式的結(jié)果
注意:變量名沒有類型,對(duì)象才有
☉要素3:組合數(shù)據(jù)類型
編寫,執(zhí)行Python代碼
1.交互式解釋器★直接啟動(dòng)python,其顯示信息取決于程序版本及操作系統(tǒng)等
2.python程序文件★交互式模式下的程序執(zhí)行完成后難以再次運(yùn)行;
★將編寫的程序文件保存至文件(.py)中方便多次運(yùn)行python的此類包含了一系列預(yù)編寫好的語句的程序文件稱作“模塊”;
能夠直接運(yùn)行的模塊文件通常稱作腳本(即程序的頂層文件)
☉python源程序文件通常以 .py 為擴(kuò)展名
第一行為shebang,即執(zhí)行腳本時(shí),通知內(nèi)容要啟動(dòng)的解釋器;
第二行通過 import 導(dǎo)入一個(gè)python模塊 platform
第三行打印platform模塊的platform 方法的執(zhí)行結(jié)果
☉給予此腳本執(zhí)行權(quán)限,并執(zhí)行即可
★Python程序可以分解成模塊,語句,表達(dá)式和對(duì)象
☉程序由模塊組成;
☉模塊包含語句;
☉語句包含表達(dá)式
☉表達(dá)式建立并處理對(duì)象表達(dá)式是“某事”,而語句是“做某事(即指令)”;例如: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標(biāo)準(zhǔn) Python 環(huán)境提供
☉Eclipse和PyDev
☉PythonWin
☉Komodo
☉Wingware
☉PyCharm
總結(jié)
以上是生活随笔為你收集整理的python脚本式编程_Python编程入门(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python创建sqlite3数据库_树
- 下一篇: 灯效控制器和rgb控制器_更具个性的RG