>>> new_list = [1, 2, 3, 4, 5, 6]
>>> new_list == sorted(new_list)
True
>>> new_list = [1, 2, 3, 4, 8, 6, 5]
>>> new_list == sorted(new_list)
False
>>> num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> len_num_list = len(num_list) + 1
>>> start_slice = 0
>>> for i in range(5, len_num_list, 5):
print(num_list[start_slice:i])
start_slice += i
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
>>> import random
>>> new_num_list = num_list[:]
>>> random.shuffle(new_num_list)
>>> start_slice = 0
>>> for i in range(5, len_num_list, 5):
print(new_num_list[start_slice:i])
start_slice += i
[8, 2, 9, 3, 6]
[10, 1, 4, 7, 5]
>>> text = ['1', '12', '123', '11', '1', '12', '12']
>>> from collections import Counter
>>> text_counts = Counter(text)
>>> text_counts
Counter({'12': 3, '1': 2, '11': 1, '123': 1})
>>> top_two = text_counts.most_common(2)
>>> top_two
[('12', 3), ('1', 2)]
>>> list = [1, 2, 3, 4, 5, 6]
>>> new_list = [str(i)+'\n' for i in list]
>>> with open('file.txt', 'w') as f:
f.writelines(new_list)
>>> def my_func():
name = input('What is your name?: ')
print(name)
>>> my_func()
What is your name?: Alex
Alex
>>> my_func()
What is your name?: Bob
Bob
>>> def my_func(name):
print(name)
>>> my_func('Alex')
Alex
>>> my_func('Bob')
Bob
>>> stats = [["сила",0],["здоровье", 1],["мудрость",0],["ловкость", 0]]
>>> stats[0][1] += 1
>>> stats
[['сила', 1], ['здоровье', 1], ['мудрость', 0], ['ловкость', 0]]
>>> stats[1][1] += 5
>>> stats
[['сила', 1], ['здоровье', 6], ['мудрость', 0], ['ловкость', 0]]
>>> stats[1][1] -= 2
>>> stats
[['сила', 1], ['здоровье', 4], ['мудрость', 0], ['ловкость', 0]]
>>> text = '''
[attachmentid=1923106]
[attachmentid=1923108]
[attachmentid=1923110]
[attachmentid=1923112]
[attachmentid=1923114]
'''
>>> text[0]
'\n'
>>> text[1]
'['
>>> new_text = text.split()
>>> new_text[0]
'[attachmentid=1923106]'
>>> new_text[1]
'[attachmentid=1923108]'
>>> s = 'ABC' #iterable, но не iterator
>>> for char in s:
print(char)
A
B
C
>>> s = 'ABC'
>>> it = iter(s) #а вот уже iterator, созданный из iterable
>>> for i in range(4):
print(next(it))
A
B
C
Traceback (most recent call last):
File "<pyshell#35>", line 2, in <module>
print(next(it))
StopIteration
>>>
>>> class M:
data = []
def __init__(self, value):
self.value = value
>>> x = M(1)
>>> y = M(2)
>>> x.value
1
>>> y.value
2
>>> x.data
[]
>>> y.data
[]
>>> x.data.append(10)
>>> x.data
[10]
>>> y.data
[10]
>>> x.value += 10
>>> x.value
11
>>> y.value
2
>>> text = 'eujiyghkiuyhjiu'
>>> from collections import Counter
>>> Counter(text)
Counter({'u': 3, 'i': 3, 'h': 2, 'j': 2, 'y': 2, 'e': 1, 'k': 1, 'g': 1})