天天看點

Python細緻技巧總結(不斷更新)

1. 倒序輸出一個數值或字元串

print(str(n)[::-1])
           

2. 靜态語言的局限,動态語言牛的地方:

動态語言能用type()動态建立類,靜态語言不行

3. 字元串轉數字

一、python中字元串轉換成數字

(方法1)

類中進行導入:import string
str='555'
num=string.atoi(str)
num即為str轉換成的數字
轉換為浮點數:string.atof(str)

(方法2)直接int
int(str)即可。

二、數字轉換成字元串
num=322
str='%d'%num
str即為num轉換成的字元串 
           

4.pycharm使用:聲明使用utf-8

SyntaxError: Non-ASCII character '\xef' in file 1.py on line 12, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
#encoding:utf-8          -----------------------------------缺少此行将會導緻以上的錯誤
           

5.python 擷取list特定元素下标

方法一: 利用數組自身的特性 a.index(target), 其中a是你的目标list,target是你需要的下标對應的值

a=[72, 56, 76, 84, 80, 88]
print(a.index(76))

output:
2
           

6.pycharm 提醒 Typo in word

在使用IDEA時,代碼中單詞底部有波浪線,提示typo in word時

原因:單詞拼寫檢查功能,說明目前拼寫有問題,

解決方式:按照駝峰命名法,重新命名即可;可以通過pycharm設定規避提醒

https://blog.csdn.net/HelloZEX/article/details/80996738