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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

第一章 TensorFlow基础——python语法(二)

發(fā)布時(shí)間:2025/1/21 python 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一章 TensorFlow基础——python语法(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡明Python基礎(chǔ)(二)

此為jupyter notebook導(dǎo)出文檔,如果習(xí)慣jupyter界面可以下載文件

鏈接:https://pan.xunlei.com/s/VMn5sAsjVypJElzIIa8PJ1G7A1
提取碼:pb54
復(fù)制這段內(nèi)容后打開手機(jī)迅雷App,查看更方便

文章目錄

  • 簡明Python基礎(chǔ)(二)
    • 1. 字符串
    • List(列表)
    • Tuple元組
    • Set集合
    • Dictionary字典
    • print格式化輸出
      • 格式化操作符輔助指令
    • 類型轉(zhuǎn)換

1. 字符串

字符串可以用雙引號修飾,也可以用單引號。

var1 = "I love Python" var2 = '我愛中華'#python3直接支持中文等符號,包括標(biāo)識符print(var1,type(var1)) print(var2,type(var2)) I love Python <class 'str'> 我愛中華 <class 'str'>

輸出本身帶有引號的字符串

#單引號和雙引號交替使用#輸出 He said "hello" print('He said "hello" ')#輸出 He said 'hello' print("He said 'hello'") He said "hello" He said 'hello'

使用轉(zhuǎn)義字符 反斜杠"\"

# \n 換行符 print("This is first line \nThis is second line") This is first line This is second line

轉(zhuǎn)義字符是反斜杠"\",如果你不想讓反斜杠發(fā)生轉(zhuǎn)義,可以在字符串前面添加一個r,表示原始字符串

print(r"This is first line \nThis is second line") This is first line \nThis is second line

多行字符串可以通過三個連續(xù)的單引號或三個雙引號修飾

print("""first line second line third line""") first line second line third line

使用 + 進(jìn)行字符串鏈接:

"Hello"+"World" 'HelloWorld'

使用 * 進(jìn)行字符串鏈接

"Bye" * 2 'ByeBye'

Python中沒有單獨(dú)的單個字符類型,要注意字符串和數(shù)字之間的區(qū)別:

4 + 5 9 "4" + "5" '45' "4" + 5 ---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-11-42fa82f3c420> in <module> ----> 1 "4" + 5TypeError: can only concatenate str (not "int") to str

List(列表)

List(列表)是Python中使用最頻繁的數(shù)據(jù)類型

列表是寫在方括號[ ]之間的、元素之間用逗號隔開

list1 = [1,2,3,4,5,6] print(list1) [1, 2, 3, 4, 5, 6]

列表中元素的類型可以不相同,它支持?jǐn)?shù)字,字符串甚至可以包含列表(所謂嵌套)

list2 = [1,2,3,4,5,6,"hello,world",[8,9,10,11,12]] print(list2) [1, 2, 3, 4, 5, 6, 'hello,world', [8, 9, 10, 11, 12]]

列表元素訪問,可以通過索引(下標(biāo))和截取(切片),列表被截取后趕回一個包含所需元素的新列表

單個列表元素訪問的語法格式為:列表名[下標(biāo)]

列表下標(biāo)從0開始,-1表示倒數(shù)第1個

list1 = [1,2,3,4,5,6] list1[0] 1 list1[2] 3 list1[-1] 6 list1[-3] 4

下標(biāo)訪問不能越界

list1[6] ---------------------------------------------------------------------------IndexError Traceback (most recent call last)<ipython-input-18-fa2dc7f71df5> in <module> ----> 1 list1[6]IndexError: list index out of range

