码迷,mamicode.com
首页 > 其他好文 > 详细

变量的管理 -三元运算

时间:2017-05-12 22:12:17      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:对象 类 方法

作用域:

对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用。

#只要内存里存在,则就能使用。()



name = ‘dick‘

if 1==1:

   name = ‘sb‘

   print name

else:

   name = ‘2b‘

# 值1 if条件 else 值2

上面可以转换成:

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

eg

1.用户输入

2.运算,得结果:如果用户输入dick,得出结果sb,如果不是dick,结果是好人

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

 

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

print result

执行结果:

E:\>pythontest.py

please input yourname:huwei

好人

 

E:\>pythontest.py

please input yourname:dick

sb

 

1.对于Python,一切事物都是对象,对象都是基于类创建的

 

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

 

3.help(类型名) 查看类中所有详细的功能

 

4.help(类型名.功能名) 查看类中某功能的详细

 

 

数据类型的内置方法:

 

示例:

>>>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.带下划线的表示内置方法:可能有多种执行方式,至少有一种

2.不带下划线的表示非内置方法:只有一种执行方式

 

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

 


本文出自 “技术社区” 博客,请务必保留此出处http://2889688.blog.51cto.com/2879688/1925110

变量的管理 -三元运算

标签:对象 类 方法

原文地址:http://2889688.blog.51cto.com/2879688/1925110

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!