日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python全部语法_python基本语法

發(fā)布時(shí)間:2025/3/15 python 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python全部语法_python基本语法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python:跨平臺(tái)、面向?qū)ο蟮膭?dòng)態(tài)編程語言

發(fā)布時(shí)間:1991年

發(fā)明:guido

van rossum

python的語言特點(diǎn)

1、開源免費(fèi)

2、腳本語言,解析執(zhí)行

3、跨平臺(tái)

4、簡潔美觀

5、一切皆對(duì)象

6、可擴(kuò)展的膠水語言

解析執(zhí)行原理:

1、編輯文本文件

2、意識(shí)編譯字節(jié)碼,形成pyc源碼

3、在pvm虛擬機(jī)執(zhí)行

4、在cpu執(zhí)行,打印到屏幕

解析器cpython、jpython、ironpython

------------------------------------------------------------------------------------------------------------------

1、python環(huán)境準(zhǔn)備

執(zhí)行命令:python

--version 打印出版本號(hào)

2、python安裝

1、預(yù)安裝:linux、unix、macos一般都是自帶安裝python

2、源碼安裝

3、安裝包安裝

rpm、msi(windows)

a)、源碼編譯安裝

tar

xjvf python-3.3.2.tar.bz2

cd

python-3.3.2

./configure

make

make

install

ln

-s /usr/loacl/bin/python3 /usr/bin/python

3、基本語法

1、>>>

print('hello')

>>>

是主提示符

2、

... print 'hello'

...是輔助提示符

3、>>>

exit()

退出

root@localhost#

python

>>>print

('hello')

hello

>>>if

1 + 1 == 2: 冒號(hào)不是分號(hào)

... print

'hello'

...

hello

>>>exit()就退出來了。

4、python的自省

>>>

help(str)遇到問題可以help

>>>type('hello')

>>>isinstance('hello',str)

>>>dir(str)

5、模塊導(dǎo)入

1、import

m

print m.plus(1,2)

2、from

m import plus

print

plus(1,2)

示例:m.py ,new.py

def plus(a,b):import m

return a + bprint m.plus(1,2)

def plus2():

exit()打印結(jié)果 3

6、導(dǎo)入系統(tǒng)模塊

1、import

sys.path

from os import *

7、導(dǎo)入私有模塊

1、import sys

2、 path

= '/root/dir/' 把模塊的目錄添加到path中,方便查找加載

3、 sys.path.append(path)或者直接sys.path.append("root/dir/")

4、 from

m import plus2

-------------------------------------------------------------------------------------------------------------------

8、變量的使用

內(nèi)建數(shù)據(jù)類型:

1、boolean--> exam_is_pass = true

2、integer--> person_number = 100

3、float--> weight = 65.18

4、string--> greet_msg = "welcome to visit

itercast"

5、list--> score_list = [12, 25, 56, 78]順序存儲(chǔ)相當(dāng)于數(shù)組,字符串和數(shù)字混合

6、tuple -->

index_tuple(0, 1, 2, 3, 4)不能改變的數(shù)組

7、dict -->age_dict

= { 'bob': 18, 'lucy': 16} 字典

8、set--> index_set = set([0, 1, 2, 3])集合,和數(shù)學(xué)里面的集合一樣沒有順序

9、none

空字符串‘ ’ 、空元組()、空列表[]、空字典{} ,在邏輯判斷的時(shí)候都是false

>>>

'i love "itercast"'

'i love "itercast"'

>>>

"i love \"itercast\""

'i

love "itercast"'

>>>

str1 = '''i love

...

"itercast"

...

'''

>>>

str1

'i

love \n"itercast"\n'

字符串和list之間的轉(zhuǎn)換:

>>>

list1 = str1.split()

>>>

list1

>>>

['i', 'love', '"itercast"']

往list里面添加字符串

list1.append('!')

or ("!")

往list里面添加數(shù)字

list1.append(8)

------------------------------------------------------------------------------------------------------------

9、 字典使用:

字典名 =

{’關(guān)鍵字‘:值,...}

字典名.get('關(guān)鍵字',默認(rèn)值)

賦值:自頂名['關(guān)鍵字']

= 值

字典名.keys()將關(guān)鍵字返回出一個(gè)列表

字典名.values()將字典的值返回出一個(gè)列表

字典名.copy()age_dict2 = age_dict.copy()

>>>

del age_dict['bob']

>>>

age_dict2.popitem()彈出第一項(xiàng)

