In [7]:
# http://python.math-hse.info
my_numbers = [6.0, 8, 2, 15]
for number in my_numbers:
    print(number + 1)
    print("Next number, please")
print("That's all")
7.0
Next number, please
9
Next number, please
3
Next number, please
16
Next number, please
That's all
In [9]:
# дан список, содержащий числа
# найти сумму этих чисел
numbers = [7, 8, 12, 765, 3, 8, 65.7]
s = 0
for number in numbers:
    s = s + number
print(s)
868.7
In [10]:
sum(numbers)
Out[10]:
868.7
In [11]:
sum = 7
In [12]:
sum(numbers)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-73ee437a0094> in <module>()
----> 1 sum(numbers)

TypeError: 'int' object is not callable
In [13]:
del sum
In [14]:
sum(numbers)
Out[14]:
868.7
In [18]:
some_str = "this     is \t a\n\ntest"
In [21]:
some_str
Out[21]:
'this     is \t a\n\ntest'
In [22]:
print(some_str)
this     is 	 a

test
In [23]:
words = some_str.split()
In [24]:
words
Out[24]:
['this', 'is', 'a', 'test']
In [25]:
s = "Hello, world! This is a test!"
In [26]:
sentences = s.split("!")
In [27]:
sentences
Out[27]:
['Hello, world', ' This is a test', '']
In [28]:
s
Out[28]:
'Hello, world! This is a test!'
In [44]:
words = ["hello", "world", "test"]
line = ", ".join(words)
In [45]:
line
Out[45]:
'hello, world, test'
In [31]:
'"'
Out[31]:
'"'
In [47]:
for i in range(5):
    print("Hello!")
    print("i =", i)
Hello!
i = 0
Hello!
i = 1
Hello!
i = 2
Hello!
i = 3
Hello!
i = 4
In [48]:
range(4)
Out[48]:
range(0, 4)
In [49]:
list(range(5))
Out[49]:
[0, 1, 2, 3, 4]
In [50]:
list(range(2, 5))
Out[50]:
[2, 3, 4]
In [53]:
list(range(2, 17, 3))
Out[53]:
[2, 5, 8, 11, 14]
In [56]:
list(range(9, 1, -1))
Out[56]:
[9, 8, 7, 6, 5, 4, 3, 2]
In [57]:
# Don't!
numbers = [7, 8, 2, 54]
s = 0
for i in range(len(numbers)):
    s = s + numbers[i]
print(s)
71
In [58]:
len(numbers)
Out[58]:
4
In [59]:
my_list = [0, 10, 20, 30, 40, 50]
In [60]:
my_list[2:4]
Out[60]:
[20, 30]
In [61]:
my_list[1:5:2]
Out[61]:
[10, 30]
In [62]:
my_list[::-1]
Out[62]:
[50, 40, 30, 20, 10, 0]
In [63]:
numbers_as_str = ['7', '12', '3', '45']
# создать список, в котором эти числа записаны как числа
numbers_as_int = []
for number in numbers_as_str:
    numbers_as_int.append(int(number))
In [64]:
numbers_as_int
Out[64]:
[7, 12, 3, 45]
In [65]:
type(3.)
Out[65]:
float
In [66]:
numbers = [6, 9, 15]
for number in numbers:
    number = number + 1
print(numbers)
[6, 9, 15]
In [67]:
x, y = [5, 10]
In [68]:
my_tuple = (6, 12, "Hello")
In [69]:
my_tuple
Out[69]:
(6, 12, 'Hello')
In [70]:
my_tuple[1]
Out[70]:
12
In [71]:
my_tuple[1] = 9876
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-71-33e3f1fa376c> in <module>()
----> 1 my_tuple[1] = 9876

TypeError: 'tuple' object does not support item assignment
In [80]:
pairs = [(1, 6), (8, 3), (2, 5), (7, 3)]
In [81]:
pairs[2]
Out[81]:
(2, 5)
In [82]:
pairs
Out[82]:
[(1, 6), (8, 3), (2, 5), (7, 3)]
In [83]:
for a, b in pairs:
    print("a =", a)
    print("b =", b)
    print("Next item")
a = 1
b = 6
Next item
a = 8
b = 3
Next item
a = 2
b = 5
Next item
a = 7
b = 3
Next item
In [84]:
some_list = ["Hello", "world", "test"]
for i, element in enumerate(some_list):
    print("word", element, "position", i)
word Hello position 0
word world position 1
word test position 2
In [85]:
list(enumerate(some_list))
Out[85]:
[(0, 'Hello'), (1, 'world'), (2, 'test')]
In [93]:
numbers = [3, 8, 9, 12]
for i, x in enumerate(numbers):
    numbers[i] = x + 1
In [91]:
numbers
Out[91]:
[4, 9, 10, 13]
In [92]:
numbers
Out[92]:
[4, 9, 10, 13]
In [89]:
for x in numbers[1:]:
    print(x)
9
10
13
In [94]:
print(*numbers, sep="\n")
4
9
10
13
In [ ]: