欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > >内容正文

python

第一章 tensorflow基础——python语法(二) -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 16 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 第一章 tensorflow基础——python语法(二) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

此为jupyter notebook导出文档,如果习惯jupyter界面可以下载文件

链接:https://pan.xunlei.com/s/vmn5sasjvypjelziia8pj1g7a1
提取码:pb54
复制这段内容后打开手机迅雷app,查看更方便

文章目录

  • 简明python基础(二)
    • 1. 字符串
    • list(列表)
    • tuple元组
    • set集合
    • dictionary字典
    • print格式化输出
      • 格式化操作符辅助指令
    • 类型转换

1. 字符串

字符串可以用双引号修饰,也可以用单引号。

var1 = "i love python" var2 = '我爱中华'#python3直接支持中文等符号,包括标识符print(var1,type(var1)) print(var2,type(var2)) i love python 我爱中华

输出本身带有引号的字符串

#单引号和双引号交替使用#输出 he said "hello" print('he said "hello" ')#输出 he said 'hello' print("he said 'hello'") he said "hello" he said 'hello'

使用转义字符 反斜杠"\"

# \n 换行符 print("this is first line \nthis is second line") this is first line this is second line

转义字符是反斜杠"\",如果你不想让反斜杠发生转义,可以在字符串前面添加一个r,表示原始字符串

print(r"this is first line \nthis is second line") this is first line \nthis is second line

多行字符串可以通过三个连续的单引号或三个双引号修饰

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

使用 进行字符串链接:

"hello""world" 'helloworld'

使用 * 进行字符串链接

"bye" * 2 'byebye'

python中没有单独的单个字符类型,要注意字符串和数字之间的区别:

4 5 9 "4" "5" '45' "4" 5 ---------------------------------------------------------------------------typeerror traceback (most recent call last) in ----> 1 "4" 5typeerror: can only concatenate str (not "int") to str

list(列表)

list(列表)是python中使用最频繁的数据类型

列表是写在方括号[ ]之间的、元素之间用逗号隔开

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

列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套)

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

列表元素访问,可以通过索引(下标)和截取(切片),列表被截取后赶回一个包含所需元素的新列表

单个列表元素访问的语法格式为:列表名[下标]

列表下标从0开始,-1表示倒数第1个

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

下标访问不能越界

list1[6] ---------------------------------------------------------------------------indexerror traceback (most recent call last) in ----> 1 list1[6]indexerror: list index out of range

列表截取的语法格式为:列表名[头下标:尾下标](结果不包含尾下标

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

元组的元素访问和截取方式与列表相同,通过下标来操作

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) in ----> 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)是一个无序、且不含重复元素的序列

集合主要用来进行成员关系测试和删除重复元素

可以使用大括号{}或者set()函数创建集合

# 自动去除重复元素 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} # 集合的补,两个集合中不同时存在的元素 set1 ^ set2 {1, 3, 4, 5} # 集合的补 = 集合的并 - 集合的交 (set1|set2)-(set1&set2) {1, 3, 4, 5}

dictionary字典

字典是一种映射类型,用"{"标识,它是一个无序的 键(key):值(value)

对集合键(key)必须使用不可变类型,在同一个字典中,键(key)是唯一的

字典当中的元素是通过键来存取的

dict1 = {"name":"giggle","height":176,"weight":72} dict1["height"] 176 #修改字典某项值 dict1["weight"] = 73 print(dict1) {'name': 'giggle', 'height': 176, 'weight': 73} #在字典中增加一项 dict1["sex"] = "m" print(dict1) {'name': 'giggle', 'height': 176, 'weight': 73, 'sex': 'm'} #构建空字典 dict2 = {} #通过元组序列构造字典 dict2 = dict([('name','giggle'),('height',176)]) print(dict2) {'name': 'giggle', 'height': 176} #通过dict函数构造字典 dict2 = dict(name = 'giggle',weight = 72) print(dict2) {'name': 'giggle', 'weight': 72}

字典中的内置函数,例如clear()、keys()、values

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

print格式化输出

字符串格式化符号描述
%c格式化字符及其ascii码
%s格式化字符串
%d格式化整数
%u格式化无符号整型
%o格式化无符号八进制数%x格式化无符号十六进制数
%x格式化无符号十六进制数(大写)
%f格式化浮点数字,可指定小数点后的精度
%e用科学计数法格式化浮点数
%e作用同%e,用科学计数法格式化浮点数
%g等同于%f和%e的简写
%g等同于%f和%e的简写

格式化输出格式: print(“格式化字符串” % 带格式化的输出内容)

# %c格式化字符 # 输入数字理解为ascii码 print("%c" % "a") print("%c" % 65) a a # %s格式化字符串 print("%s" % "hello,world") hello,world # %d格式化整数 print("%d" % 10) 10 # %o 格式化无符号八进制数 print("%o" % 10) 12 #%x 格式化无符号十六进制数 #%x 格式化无符号十六进制数(大写) print("%x" % 20) 14 #在十六进制前面显示'0x'或者'0x'(取决于用的是'x'还是'x') print("%#x" % 20) 0x14 # %f 格式化浮点数字,可指定小数点后的精度 print("%f" % 3.14) 3.140000 # %e 用科学计数法格式化浮点数 # %e 作用同%e print("%e" % 314000000) 3.140000e 08 # %g 是%f和%e的简写 # %g 是%f和%e的简写 # 自动根据数字大小显示科学计数法还是普通显示 print("%g" % 314000) print("%g" % 3140000000000) 314000 3.14e 12

格式化操作符辅助指令

  • m.n. m是显示的最小总宽度(如果指定的话),n是小数点后的位数(如果指定的话)
  • *定义宽度或者小数点精度
  • -用做左对齐
  • 在正数前面显示加号
  • 在正数前面显示空格#
    • 在八进制数前面显示零(‘0’)
    • 在十六进制前面显示’ox’或者’ox’(取决于用的是’x’还是’x’)
  • ‘%%‘输出一个单一的’%’
  • (var)映射变量(字典参数)
# m.n. m是显示的最小总宽度(如果指定的话),n是小数点后的位数(如果指定的话) # 默认右对齐 最小宽度不足前面加空格 小数点算宽度 print("%5.2f" % 31400.1234) print("%5.2f" % 3.1234) 31400.123.12 # -用做左对齐 print(".6f" % 3.1234) print("%-10.6f" % 3.1234) 3.123400 3.123400 # 在正数前面显示加号 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

如果想通过变量来填充格式控制字符串,那么可以使用运算符(%)和一个元组,在目标字符串中从左至右使用%来指代变量的位置

# 根据元组内的顺序,第一个值填充到第一个百分号,以此类推 # %可以增加格式化 print("i like %s and can eat %.2f kg." % ("orange",1.5)) i like orange and can eat 1.50 kg.

使用字典来对应填充

# (var)映射变量(字典参数) 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.

类型转换

数据类型的转换,只需要将数据类型作为函数名即可使用

这些函数返回一个新的对象,表示要转换的值,如:int(),float(),str()

x = "6" print(x,type(x)) x = int("6") print(x,type(x)) 6 6 float("1.25") 1.25 str(4) '4' # 不能转换的值会出错 int("a") ---------------------------------------------------------------------------valueerror traceback (most recent call last) in 1 # 不能转换的值会出错 ----> 2 int("a")valueerror: invalid literal for int() with base 10: 'a' # 字符转化为ascii码值 ord("a") 97 # 将ascii码值转化为字符 chr(65) 'a' # 元组、列表、字典 之间的相互转化 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} # 元组和字典的相互转换 tuple1 = (("name","giggle"),("height",176)) dict1 = dict(tuple1) print(tuple1) print(dict1) (('name', 'giggle'), ('height', 176)) {'name': 'giggle', 'height': 176}

超强的表达式计算(表达式字符串到数值的转换)

x = 8 calc = "5*x 9" eval(calc) 49 与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的第一章 tensorflow基础——python语法(二)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图