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

原文地址:https://www.cnblogs.com/dindin1995/p/13059174.html