Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 11, 2011 19:29
Show Gist options
  • Save dketov/1462253 to your computer and use it in GitHub Desktop.
Save dketov/1462253 to your computer and use it in GitHub Desktop.
Логические значения и битовые операции
# -*- encoding: utf-8 -*-
"""
Единица - это истина
"""
if 1:
print 'true'
if not 1:
print 'true'
else:
print 'false'
# -*- 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'
# -*- encoding: utf-8 -*-
"""
Число
"""
a = 0
if a:
print "Here"
a = 2
if a:
print "There"
# -*- 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."
# -*- encoding: utf-8 -*-
"""
Побитовое ИЛИ
"""
x = 1 # 0001
x | 2 # bitwise OR: 0011
print x
# -*- encoding: utf-8 -*-
"""
Побитовое И
"""
x = 1 # 0001
x & 1 # bitwise AND: 0001
print x
# -*- 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