溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

變量的管理 -三元運(yùn)算

發(fā)布時(shí)間:2020-06-29 06:56:47 來(lái)源:網(wǎng)絡(luò) 閱讀:604 作者:huwei0512 欄目:開發(fā)技術(shù)

作用域:

對(duì)于變量的作用域,執(zhí)行聲明并在內(nèi)存中存在,該變量就可以在下面的代碼中使用。

#只要內(nèi)存里存在,則就能使用。()



name = 'dick'

if 1==1:

   name = 'sb'

   print name

else:

   name = '2b'

# 值1 if條件 else 值2

上面可以轉(zhuǎn)換成:

name = 'sb' if 1==1 else '2b'

eg

1.用戶輸入

2.運(yùn)算,得結(jié)果:如果用戶輸入dick,得出結(jié)果sb,如果不是dick,結(jié)果是好人

content = raw_input('please input your name:')

 

result = 'sb' if content == 'dick' else '好人'

print result

執(zhí)行結(jié)果:

E:\>pythontest.py

please input yourname:huwei

好人

 

E:\>pythontest.py

please input yourname:dick

sb

 

1.對(duì)于Python,一切事物都是對(duì)象,對(duì)象都是基于類創(chuàng)建的

 

2.type(類型名查看類中提供的所有功能

 

3.help(類型名) 查看類中所有詳細(xì)的功能

 

4.help(類型名.功能名) 查看類中某功能的詳細(xì)

 

 

數(shù)據(jù)類型的內(nèi)置方法:

 

示例:

>>>dir(list)

['__add__','__class__', '__contains__', '__delattr__', '__delitem__', '__delsli

ce__', '__doc__','__eq__', '__format__', '__ge__', '__getattribute__', '__getit

em__','__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',

 '__iter__', '__le__', '__len__', '__lt__','__mul__', '__ne__', '__new__', '__r

educe__','__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__'

, '__setitem__','__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a

ppend', 'count','extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'

 

類中的方法:

1.帶下劃線的表示內(nèi)置方法:可能有多種執(zhí)行方式,至少有一種

2.不帶下劃線的表示非內(nèi)置方法:只有一種執(zhí)行方式

 

>>>dir(int)

['__abs__','__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delat

tr__', '__div__','__divmod__', '__doc__', '__float__', '__floordiv__', '__forma

t__','__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__',

'__init__','__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul

__', '__neg__','__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow

__', '__radd__','__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_

ex__','__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ro

r__', '__rpow__','__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rx

or__','__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '_

_truediv__','__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', '

imag','numerator', 'real']

 

>>>n1.__add__(n2)

6

 

>>>n2.__divmod__(9)

(11, 0)

 

>>>n2.__float__()

99.0

>>> ah =n2.__float__()

>>>type(ah)

<type'float'>

 

>>> n1 =9

>>>n1.__neg__()

-9

>>>n1.__oct__()

'011'

 

>>>n1.__pow__(8)

43046721

 

>>> n1 =-9

>>>n1.__abs__()

9

 

divmod

 


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI