日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python学习笔记(一)基本数据类型

發布時間:2024/1/23 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python学习笔记(一)基本数据类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 用內建函數 id()可以查看每個對象的內存地址,即身份。

>>> id(3) 140574872 >>> id(3.222222) 140612356 >>> id(3.0) 140612356 2. 使用type()能夠查看對象的類型。<type ‘int’>,說明 3 是整數類型(Interger);<type ‘float’>則告訴我們那個對象是浮點型(Floating point real number)。與 id()的結果類似,type()得到的結果也是只讀的。

>>> type(3) <type 'int'> >>> type(3.0) <type 'float'> >>> type(3.222222) <type 'float'> 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()apply()
delattr()help()next()setattr()buffer()
dict()hex()object()slice()coerce()
dir()id()oct()sorted()intern()

這些內建函數,怎么才能知道哪個函數怎么用,是干什么用的呢?

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

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

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学习笔记(一)基本数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。