Created
December 11, 2011 19:29
-
-
Save dketov/1462253 to your computer and use it in GitHub Desktop.
Логические значения и битовые операции
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Единица - это истина | |
""" | |
if 1: | |
print 'true' | |
if not 1: | |
print 'true' | |
else: | |
print 'false' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Что есть истина | |
""" | |
# 0 is false; all other numbers are true. | |
print 'true' if 0 else 'false' | |
# An empty string ("") is false, all other strings are true. | |
print 'true' if "" else 'false' | |
# An empty list ([]) is false; all other lists are true. | |
print 'true' if [] else 'false' | |
# An empty tuple (()) is false; all other tuples are true. | |
print 'true' if () else 'false' | |
# An empty dictionary ({}) is false; all other dictionaries are true. | |
print 'true' if {} else 'false' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Число | |
""" | |
a = 0 | |
if a: | |
print "Here" | |
a = 2 | |
if a: | |
print "There" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Сложный тип | |
""" | |
if []: | |
print "empty." | |
if [1]: | |
print "not empty." | |
if (): | |
print "empty." | |
if (1): | |
print "not empty." | |
if {}: | |
print "empty." | |
if {1:0}: | |
print "not empty." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Побитовое ИЛИ | |
""" | |
x = 1 # 0001 | |
x | 2 # bitwise OR: 0011 | |
print x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Побитовое И | |
""" | |
x = 1 # 0001 | |
x & 1 # bitwise AND: 0001 | |
print x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Побитовый сдвиг | |
""" | |
x = 1 # 0001 | |
x << 2 # shift left 2 bits: 0100 | |
print x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment