Note:
iterable i.e. elements of the object can be sequentially accessed
empty_list = []
square_brackets_list = ["Hello",",","World","!", 2]
list_comprehension = [letter.upper() for letter in "some_string"]
with_type_constructor_list = list("some_string_2")
print(empty_list,square_brackets_list,list_comprehension,with_type_constructor_list,sep="\n")
print(square_brackets_list[0])
print(square_brackets_list[1:3])
list_hse = ['H', 'S', 'E']
# 0 1 2
list_hse[3] = '!'
str_hse = 'HSE'
list_hse[2] = 'Y'
# str_hse[2] = 'Y'
print(list_hse)
print(str_hse)
square_brackets_list[10] = "Error Example" # remember about list range
empty_list.append("new_item") # to the end of the sequence
empty_list
square_brackets_list.insert(1,"INSERTION_AT_1") # into list at the index
square_brackets_list.insert(10,"INSERTION_AT_10")
square_brackets_list
square_brackets_list[10] # list index out of range
square_brackets_list.remove("INSERTION_AT_10") # removing the specified item (tagret), if tagret not in list => ValueError
square_brackets_list
square_brackets_list.pop(1) # removing the specified index, by default the specified index = the last item
square_brackets_list
The del statement is used to delete objects (variables, lists or its parts etc. -- everything is an object)
A useful application is to free memory
a = 1
b_list = [1,2,3,4,'5']
del a
a # name 'a' is not defined
del b_list[0:2]
b_list
del b_list
list_of_letters = [letter for letter in "qwertyuiopasdfghjklzxcvbnm"]
print("LIST WITH LETTERS:",list_of_letters,sep='\n')
list_of_letters.clear()
print("EMPTY LIST:",list_of_letters,sep='\n')
list_of_letters = [letter for letter in "qwertyuiopasdfghjklzxcvbnm"]
print(list_of_letters)
list_of_letters.reverse()
answer = reversed(list_of_letters)
print(list_of_letters) # reversing list
answer = sorted(list_of_letters)
list_of_letters.sort(reverse=True) # sorting. Remember! sort() method accepts two argument and returns None
print(list_of_letters)
m_ind = list_of_letters.index('m') # find the first item whose value is equal to target
print(list_of_letters[m_ind])
list_of_letters.extend(list('1234567890')) # iterable extending
print(list_of_letters)
print("LIST SIZE:",len(list_of_letters)) # list length
a = 1
b = 0
a, b = b, a
example_tuple = (b, a)
a, b = example_tuple
print(a, b)
empty_tuple = ()
singleton = 'singleton',
separated_tuple = ("Hello",",","World","!") # separated_tuple = "Hello",",","World","!"
with_type_constructor_tuple = tuple("some_string_2")
# Important
tuple( ("Hello",",","World","!") ) # it works
tuple("Hello",",","World","!") # it doesn not work
print(empty_tuple)
print(singleton)
print(separated_tuple)
print(with_type_constructor_tuple)
a,b,c,d = separated_tuple # tuple unpacking
print(a,b,c,d)
a,b,c,d = d,c,a,b # tuple unpacking is not equal the following statement: a=d,b=c,c=a,d=b
print(a,b,c,d) # not ! World ! World
hello,_,_,_ = separated_tuple # syntax allows "skipping" not necessary variables
print(hello)
with_type_constructor_tuple[0]
Tuples are immutable sequences
with_type_constructor_tuple[0] = 'QQQQQQ' # 'tuple' object does not support item assignment
del with_type_constructor_tuple[0] # 'tuple' object doesn't support item deletion
del with_type_constructor_tuple # but you can delete a whole object
letter_tuple = tuple("qwertyuiopasdfghjklzxcvbnm")
print(letter_tuple)
letter_index = letter_tuple.index('o')
print(letter_tuple[letter_index])
print("TUPLE SIZE:",len(letter_tuple)) # list length
if 'a' in ['1','2','a']:
print("YES_list!")
if 'q' in ('q','w',0):
print("YES_tuple!")
print(1 in ['1','2','3'])
For Loops can be used for any iterable objects
list_of_letters = list("qwertyuiopasdfghjklzxcvbnm")
for i in range(len(list_of_letters)-1):
print(list_of_letters[i]+list_of_letters[i+1], end='\t')
print()
for letter in list_of_letters:
print(letter,end="\t")
print()
for number in tuple("1234567890"):
print(number,end="\t")
print(number,letter)
for letter in list_of_letters:
if letter in ['a','b','c']:
print(letter,end="\t")
else:
print('*',end="\t")
for letter in list_of_letters:
if letter in ['a','b','c']:
continue
print(letter,end="\t")
for number in tuple((1,2,34,5,6,7,8,9,0)):
if number%3==0:
break
print(number,end="\t")
for letter in 'qwertyuiop':
continue
else:
print("Else keyword allows executing code inside when For Loop is finised")
for letter in list_of_letters:
if letter!='q':
print(letter)
for i in '1234567890':
print(i,end="\t")
Enumerate allows to loop over iterable object and have an automatic counter.
for j,letter in enumerate(list_of_letters[:6]):
print(j,letter)
for j,letter in enumerate(list_of_letters):
if letter in tuple('milk'):
print("!"*90)
continue
# elif letter =='b':
# break
print(letter,j,sep='-itter_',end="\t")
for i in '1234567890':
print(i,end="\t")
print()
One more mutable sequence type. The range() function allows to generate iterable integer objects and always used for loops with specified executing number of times.
range(stop) -> range object
range(start, stop[, step]) -> range object
print(tuple(range(10)))
print(list(range(5,30,3)))
for i in range(17,8,-1):
print(i,end='\t')
if i%9==0:
print('\n',type(i),sep='')
for i in range(10):
for j in range(0,5):
print(i,'*',j,'=',i*j,end='\t')
print()
print()
for i in range(10):
for j in range(5,10):
print(i,'*',j,'=',i*j,end='\t')
print()
for i in range(len(list_of_letters)):
print(i,list_of_letters[i],end='\t',sep='<-')
list_of_letters_2 = list_of_letters.copy() # otherwise list_of_letters will be changed too
for i in range(len(list_of_letters_2)):
list_of_letters_2[i] = '*'
print(list_of_letters_2,list_of_letters,sep='\n')
print([i for i in range(0,101,10)])
print()
print([i for i in range(20) if i%3]) # if i%3 equals if i%3==1
print()
print([i if i%2==0 else "*" for i in range(20)])
print()
print([(i,i**2,i**3) for i in range(10)])
print()
print([[j for j in range(i)] for i in range(10)]) # nested
Your program receives a list of numbers in a single line as an input.
Group every pair of consecutive inputs into a tuple, put these tuples in a list and print it.
If input contains an odd amount of numbers, the last tuple has to have only a single number in it.
Example input:
11 9 20 8 3 7 4 6 5 5 4 9 1
Example output:
[('11', '9'), ('20', '8'), ('3', '7'), ('4', '6'), ('5', '5'), ('4', '9'), ('1',)]
input_data = input().split()
print(input_data)