>>>

bob in age_dict2

在腳本里面寫:

dirt = {'bob':10, 'lucy':20, 'jim':30}定義字典

print dirt['bob']

dirt['abc']=40添加元素

print

dirt

deldirt['bob']

字典內(nèi)置函數(shù)&方法

Python字典包含了以下內(nèi)置函數(shù):

1、cmp(dict1,

dict2):比較兩個(gè)字典元素。

2、len(dict):計(jì)算字典元素個(gè)數(shù),即鍵的總數(shù)。

3、str(dict):輸出字典可打印的字符串表示。

4、type(variable):返回輸入的變量類型,如果變量是字典就返回字典類型。

Python字典包含了以下內(nèi)置方法:

1、radiansdict.clear():刪除字典內(nèi)所有元素

2、radiansdict.copy():返回一個(gè)字典的淺復(fù)制

3、radiansdict.fromkeys():創(chuàng)建一個(gè)新字典,以序列seq中元素做字典的鍵,val為字典所有鍵對(duì)應(yīng)的初始值

4、radiansdict.get(key,

default=None):返回指定鍵的值,如果值不在字典中返回default值

5、radiansdict.has_key(key):如果鍵在字典dict里返回true,否則返回false

6、radiansdict.items():以列表返回可遍歷的(鍵,

值) 元組數(shù)組

7、radiansdict.keys():以列表返回一個(gè)字典所有的鍵

8、radiansdict.setdefault(key,

default=None):和get()類似, 但如果鍵不已經(jīng)存在于字典中,將會(huì)添加鍵并將值設(shè)為default

9、radiansdict.update(dict2):把字典dict2的鍵/值對(duì)更新到dict里

10、radiansdict.values():以列表返回字典中的所有值

false

實(shí)例:

>>>dirt

= { 'bob':10, 'lucy':20, 10:10}

>>>

dirt['jim'] = 30

返回關(guān)鍵字的列表

>>>

dirt.keys()

['jim',

'bob', 10, 'lucy']

返回值得列表

>>>

dirt.values()

[30,

10, 10, 20]

list復(fù)制:

>>>

dir1 = dirt.copy()

>>>

dir1.values()

[10,

30, 10, 20]

>>>

dir1.keys()

['bob',

'jim', 10, 'lucy']

list刪除關(guān)鍵字

del

dirt['bob']

list彈出第一個(gè)關(guān)鍵字

dirt.popitem()

Python表達(dá)式:

=賦值

==等于

>,<大于號(hào)和小于號(hào)

>=,<=大于等于和小于等于

And邏輯與

Or邏輯或

Not邏輯非

if

1==1 and 2==2:

print 'hello'

else:

print 'error'

python的邏輯分支:

If條件判斷語句:if條件判斷語句:

. . . .。。。。

else:elif條件判斷語句:

. . . .。。。。

. . . .else:

.。。。。。。。

Python沒有switch語句

ifsys.argv[1] == '-s':

time.sleep(10);

elif sys.argv[1] == '-q':

quit();

else:

print('error')

例子:

sex =

'boy'

age =

19

if

sex == 'boy':

print 'hi,boy'

elif

sex == 'girl':

if age < 18:

print 'hi,girl'

else:

print 'hi,lady'

else:

print 'weather is good'

print

'end'

python循環(huán):

for循環(huán):

例一:

IndentationError: unexpected indent

>>>

for i in ['bob', 'lucy', 'jim']:

...print i,

...

bob

lucy jim

>>>

for i in ['bob', 'lucy', 'jim']:

...print i

...

bob

lucy

jim

例二:

for name,age in (('afe',18), ('fleajf',16)):

>>>

for name,age in (('afe',18), ('fleajf',16)):

...print name,age

...

afe 18

fleajf

16

While循環(huán):

>>>

i = 0

>>>

while i < 10:

...print i,

...i += 1

迭代器:

>>>

obj = range(5)

>>> itera = iter(obj)相當(dāng)于obj._iter_()

>>>

try:

...while True:

...print itera.next(),

...

except StopIteration:

...pass

...

0 1 2

3 4

python函數(shù)定義:

1、不帶參數(shù)函數(shù)的定義

函數(shù)定義

函數(shù)調(diào)用

def函數(shù)名()

函數(shù)體。。。

函數(shù)名()

實(shí)例:def

welcome():

print( ' I love it')

2、帶參數(shù)函數(shù)的定義

