python学习笔记(一)基本数据类型
1. 用內(nèi)建函數(shù) id()可以查看每個對象的內(nèi)存地址,即身份。
這個模塊都能做哪些事情呢?可以用下面的方法看到:
>>> dir(math) ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']Python 是一個非常周到的姑娘,她早就提供了一個命令,讓我們來查看每個函數(shù)的使用方法。
>>> help(math.pow)在交互模式下輸入上面的指令,然后回車,看到下面的信息:
Help on built-in function pow in module math:pow(...)pow(x, y)Return x**y (x to the power of y). >>> math.sqrt(9) 3.0 >>> math.floor(3.14) 3.0 >>> math.floor(3.92) 3.0 >>> math.fabs(-2) # 等價于 abs(-2) 2.0 >>> abs(-2) 2 >>> math.fmod(5,3) # 等價于 5%3 2.0 >>> 5%3 24. 字符串
不過在寫這個功能前,要了解兩個函數(shù):raw_input 和 print
這兩個都是 Python 的內(nèi)建函數(shù)(built-in function)。關(guān)于 Python 的內(nèi)建函數(shù),下面這個表格都列出來了。所謂內(nèi)建函數(shù),就是能夠在 Python 中直接調(diào)用,不需要做其它的操作。
Built-in Functions
| all() | enumerate() | int() | ord() | str() |
| any() | eval() | isinstance() | pow() | sum() |
| basestring() | execfile() | issubclass() | print() | super() |
| bin() | file() | iter() | property() | tuple() |
| bool() | filter() | len() | range() | type() |
| bytearray() | float() | list() | raw_input() | unichr() |
| callable() | format() | locals() | reduce() | unicode() |
| chr() | frozenset() | long() | reload() | vars() |
| classmethod() | getattr() | map() | repr() | xrange() |
| cmp() | globals() | max() | reversed() | zip() |
| compile() | hasattr() | memoryview() | round() | import() |
| complex() | hash() | min() | set() | apply() |
| delattr() | help() | next() | setattr() | buffer() |
| dict() | hex() | object() | slice() | coerce() |
| dir() | id() | oct() | sorted() | intern() |
這些內(nèi)建函數(shù),怎么才能知道哪個函數(shù)怎么用,是干什么用的呢?
不知道你是否還記得我在前面使用過的方法,這里再進行演示,這種方法是學(xué)習(xí) Python 的法寶。
>>> help(raw_input) 5.Python 中如何避免中文是亂碼Python 中如何避免中文是亂碼
這個問題是一個具有很強操作性的問題。我這里有一個經(jīng)驗總結(jié),分享一下,供參考:
首先,提倡使用 utf-8 編碼方案,因為它跨平臺不錯。
經(jīng)驗一:在開頭聲明:
# -*- coding: utf-8 -*-有朋友問我-*-有什么作用,那個就是為了好看,愛美之心人皆有,更何況程序員?當然,也可以寫成:
# coding:utf-8經(jīng)驗二:遇到字符(節(jié))串,立刻轉(zhuǎn)化為 unicode,不要用 str(),直接使用 unicode()
unicode_str = unicode('中文', encoding='utf-8') print unicode_str.encode('utf-8')經(jīng)驗三:如果對文件操作,打開文件的時候,最好用 codecs.open,替代 open(這個后面會講到,先放在這里)
import codecs codecs.open('filename', encoding='utf8')總結(jié)
以上是生活随笔為你收集整理的python学习笔记(一)基本数据类型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Machine Learning on
- 下一篇: python学习笔记(二)列表(List