PYTHON機器學習自學/自修 整理[00010] ~ 語言技術:PYTHON GOSSIP(運算子)

PYTHON機器學習自學/自修 整理[00010] ~ 語言技術:PYTHON GOSSIP(運算子)

PYTHON機器學習自學/自修 整理[00010] ~ 語言技術:PYTHON GOSSIP(運算子)



關係運算:如>、>=、<、<=、==、!=

邏輯運算:如and、or、not

位 元運算:如&、|、~、^、>>、<<

指 定運算:如+=、-=、*=、/=、%=、&=、|=、>>=、<<=



import sys
import decimal#精準度/精度 運算

StrBuf = 'Just' + 'in'
print(StrBuf)#String組合

ListBuf = [1, 2] + [3, 4]
print(ListBuf)#List組合

TupleBuf = (1, 2) + (3, 4)
print(TupleBuf)#Tuple組合

StrBuf = StrBuf * 4
print(StrBuf)#String複製4倍

ListBuf = ListBuf * 4
print(ListBuf)#List複製4倍

TupleBuf = TupleBuf * 4 
print(TupleBuf)#Tuple複製4倍

intBuf=9**3
print(intBuf)#求次方

fltBuf=9**0.5
print(fltBuf)#開根號

fltBuf=10/3
print(fltBuf)#隱性轉成浮點數

intBuf=10//3
print(intBuf)#取商

intBuf=10%3
print(intBuf)#取餘數

fltBuf=1.0+0.2
print(fltBuf)

fltBuf=1.0-0.2
print(fltBuf)

h = decimal.Decimal('1.0')
i = decimal.Decimal('0.8')
print(h - i)
print(type(h - i))#偵測變數型態

j = 3 + 2j
k = 5 + 3j
l = j + k#複數運算
print(l)
print(type(l))#偵測變數型態

#https://mp.weixin.qq.com/s?__biz=MzAxMTkwODIyNA==&mid=2247507879&idx=2&sn=1de399c7429c60b16e3abd51de7bc959&chksm=9bbb7a48acccf35e6fe8af98f2c95d78bffe0e311c907ace0e6d4eda459ab70475e8aab5d3dd&scene=126&sessionid=1602119485&key=391633c74d74d5c55ed260e1bbec1136afddeb501b02529dc01fe64af57262a8cd16610970c8a7bcc55ca8e16709a1ccd905d96cd4d25bb2e77a102809aa6520eab81c12f0d7b3cc18dc180257c23c7104f0e83272b495622e529e375b1331630d3d27283131539ead27fda7da2cb2644c6ff76e31711a406ebb520cc765d85d&ascene=1&uin=MjIwODk2NDgxNw%3D%3D&devicetype=Windows+10+x64&version=6300002f&lang=zh_TW&exportkey=ApwKVfNUnWvkpZSdCRXfs1M%3D&pass_ticket=Q1hSP1pQjhToIguIwSV1VnQ9A%2FNIuppXZI6XV2MX7y4r7t9bUk%2FtWCKfacHM3qTw&wx_header=0

# 整数
3  # => 3

# 算术没有什么出乎意料的
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20

# 但是除法例外,会自动转换成浮点数
35 / 5  # => 7.0
5 / 3  # => 1.6666666666666667

# 整数除法的结果都是向下取整
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # 浮点数也可以
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# 浮点数的运算结果也是浮点数
3 * 2.0 # => 6.0

# 模除
7 % 3 # => 1

# x的y次方
2**4 # => 16

# 用括号决定优先级
(1 + 3) * 2  # => 8

# 布尔值
True
False

# 用not取非
not True  # => False
not False  # => True

# 逻辑运算符,注意and和or都是小写
True and False # => False
False or True # => True

# 整数也可以当作布尔值
0 and 2 # => 0
-5 or 0 # => -5
0 == False # => True
2 == True # => False
1 == True # => True

# 用==判断相等
print(1 == 1)  # => True
print(2 == 1)  # => False

# 用!=判断不等
print(1 != 1)  # => False
print(2 != 1)  # => True

# 比较大小
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# 大小比较可以连起来!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# 字符串用单引双引都可以
"这是个字符串"
'这也是个字符串'

# 用加号连接字符串
"Hello " + "world!"  # => "Hello world!"

# 字符串可以被当作字符列表
"This is a string"[0]  # => 'T'

# 用.format来格式化字符串
"{} can be {}".format("strings", "interpolated")

# 可以重复参数以节省时间
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
# => "Jack be nimble, Jack be quick, Jack jump over the candle stick"

# 如果不想数参数,可以用关键字
"{name} wants to eat {food}".format(name="Bob", food="lasagna") 
# => "Bob wants to eat lasagna"

# 如果你的Python3程序也要在Python2.5以下环境运行,也可以用老式的格式化语法
"%s can be %s the %s way" % ("strings", "interpolated", "old")

# None是一个对象
None  # => None

# 当与None进行比较时不要用 ==,要用is。is是用来比较两个变量是否指向同一个对象。
#"etc" is None  # => False
None is None  # => True

# None,0,空字符串,空列表,空字典都算是False
# 所有其他值都是True
bool(0)  # => False
bool("")  # => False
bool([]) # => False
bool({}) # => False

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *