python-常見用法

來源:互聯網
上載者:User

標籤:%s   定位   fill   sdi   ...   new   字典   多行   轉義   

一、注釋

單行注釋:#後全部注釋

多行注釋:‘‘‘所有內容‘‘‘  或者使用 """所有內容"""  ,多行注釋用三對單引號或雙引號包裹

二、數字Number

int---整數         1 2 3 0 -1 -1898668

float---浮點數    0.2344232    -8.999

bool---布爾   True   False

complex---複數   1 + 2j、 1.1 + 2.2j

三、快捷方法

tab    選中行整體後移

shift+tab   選中行整體前移

ctrl+/   選中多行一次注釋,每行開始加#   ;再次執行,取消注釋

ctrl+z  代碼回撤到上次輸入

空行  分隔兩段不同功能或含義的代碼,便於日後代碼的維護或重構。空行也是程式碼的一部分

同一行顯示多句代碼,相互間使用;隔開

import 與 from...import

在 python 用 import 或者 from...import 來匯入相應的模組。

將整個模組(somemodule)匯入,格式為: import somemodule

從某個模組中匯入某個函數,格式為: from somemodule import somefunction

從某個模組中匯入多個函數,格式為: from somemodule import firstfunc, secondfunc, thirdfunc

將某個模組中的全部函數匯入,格式為: from somemodule import *

四、字串string

a=‘123456789‘

訪問字串某值 a[2] 是 3

更新字串  a[:4]+‘ok‘    字元a的開始到第四位+字串ok    1234ok

逸出字元 \ :  \在行尾為續行;\\ 反斜線;\n 換行;\000  空 ;\r  斷行符號

+ :a+b 兩個字串拼接

* :a*2   兩個a字元拼接

%:格式化字串,%s  格式化字串  %d 格式化整數   %f格式化浮點數    %.2f  兩位小數

name = ‘The Apple‘
print(name.capitalize()) #把字串首字母大寫
print(name.center(100,‘-‘)) #把字串置中的
print(name.index(‘p‘))#找不到下標的時候會報錯
print(name.isalnum()) #只能有英文、數字
print(name.isalpha()) #判斷是否只為英文的,漢字也可以
print(name.count(‘歡‘)) #查詢次數
print(name.endswith(‘.jpg‘))#判斷字串是否以xx結尾
print(name.startswith(‘138‘))#判斷字串是否以xx開頭
# print(name.upper()) #都給你變成大寫的
# print(name.lower()) #都變成小寫
# print(name.find(‘p‘)) #它找不到元素的時候,不會報錯,會返回-1
# print(name.isdigit()) #判斷是否為純數字
# print(name.isspace()) #判斷是否全都是空格
# print(name.strip())#去掉字串兩邊的東西,預設是去調兩邊的空格和分行符號的
# print(name.lstrip())#只去掉左邊的
# print(name.rstrip())#只去掉右邊的
# print(name.replace(‘abc‘,‘周旭東‘)) #替換字串,把前面的替換成後面的
# name = ‘5‘
# print(name.zfill(2))#在前面補0
names = ‘‘‘lxy,zyf,wdz,nl,wy,gfw‘‘‘
# print(names.split(‘,‘)) #1、分割字串,2、把字串變成一個list 3、預設是以空格和分行符號分割的
stus = [‘lxy‘, ‘zyf‘, ‘wdz‘, ‘nl‘, ‘wy‘, ‘gfw‘]
‘‘‘lxy、zyf、wdz‘‘‘
print(‘;‘.join(stus))#1、是吧list變成字串的 2、以某個字串串連

import string
print(string.ascii_letters) #所有的大寫+小寫字母
print(string.ascii_lowercase) #所有的小寫字母
print(string.ascii_uppercase)# 所有的大寫字母
print(string.digits) #所有的數字
print(string.punctuation) #所有的特殊字元

# print(name.format())
# print(name.format_map())
# print(name[1]) #字串也是可以根據下標來取值的

五、list列表

int  str  float  三個類型  type(a) 判斷啊是什麼類型    a[1]  尋找a的下標是1 的值

stus.append(‘周伊凡‘) #在列表末尾增加一個元素
stus.insert(0,‘聶磊‘) #在指定位置添加一個元素

# stus.pop(2) #刪除指定位置的元素
# stus.remove(‘聶磊‘) #刪除指定的元素
# del stus[3] #刪除指定位置的元素

stus[1]=‘老王‘  #修改

字串的方法
strip()
lstrip()
rstrip()
split()
‘,‘.join([1,2,3,4,5])
upper()
lower()
replace(old,new)    
find(‘xxxx‘) # -1
index(‘xxxx‘) # 報錯  

reverse() #反轉

clear()清空整個list
count(‘xx‘)  查看某值在字串出現次數
endswith(‘.xls‘) endswith(‘.jpg‘)
startswith(‘test‘)
isdigits(‘sdfs‘)#是否是純數字
‘999‘.zfill(3)
切片:
list、字串,範圍取值的一種方法
a = [1,2,4]
a[1:2]

六、元組

# 增加
shenyang[‘女朋友‘] = ‘很漂亮‘
shenyang.setdefault(‘house‘,‘三環裡面有10套‘)
shenyang[‘age‘] = 38
shenyang.setdefault(‘age‘,49) #setdefault這種方式,如果key已經存在,不管它,如果key不存在的話,就新增。

#字典是無序
#修改
shenyang[‘女朋友‘] = ‘很漂亮‘
#刪除
# shenyang.pop(‘女朋友‘) #指定key刪除 刪除的時候key不存在是會報錯的
# del shenyang[‘age‘] #用del來刪,刪除的時候key不存在是會報錯的
# 取值
# print( shenyang[‘addr‘])
# print(shenyang.get(‘email2‘,‘[email protected]‘))
# shenyang.clear() #清空字典
# shenyang.popitem() #隨機刪除一個key

七、檔案讀寫

檔案讀寫
f = open(‘a.txt‘,‘w‘,ending=‘utf-8‘)
# w+
# r+ #指標在最前面
# a+ #指標在最後
f.seek(0)  指標移到初始位置
f.read() #讀取檔案裡面所有的內容,字串
f.readline() #讀取一行
f.readlines() #讀取檔案裡面所有的內容,返回的是一個list,每一行的內容放到一個list

f.write() #寫一個字串
f.writelines(abc) #寫一個list
f.close()

f.truncate() #清空檔案內容

f.flush()

例:

#1、高效的處理方式
# words words
# 1、先開啟原來的檔案,再開啟一個空檔案
#2、迴圈處理原來檔案裡面每一行資料,處理完之後,寫到新檔案裡面
#3、把原來的檔案刪除,把新檔案的名字改成原來檔案的名字

#1、前面的空格
#2、把空行去掉
#3、你替換you
#4、寫到新檔案裡
#5、把原來的檔案刪除,把新檔案的名字改成原來檔案的名字
import os
with open(‘words‘,encoding=‘utf-8‘) as fr, open(‘.words‘,‘w‘,encoding=‘utf-8‘) as fw:
for line in fr:
line = line.lstrip() #去掉左邊的空格
if line:#判斷這一行是否有資料
line = line.replace(‘你‘,‘you‘)#替換資料
fw.write(line)#寫到新檔案裡面

os.remove(‘words‘)#刪除原來的檔案
os.rename(‘.words‘,‘words‘) #把新檔案的名字改成原來檔案的名字

python-常見用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.