列表截取的語法格式為:列表名[頭下標(biāo):尾下標(biāo)](結(jié)果不包含尾下標(biāo)

list1[0:3] [1, 2, 3]

切片步長

#不限制頭尾,步長為2 list1[::2] [1, 3, 5] list1[1::2] [2, 4, 6]

訪問嵌套列表元素:層層深入

list2 = [1,2,3,4,5,6,"hello,world",[8,9,10,11,12]] print(list2[-1][1:]) [9, 10, 11, 12]

字符串是一種特殊列表,可以按照列表元素的訪問方法來訪問字符串中的元素

str1 = "hello,hangzhou!" print(str1[2:5]) llo print(str1[-1]) !

Tuple元組

元組(tuple)與列表類似,不同之處在于元組的元素不能修改

元組寫在小括號()里,元素之間用逗號隔開

元組中元素的類型可以不相同,和列表類似,也支持嵌套

tuple1 = (1,2,3,4,5,6,"hello,world",[8,9,10,11,12],(13,14)) print(tuple1,type(tuple1)) (1, 2, 3, 4, 5, 6, 'hello,world', [8, 9, 10, 11, 12], (13, 14)) <class 'tuple'>

元組的元素訪問和截取方式與列表相同,通過下標(biāo)來操作

tuple1 = (1,2,3,4,5,6,"hello,world",[8,9,10,11,12],(13,14)) print(tuple1[0]) print(tuple1[-1]) print(tuple1[6:-1]) print(tuple1[-1][-1]) 1 (13, 14) ('hello,world', [8, 9, 10, 11, 12]) 14

元組一旦定義好不能修改,是只讀的|

tuple1[1] = 7 ---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-27-721c4f9288a7> in <module> ----> 1 tuple1[1] = 7TypeError: 'tuple' object does not support item assignment #列表可以更改 list1 = [1,2,3,4,5] list1[1] = 2000 print(list1) [1, 2000, 3, 4, 5]

Set集合

集合(set)是一個無序、且不含重復(fù)元素的序列

集合主要用來進(jìn)行成員關(guān)系測試和刪除重復(fù)元素

可以使用大括號{}或者set()函數(shù)創(chuàng)建集合

# 自動去除重復(fù)元素 set1 = {1,5,3,5,5,3,1} print(set1) {1, 3, 5} 5 in set1 True 8 in set1 False set1 = {1,2,3} set2 = {2,4,5} #集合的并 set1 | set2 {1, 2, 3, 4, 5} #集合的交 set1 & set2 {2} #集合的差 set1 - set2 {1, 3} # 集合的補(bǔ),兩個集合中不同時(shí)存在的元素 set1 ^ set2 {1, 3, 4, 5} # 集合的補(bǔ) = 集合的并 - 集合的交 (set1|set2)-(set1&set2) {1, 3, 4, 5}

Dictionary字典

字典是一種映射類型,用"{"標(biāo)識,它是一個無序的 鍵(key):值(value)

對集合鍵(key)必須使用不可變類型,在同一個字典中,鍵(key)是唯一的

字典當(dāng)中的元素是通過鍵來存取的

dict1 = {"name":"giggle","height":176,"weight":72} dict1["height"] 176 #修改字典某項(xiàng)值 dict1["weight"] = 73 print(dict1) {'name': 'giggle', 'height': 176, 'weight': 73} #在字典中增加一項(xiàng) dict1["sex"] = "M" print(dict1) {'name': 'giggle', 'height': 176, 'weight': 73, 'sex': 'M'} #構(gòu)建空字典 dict2 = {} #通過元組序列構(gòu)造字典 dict2 = dict([('name','giggle'),('height',176)]) print(dict2) {'name': 'giggle', 'height': 176} #通過dict函數(shù)構(gòu)造字典 dict2 = dict(name = 'giggle',weight = 72) print(dict2) {'name': 'giggle', 'weight': 72}

字典中的內(nèi)置函數(shù),例如clear()、keys()、values

dict2.keys() dict_keys(['name', 'weight']) dict2.values() dict_values(['giggle', 72]) dict2.clear() print(dict2) {}

print格式化輸出

字符串格式化符號描述
%c格式化字符及其ASCII碼
%s格式化字符串
%d格式化整數(shù)
%u格式化無符號整型
%o格式化無符號八進(jìn)制數(shù)%x格式化無符號十六進(jìn)制數(shù)
%X格式化無符號十六進(jìn)制數(shù)(大寫)
%f格式化浮點(diǎn)數(shù)字,可指定小數(shù)點(diǎn)后的精度
%e用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù)
%E作用同%e,用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù)
%g等同于%f和%e的簡寫
%G等同于%f和%E的簡寫

格式化輸出格式: print(“格式化字符串” % 帶格式化的輸出內(nèi)容)

