2016年3月23日 星期三

[python] python 3.x 新手的一些小觀念 (tuple, 三元運算子,型別物件 type object )



(0) #是一個整數 0 的 int 物件
(0,)#是一個有一個元素是0的tuple物件


--------------------------------



三元運算子(x if y > 0 else z)=>如果 y 對, 走x, y錯 走z

x,y,z=3,-1,6
print(x if y>0 else z)

x,y,z=3,6,6
print(x if y>0 else z)

6
3
[Finished in 0.1s]


--------------------------------------------------

type is an object also
型別也是一個物件(所以可以assign給別人), 叫做物件型別

ex:
a=3 #a is assigned to int object, value=3
print (type(a)) #the type is int


=><class 'int'>


# type(a) return a type, and type also is an object , so it can assign to atype.
# so we still can check the type of atype.
#which means we will see the return type of type(a)


atype=type(a) 
print(type(atype))


=><class 'type'>

# and it shows <class 'type'>, which means we got a object named type.







沒有留言:

張貼留言