好记性不如烂笔头.
系统:Win10 64bit
Python: 3.9.1
学到这一章节,感觉unity的C#可以无缝切换过来。语法基本一致,出了末尾没有分号;
数据变量以及字符串的常用方法
-
counter = 100 # 赋值整型变量 miles = 1000.0 # 浮点型 name = 'my name is dobby' # 字符串 career = "STUDENT" m1 = 'i "like you' m2 = 'i like "candy"' m3 = 'i like \'u' # \ 转义字符 desc = ' abcd efg ' print(counter) print(miles) print(name) print('=====================') print(m1) print(m2) print(m3) print('=====================') print(name.title()) # 单词首字母大写 print(name.upper()) # 全部大写 print(career.lower()) # 全部小写 print(len(career)) # 获取字符串长度 print(desc.lstrip()) # 去除头部空格 print(desc.rstrip()) # 去除末尾空格 print(desc.strip()) # 去除头尾空格 print('=====================') print("a b") print("a b") # 制表符 print("a\tb") # 制表符 print("a\nb") # 换行符
-
输出结果为:
100 1000.0 my name is dobby ===================== i "like you i like "candy" i like 'u ===================== My Name Is Dobby MY NAME IS DOBBY student 7 abcd efg abcd efg abcd efg ===================== a b a b a b a b 请按任意键继续. . .
数学运算符和赋值运算符
-
r1 = 3 + 3 r2 = 5 - 9 r3 = 8 * 2 r4 = 4 / 3 r5 = 7 % 3 # 求余运算 取模运算 r6 = 4 // 3 # 只保留整数的除法 r7 = 2 ** 3 # 幂运算 # 运算符的优先级 r8 = 3 + 4 * 2 # 修改优先级 r9 = (3 + 4) * 2 print(r1,r2,r3,r4) print(r5,r6,r7,r8,r9)
-
输出结果为:
6 -4 16 1.3333333333333333 1 1 8 11 14 请按任意键继续. . .
字符串和数字的相加与相乘运算
-
msg = "hello" * 3 print(msg) cc = 6 / 4 # Python3: 1.5 # Python2: 1 print(cc)
-
输出结果为:
hellohellohello 1.5 请按任意键继续. . .
使用input做加法运算器
-
print("请任意输入然后回车:") res = input() print("请输入数字a:") a = input() print("请输入数字b:") b = input() print("\nres:", res) print(a + b)
-
输出结果为:
请任意输入然后回车: abc 请输入数字a: 12 请输入数字b: 24 res: abc 1224 请按任意键继续. . .