Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 12, 2011 15:36
Show Gist options
  • Save dketov/1467905 to your computer and use it in GitHub Desktop.
Save dketov/1467905 to your computer and use it in GitHub Desktop.
Циклы с параметром
# -*- encoding: utf-8 -*-
"""
Итерация по списку
"""
words = ['A', 'B', 'C', 'D', 'E']
for word in words:
print word
# -*- encoding: utf-8 -*-
"""
Итерация по словарю
"""
dict = {'x': 1, 'y': 2, 'z': 3}
for key in dict:
print key, 'corresponds to', dict[key]
# -*- encoding: utf-8 -*-
"""
Использование функции range()
"""
from math import sqrt
for n in range(99, 0, -1):
root = sqrt(n)
if root == int(root):
print n
break
# -*- encoding: utf-8 -*-
"""
Подсчёт сложных процентов
"""
principal = 1000.0 # starting principal
rate = .05 # interest rate
print "Year %21s" % "Amount on deposit"
for year in range( 1, 11 ):
amount = principal * ( 1.0 + rate ) ** year
print "%4d%21.2f" % ( year, amount )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment