关于Python中的逻辑的问题 [英] Question About Logic In Python

查看:81
本文介绍了关于Python中的逻辑的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!我是Python的新手,我正在努力与和和

或 Python中的逻辑。由于Python总是最终返回一个值

,这与C有点不同,我最理解的语言是
(即C将非零返回为真,零如果是假的话,有什么东西

我应该知道Python的不同方法吗?即任何

陷阱或巧妙的技巧,使Python的方法很酷或保存我的

对接。


谢谢!


詹姆斯

解决方案

James H.写道:

问候!我是Python的新手,我正在努力与和和
或 Python中的逻辑。因为Python总是最终返回一个值
这与C语言略有不同,我最理解的语言是
(即C将非零返回为真,零返回为false),是否有任何颜色<我应该知道Python的不同方法吗?即任何使Python处理变冷或保存我的对接的陷阱或巧妙的技巧。




此功能的最常见用途是 ; x = x或default_val作为

的简写,如果不是x:x = default_val"。


另外,你可以模拟C'的三元运算符(cond?t :f)与

(cond和[t]或[f])[0]。




James H.写道:

问候!我是Python的新手,我正在努力与和和
或 Python中的逻辑。因为Python总是最终返回一个值
这与C语言略有不同,我最理解的语言是
(即C将非零返回为真,零返回为false),是否有任何颜色<我应该知道Python的不同方法吗?也就是说任何使得Python方法变得凉爽或者保存我的屁股的任何陷阱或巧妙的技巧。

谢谢!

James




布尔值是普通整数的子类型,所以如果你在算术表达式中使用它们,那么它们会计算为0和1.

def bool():
print" Boolean algebra"

print"%9s% 9s | %9s%9s%9s" %(''A'',''B'','A和B'',''A或B'',''A x或B'')

print" - * 51

for a [False,True]:

for b in [False,True]:

print"% 9s%9s | %9s%9s%9s" %(a,b,(a和b),(a或b),((a而不是

打印

print

print" Arithmetic"

print"%9s%9s | %9s%9s%9s" %(''A'',''B'',''A + B'',''A * B'',''A - B'')

print" - * 51

for a [False,True]:

for b in [False,True]:

print"% 9s%9s | %9s%9s%9s" %(a,b,(a + b),(a * b),(a - b))


bool()



布尔代数

AB | A和BA或BA xor B

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

False False |假错假

假真假假真如实

真假| False True True

True True |真假是

算术

A B | A + BA * BA - B

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

False False | 0 0 0

False True | 1 0 -1

真假| 1 0 1

True True | 2 1 0


2005年9月19日02:20,James H.写道:

问候!我是Python的新手,我正在努力与和和
或 Python中的逻辑。因为Python总是最终返回一个值
这与C语言略有不同,我最理解的语言是
(即C将非零返回为真,零返回为false),是否有任何颜色<我应该知道Python的不同方法吗?也就是说,任何使得Python方法变得酷或者保存我的
对接的陷阱或巧妙的技巧。




以确保操作产生布尔值值包装一个bool()

表达式。

无,0和'len为0的对象产生False。

so你也可以这样做:

a = []
b = [1,2 ,3]
b
[1,2,3]

等级Foo:
.... def __len __(self):return 0

....类吧:
.... def __len __(自我):返回1

.... foo = Foo()
bar = Bar()
foo或bar



< __ main__.Bar实例位于0x7D289940>


sven。


Greetings! I''m new to Python and am struggling a little with "and" and
"or" logic in Python. Since Python always ends up returning a value
and this is a little different from C, the language I understand best
(i.e. C returns non-zero as true, and zero as false), is there anything
I should be aware of given Python''s different approach? Namely any
pitfalls or neat tricks that make the Python approach cool or save my
butt.

Thank you!

James

解决方案

James H. wrote:

Greetings! I''m new to Python and am struggling a little with "and" and
"or" logic in Python. Since Python always ends up returning a value
and this is a little different from C, the language I understand best
(i.e. C returns non-zero as true, and zero as false), is there anything
I should be aware of given Python''s different approach? Namely any
pitfalls or neat tricks that make the Python approach cool or save my
butt.



The most common use of this feature is "x = x or default_val" as a
shorthand for "if not x: x = default_val".

Also, you can emulate C''s ternary operator (cond ? t : f) with
(cond and [t] or [f])[0].



James H. wrote:

Greetings! I''m new to Python and am struggling a little with "and" and
"or" logic in Python. Since Python always ends up returning a value
and this is a little different from C, the language I understand best
(i.e. C returns non-zero as true, and zero as false), is there anything
I should be aware of given Python''s different approach? Namely any
pitfalls or neat tricks that make the Python approach cool or save my
butt.

Thank you!

James



Booleans are a subtype of plain integers, so if you use them
in arithmetic expressions, they evaluate to 0 and 1.

def bool(): print "Boolean algebra"
print "%9s %9s | %9s %9s %9s" % (''A'',''B'',''A and B'',''A or B'',''A xor B'')
print "-"*51
for a in [False,True]:
for b in [False,True]:
print "%9s %9s | %9s %9s %9s" % (a,b,(a and b),(a or b),((a and not
b) or (not a and b)))
print
print
print "Arithmetic"
print "%9s %9s | %9s %9s %9s" % (''A'',''B'',''A + B'',''A * B'',''A - B'')
print "-"*51
for a in [False,True]:
for b in [False,True]:
print "%9s %9s | %9s %9s %9s" % (a,b,(a + b),(a * b),(a - b))

bool()


Boolean algebra
A B | A and B A or B A xor B
---------------------------------------------------
False False | False False False
False True | False True True
True False | False True True
True True | True True False
Arithmetic
A B | A + B A * B A - B
---------------------------------------------------
False False | 0 0 0
False True | 1 0 -1
True False | 1 0 1
True True | 2 1 0


At 02:20 19.09.2005, James H. wrote:

Greetings! I''m new to Python and am struggling a little with "and" and
"or" logic in Python. Since Python always ends up returning a value
and this is a little different from C, the language I understand best
(i.e. C returns non-zero as true, and zero as false), is there anything
I should be aware of given Python''s different approach? Namely any
pitfalls or neat tricks that make the Python approach cool or save my
butt.



to make sure that an operation yields a boolean value wrap a bool()
around an expression.
None, 0 and objects which''s len is 0 yield False.
so you can also do stuff like that:

a = []
b = [1,2,3]
a or b [1, 2, 3]
class Foo: .... def __len__(self): return 0
.... class Bar: .... def __len__(self): return 1
.... foo = Foo()
bar = Bar()
foo or bar


<__main__.Bar instance at 0x7D289940>

sven.


这篇关于关于Python中的逻辑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