函數(shù)定義

函數(shù)調(diào)用

def函數(shù)名(參數(shù)列表):

函數(shù)體。。。

函數(shù)名(參數(shù)列表)

例子:

>>> def

welcome(who,action):

...print(who + ' ' + action)

...

>>>

welcome('wang','nihao')

wang nihao

3、Python變成參數(shù):參數(shù)會(huì)自動(dòng)轉(zhuǎn)變成一參數(shù)命名的元組或字典

函數(shù)定義1

函數(shù)定義2

def函數(shù)名(*args):轉(zhuǎn)換成元組

函數(shù)體。。。

函數(shù)名(v1,v2,v3)

def函數(shù)名(**args):轉(zhuǎn)換成字典

函數(shù)名。。。

函數(shù)名(k1=v1,k2=v2)

例1:

例2

例1:

>>> def

welcome(*args):

...print args

...

>>>

welcome('jim','is','thinking')

('jim', 'is',

'thinking')

>>> def

welcome(**args):

...print args

...

>>>

welcome(who='jim',state='is',action='thinking')

{'action': 'thinking',

'state': 'is', 'who': 'jim'}

4、參數(shù)默認(rèn)值

>>>

def welcome(who,state='is',action='sleeping'):

...print who,state,action

...

>>>

welcome('jim','was')

jim

was sleeping

第一個(gè)參數(shù)必須有,第二個(gè)和第三個(gè)可以沒有,沒有就用默認(rèn)值

5、函數(shù)的返回值

def函數(shù)名(函數(shù)列表)

def函數(shù)名(參數(shù)列表)

def函數(shù)名(參數(shù)列表)

函數(shù)體。。。

函數(shù)體。。。

函數(shù)體。。。

return

obj

return

返回obj對(duì)象

返回none

返回none

面向?qū)ο蠡A(chǔ)--類

Python種一切都是對(duì)象,雷和對(duì)象是面向?qū)ο蟮幕A(chǔ)

定義類

類的實(shí)例化

class類名稱():默認(rèn)繼承object

·····

class類名稱(father):

·····

實(shí)例名=類名稱()

例子:

定義一個(gè)類

實(shí)例化monk類

查看實(shí)例化類的類型

class monk:

def__init__(self,name,age):

self.name = name

self.age = age

yc_monk

= mokn('yancan',20)

yc_monk.name #成員變量

yc_monk._class_

type(yc_monk)

類的方法:

類的方法

實(shí)例化方法的調(diào)用

class類名稱

。。。

def方法名(self,參數(shù)列表)

。。。

實(shí)例名.方法名(參數(shù)列表)

例子

class monk:

def

__init__(self, name, age):

self.name

= name

self.age

= age

def

net(self):

print

"i love webo"

def

play(self, str):

print

"play with",str

m1 =

monk("yancan", 19)

print m1.name

m1.net()

m1.play("monkey")

python文件IO

print方法:輸出數(shù)據(jù)到標(biāo)準(zhǔn)輸出

raw_ipnut方法:從標(biāo)準(zhǔn)輸入讀數(shù)據(jù)

從標(biāo)準(zhǔn)輸入讀取內(nèi)容

將用戶輸入的信息輸出到標(biāo)準(zhǔn)輸出

>>>

input = raw_input('prompt@ ')

prompt@nihaowoshi

>>>

print(input)

nihaowoshi

file類-讀文件

創(chuàng)建一個(gè)file類實(shí)例f,以只讀模式打開文本文件

>>>f = file('1.txt', 'r')

通過f實(shí)例讀取文件內(nèi)容

>>>

f.read()

關(guān)閉文件實(shí)例,釋放資源

>>>

f.close()

使用open函數(shù)打開文件,返回一個(gè)file類實(shí)例

>>>

f= open('1.txt', 'r')

讀取全部,返回字符串

>>>

f.read()

一次只讀取一行

>>>

f.readline()

讀取全部返回列表

>>>

f.readlines()

已追加模式打開文件,返回一個(gè)file類實(shí)例

>>>

f = file('1.txt','a')

向文件寫入字符串

>>>

f.write('I love you\nEnd')

刷新緩沖區(qū)

>>>

f.flush()

>>>

f.close()

"r"------->只讀

"w"------->只寫

"a"------->追加

"b"------->二進(jìn)制

"r+","a+"------->更新

總結(jié)

以上是生活随笔為你收集整理的python全部语法_python基本语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。