# %c格式化字符 # 輸入數(shù)字理解為ASCII碼 print("%c" % "A") print("%c" % 65) A A # %s格式化字符串 print("%s" % "Hello,world") Hello,world # %d格式化整數(shù) print("%d" % 10) 10 # %o 格式化無符號八進(jìn)制數(shù) print("%o" % 10) 12 #%x 格式化無符號十六進(jìn)制數(shù) #%X 格式化無符號十六進(jìn)制數(shù)(大寫) print("%x" % 20) 14 #在十六進(jìn)制前面顯示'0x'或者'0X'(取決于用的是'x'還是'X') print("%#X" % 20) 0X14 # %f 格式化浮點(diǎn)數(shù)字,可指定小數(shù)點(diǎn)后的精度 print("%f" % 3.14) 3.140000 # %e 用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù) # %E 作用同%e print("%e" % 314000000) 3.140000e+08 # %g 是%f和%e的簡寫 # %G 是%F和%E的簡寫 # 自動根據(jù)數(shù)字大小顯示科學(xué)計(jì)數(shù)法還是普通顯示 print("%g" % 314000) print("%g" % 3140000000000) 314000 3.14e+12

格式化操作符輔助指令

  • m.n. m是顯示的最小總寬度(如果指定的話),n是小數(shù)點(diǎn)后的位數(shù)(如果指定的話)
  • *定義寬度或者小數(shù)點(diǎn)精度
  • -用做左對齊
  • +在正數(shù)前面顯示加號+
  • 在正數(shù)前面顯示空格#
    • 在八進(jìn)制數(shù)前面顯示零(‘0’)
    • 在十六進(jìn)制前面顯示’ox’或者’oX’(取決于用的是’x’還是’X’)
  • ‘%%‘輸出一個單一的’%’
  • (var)映射變量(字典參數(shù))
# m.n. m是顯示的最小總寬度(如果指定的話),n是小數(shù)點(diǎn)后的位數(shù)(如果指定的話) # 默認(rèn)右對齊 最小寬度不足前面加空格 小數(shù)點(diǎn)算寬度 print("%5.2f" % 31400.1234) print("%5.2f" % 3.1234) 31400.123.12 # -用做左對齊 print("%10.6f" % 3.1234) print("%-10.6f" % 3.1234) 3.123400 3.123400 # +在正數(shù)前面顯示加號+ print("%+10.6f" % 3.1234) print("%+-10.6f" % 3.1234) +3.123400 +3.123400 # 用*從后面元組中讀取字段寬度或精度 # 5填充*位置 pi = 3.1415926 print("pi = %.*f" % (5,pi)) pi = 3.14159

如果想通過變量來填充格式控制字符串,那么可以使用運(yùn)算符(%)和一個元組,在目標(biāo)字符串中從左至右使用%來指代變量的位置

# 根據(jù)元組內(nèi)的順序,第一個值填充到第一個百分號,以此類推 # %可以增加格式化 print("I like %s and can eat %.2f kg." % ("orange",1.5)) I like orange and can eat 1.50 kg.

使用字典來對應(yīng)填充

# (var)映射變量(字典參數(shù)) print("I like %(fruit_name)s and can eat %(weight)8.2f kg." % {"fruit_name" : "orange" , "weight" : 1.5}) I like orange and can eat 1.50 kg.

類型轉(zhuǎn)換

數(shù)據(jù)類型的轉(zhuǎn)換,只需要將數(shù)據(jù)類型作為函數(shù)名即可使用

這些函數(shù)返回一個新的對象,表示要轉(zhuǎn)換的值,如:int(),float(),str()

x = "6" print(x,type(x)) x = int("6") print(x,type(x)) 6 <class 'str'> 6 <class 'int'> float("1.25") 1.25 str(4) '4' # 不能轉(zhuǎn)換的值會出錯 int("a") ---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-65-458c4e6ac79c> in <module>1 # 不能轉(zhuǎn)換的值會出錯 ----> 2 int("a")ValueError: invalid literal for int() with base 10: 'a' # 字符轉(zhuǎn)化為ASCII碼值 ord("a") 97 # 將ASCII碼值轉(zhuǎn)化為字符 chr(65) 'A' # 元組、列表、字典 之間的相互轉(zhuǎn)化 tuple1 = (1,2,3,4,5,5,5) list1 = list(tuple1) set1 = set(list1) print(tuple1) print(list1) print(set1) (1, 2, 3, 4, 5, 5, 5) [1, 2, 3, 4, 5, 5, 5] {1, 2, 3, 4, 5} # 元組和字典的相互轉(zhuǎn)換 tuple1 = (("name","giggle"),("height",176)) dict1 = dict(tuple1) print(tuple1) print(dict1) (('name', 'giggle'), ('height', 176)) {'name': 'giggle', 'height': 176}

超強(qiáng)的表達(dá)式計(jì)算(表達(dá)式字符串到數(shù)值的轉(zhuǎn)換)

x = 8 calc = "5*x+9" eval(calc) 49 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的第一章 TensorFlow基础——python语法(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。