python學習筆記(一)基本資料型別

2021-08-03 23:45:28 字數 3673 閱讀 4130

1. 用內建函式 id()可以檢視每個物件的記憶體位址,即身份。

>>> id(3)

140574872

>>> id(3.222222)

140612356

>>> id(3.0)

140612356

2. 使用type()能夠檢視物件的型別。,說明 3 是整數型別(interger);則告訴我們那個物件是浮點型(floating point real number)。與 id()的結果類似,type()得到的結果也是唯讀的。

>>> type(3)

>>> type(3.0)

>>> type(3.222222)

3.常用數學函式和運算優先順序

>>> import math
>>> math.pi

3.141592653589793

這個模組都能做哪些事情呢?可以用下面的方法看到:

>>> 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 是乙個非常周到的姑娘,她早就提供了乙個命令,讓我們來檢視每個函式的使用方法。

>>> 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

2

4. 字串

不過在寫這個功能前,要了解兩個函式:raw_input 和 print

這兩個都是 python 的內建函式(built-in function)。關於 python 的內建函式,下面這個**都列出來了。所謂內建函式,就是能夠在 python 中直接呼叫,不需要做其它的操作。

built-in functions

abs()

divmod()

input()

open()

staticmethod()

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()

delattr()

help()

next()

setattr()

buffer()

dict()

hex()

object()

slice()

coerce()

dir()

id()

oct()

sorted()

intern()

這些內建函式,怎麼才能知道哪個函式怎麼用,是幹什麼用的呢?

不知道你是否還記得我在前面使用過的方法,這裡再進行演示,這種方法是學習 python 的法寶。

>>> help(raw_input)
5.python 中如何避免中文是亂碼

這個問題是乙個具有很強操作性的問題。我這裡有乙個經驗總結,分享一下,供參考:

首先,提倡使用 utf-8 編碼方案,因為它跨平台不錯。

經驗一:在開頭宣告:

# -*- coding: utf-8 -*-
有朋友問我-*-有什麼作用,那個就是為了好看,愛美之心人皆有,更何況程式設計師?當然,也可以寫成:

# coding:utf-8
經驗二:遇到字元(節)串,立刻轉化為 unicode,不要用 str(),直接使用 unicode()

unicode_str = unicode('中文', encoding='utf-8')

print unicode_str.encode('utf-8')

經驗三:如果對檔案操作,開啟檔案的時候,最好用 codecs.open,替代 open(這個後面會講到,先放在這裡)

import codecs

codecs.open('filename', encoding='utf8')

Python基本資料一

變數資料型別0 1 python基本資料型別包括了 數字型別,布林型,字串,列表,元組,字典,集合。一 檢視資料型別的方法 type 檢視資料型別 temp hello t type temp print t help,type 檢視資料型別下的全部方式 help type temp dir 檢視類...

Python學習筆記 基本資料型別

基本資料型別 1.整型和浮點型 對於整型int來講,不存在溢位。但是浮點型float會溢位,會損失精度,即精度無效。為什麼會有精度失效 計算機都是用二進位制表示的,必然存在0與1之間的數字沒法表示的情況 二進位制小數轉十進位制小數 101.111 2 轉 1 22 0 21 1 20 1 2 1 1...

python基本數 python基本資料型別

1.數字 int 數字又分整型和浮點型,在python中宣告變數是不用宣告所以自己就會識別 a 10 整型 a1 1.24 浮點型 支援科學計數法,將10用e來代替 2.字串 str 在python中用引號引起來的就是字串,而且單引號和雙引號並沒有什麼區別 a string a1 string a2...