Posts

Showing posts with the label len()

Nested loop in PYTHON

  Programe is: (March 28, 2022) # #    for loops in PYTHON #    for i range(n) i =0, 1, 2, 3, ..., n-1 #    for i range(k,n) i=k, k+1, k+2, ..., n-1 #    for i reange(k,n,r) i =k,k+r, k+2r,k+3r,..., k+xr and k+xr<n for i in range(10):     print(i) nr = int(input('Loops numbers is: ')) sum = 0 for i in range(nr):     sum += i print('Sums 0, 1, 2, .., ', nr, 'is: ', sum) # #  Odd numbers sum , i < n # n = nr = int(input('Numbers is: ')) for i in range(1, n, 2):     print(i) # #   for loops în list # cars_A = ["BMW", "FORD", "AUDI", "DACIA",  "LAMBORGINI"] cars_B = ["PORSCHE", "TATA", "VOLVO", "SAAB", "PEUGEOT"] print( '\n List cars_A is: ') for i in cars_A:     print(i, end = ' ') print( '\n List cars_B is: ') for i in cars_B:     print(i, end = ' ') print( '\n List cars_A + cars_B is: ') for i in (cars_A + cars_...

Lists operators

# #        String and lists operations # #        len() - components number list #         +    -  concatenate list operator #        set() - list conversion în set #          &   -  intersection operator in setss String = 'This is a string' Word_list_A = ['AUDI ', 'FORD ', 'DACIA ', 'TATA', 'RENAULT ',                    'CITROEN', 'LAMBORGHINI ', 'FERRARI' , 'VOLKSWAGEN',                  'ASTON MARTIN' 'BUICK ', 'ALFA ROMEO ',  'CRYSLER ',                 'BMW'] Word_list_B = ['LADA', 'SKODA', 'VOLGA', 'TATRA', 'PEUGEOT', 'SAAB',                    'VOLVO', 'TOYOTA', 'HYUNDAI', 'SUBARU'] Length_A = len(Word_list_A) Length_B = len(Word_list_B) pri...

Count the positive elements in a list

PYTHON program is: # #    Count the positive elements, the zero elements, #    and the negative elements in a list. #    A function is constructed that returns three values. #        return a, b, c #        x, y, z = Count_list(List) is x,y,z=a,b,c # Count_positive = 0 Count_negative = 0 Count_zero = 0 List_elements_A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List_elements_B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] List_elements_C = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10] List_elements_D = [1, 2, 3, 0, 0, 0, 0, -1, -2, -3] def Count_list(List):     C_pos = 0     C_neg = 0     C_zer = 0     No_elem = len(List)     for i in range(No_elem):         if(List[i] > 0):             C_pos = C_pos +1         if(List[i] < 0):             C_neg = C_neg +1         if(List...