02-python基礎資料型別 (Elementary Data Type)

來源:互聯網
上載者:User

標籤:spec   mod   列印   insert   school   返回   body   子串   記憶體   

python的變數不需要聲明, 但變數使用前必須複製, 因為python中所有的內容全部是對象

變數是沒有類型的, 有類型的是指向記憶體對象的類型

a = ‘123‘a = 123  是合法的

此外, python還可以進行多變數的連續賦值

a  = b = c = 1a, b, c = 1, ‘qwe‘, ‘c‘

1, Number類型

1、Python可以同時為多個變數賦值,如a, b = 1, 2。2、一個變數可以通過賦值指向不同類型的對象。3、數值的除法(/)總是返回一個浮點數,要擷取整數使用//操作符。4、在混合計算時,Python會把整型轉換成為浮點數。

python的math函數

函數          傳回值 ( 描述 )abs(x)      返回數位絕對值,如abs(-10) 返回 10ceil(x)     返回數位上入整數,如math.ceil(4.1) 返回 5cmp(x, y)    如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1exp(x)      返回e的x次冪(ex),如math.exp(1) 返回2.718281828459045fabs(x)     返回數位絕對值,如math.fabs(-10) 返回10.0floor(x)    返回數位下舍整數,如math.floor(4.9)返回 4log(x)      如math.log(math.e)返回1.0,math.log(100,10)返回2.0log10(x)     返回以10為基數的x的對數,如math.log10(100)返回 2.0max(x1, x2,...)    返回給定參數的最大值,參數可以為序列。min(x1, x2,...)    返回給定參數的最小值,參數可以為序列。modf(x)      返回x的整數部分與小數部分,兩部分的數值符號與x相同,整數部分以浮點型表示。pow(x, y)    x**y 運算後的值。round(x [,n])    返回浮點數x的四捨五入值,如給出n值,則代表舍入到小數點後的位元。sqrt(x)      返回數字x的平方根,數字可以為負數,傳回型別為實數,如math.sqrt(4)返回 2+0j

 

2, str類型

msg = "What‘s Is Your Name?"#find, 尋找子串位置, 可指定從哪開始尋找index = msg.find(‘Name‘, 1, 34)print (index)# rfind, 反向尋找index = msg.rfind(‘Name‘, 1, 20)print(index)# 與list之間的轉換msg_list = msg.split(‘ ‘)join_msg = ‘ ‘.join(msg_list)print (msg_list)print(join_msg)# 使用[:]進行截取print(msg[3: 8])

另外:  字串的運算有:

+ 字串串連 >>>a + b ‘HelloPython‘
* 重複輸出字串 >>>a * 2 ‘HelloHello‘
[] 通過索引擷取字串中字元 >>>a[1] ‘e‘
[ : ] 截取字串中的一部分 >>>a[1:4] ‘ell‘
in 成員運算子 - 如果字串中包含給定的字元返回 True >>>"H" in a True
not in 成員運算子 - 如果字串中不包含給定的字元返回 True >>>"M" not in a True
r/R 原始字串 - 原始字串:所有的字串都是直接按照字面的意思來使用,沒有轉義特殊或不能列印的字元。 原始字串除在字串的第一個引號前加上字母"r"(可以大小寫)以外,與一般字元串有著幾乎完全相同的文法。 >>>print r‘\n‘ \n >>> print R‘\n‘ \n

格式化輸出; 

類c: %s, %d, %c, %f
format: ‘{0}, {1}‘.format(str0, str1)

 

3, list類型   [value, value2, value3]

# list 的常用操作# 列表有序msg_list = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]msg_list.append(5)msg_list.reverse()msg_list.remove(5)msg_list.index(3)msg_list.insert(2, ‘a‘)         # 插入list2 = [‘a‘, ‘b‘]msg_list.extend(list2)          # 列表相加# 切片new_list = msg_list[2:4]                   # 包左不包右msg_list[-5: ]          # 從倒數第五位開始切print (msg_list)print (new_list)print (msg_list)# 求2 出現的位置first_position = 0for i in range(msg_list.count(2)) :    new_position = msg_list.index(2) + 1    msg_list = msg_list[first_position:]    first_position += new_position    print first_position# 或者使用index# for i in range(msg_list.count(2)):#     if first_position == 0 :#         first_position = msg_list.index(2)#     else:#         first_position = msg_list.index(2, first_position + 1)#     print first_position

4, 元組

元組和list的區別是, 元組的資料不可變, 需要寫在() 裡面的, 用都好隔開

其他方法和list同

5, sets

sets 是無序不重複的序列, 功能是進行成員關係的測試和重複資料刪除元素

建立空集合需要用set(), 因為{} 是用來建立空的dict

主要用來進行集合運算

#!/usr/bin/python3 student = {‘Tom‘, ‘Jim‘, ‘Mary‘, ‘Tom‘, ‘Jack‘, ‘Rose‘} print(student)   # 輸出集合,重複的元素被自動去掉 # 成員測試if(‘Rose‘ in student) :    print(‘Rose 在集合中‘)else :    print(‘Rose 不在集合中‘)  # set可以進行集合運算a = set(‘abracadabra‘)b = set(‘alacazam‘) print(a) print(a - b)     # a和b的差集 print(a | b)     # a和b的並集 print(a & b)     # a和b的交集 print(a ^ b)     # a和b中不同時存在的元素

6, dict

字典是類型java map的一種集合, 存放的key-value的形式的值

dict = {‘Alice‘: ‘2341‘, ‘Beth‘: ‘9102‘, ‘Cecil‘: ‘3258‘}# 訪問字典中的值, 沒有則報錯print "dict[‘Name‘]: ", dict[‘Name‘];print "dict[‘Age‘]: ", dict[‘Age‘];# 修改字典dict[‘Age‘] = 8; # update existing entrydict[‘School‘] = "DPS School"; # Add new entry# 刪除字典del dict[‘Name‘]; # 刪除鍵是‘Name‘的條目dict.clear();     # 清空詞典所有條目del dict ;        # 刪除詞典

字典的值可以是任意python對象,鍵不可改變且不可重複, 所以鍵只能是數字, 字串或者元群組類型

 

02-python基礎資料型別 (Elementary Data Type)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.