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 -*- | |
""" | |
Определение функции | |
""" | |
def square(x): | |
'Calculates the square of the number x.' | |
return x*x | |
print square(100) |
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 -*- | |
""" | |
Деление на ноль | |
""" | |
try: 1/0 | |
except ZeroDivisionError: | |
print "caught divide-by-0 attempt" |
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 -*- | |
""" | |
Базовый синтаксис | |
""" | |
try: | |
num = float(raw_input("\nEnter a number: ")) | |
except(ValueError): | |
print "That was not a number!" | |
else: |
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 -*- | |
""" | |
Итерация по списку | |
""" | |
words = ['A', 'B', 'C', 'D', 'E'] | |
for word in words: | |
print word |
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 | |
while x <= 100: | |
print x | |
x += 1 |
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 = int(raw_input("Please enter an integer: ")) | |
if x < 0: | |
x = 0 | |
print 'Negative changed to zero' | |
elif x == 0: |
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 -*- | |
""" | |
Логическое И | |
""" | |
print 'a' and 'b' | |
print '' and 'b' | |
print 'a' and 'b' and 'c' |
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 = 0 # x bound to an integer object | |
print x | |
x = "Hello" # now it's a string | |
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 -*- | |
""" | |
Приоритет операторов | |
""" | |
#--+--------------------+------------------------ | |
# | Operator | Description | |
#--+--------------------+------------------------ | |
# lambda | Lambda Expression | |
# or | Boolean OR |
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 -*- | |
""" | |
Открытие файла | |
""" | |
#open() returns a file object, and is most commonly used with two arguments: | |
#"open(filename, mode)". | |
f=open('/tmp/workfile', 'w') | |
print f |