1. 程式人生 > >Python編程入門(一)

Python編程入門(一)

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

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

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

註意:

如果需要大量調用系統命令(如,系統維護腳本)來完成某些操作,用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編程入門(一)