Posts

Showing posts from March, 2022

PYTHON and variables?

 PYTON does not work with variables. PYTON works with: - name, - objects, - types, - values, - reference number. If we write: a = 7 then: - a is the name - a PYTHON object corresponding to the name a is created - 7 is value - the type is whole - the reference count is 0. if we write below a = 100 then: - remain the same name, - a new object associated with the name a is created, - the type is integer, - the reference count is 1 This explains the different addresses id (a) from the first assignment and from the second assignment. Programe is: a = 7 print(id(a)) a = 100 print(id(a)) Results are: ============== RESTART: /Users/ionivan/Documents/pointerspython.py ============= 4408295856 4408298832 If the sequence is considered: a = 25 b = a the names a and b correspond to the same object, so they will have the same address. Programul este: a = 25 b = a print("Adress a name is",id(a)) print("Adress b name is",id(b)) Rezultatele sunt: ============== RESTART: /Users/ioniv

Pointers in PYTHON?

 Memory programming is used in programming. A memory area is defined by: - associated variable name, - length as number of bytes, - address of the first byte, - content as a string of bits with contextual interpretation. The contents of the pointer variable are the address of a memory area. A variable is referred to when its address is loaded into a pointer variable. A pointer variable is redirected using its contents to reach the contents of the variable whose address it contains. The reference operator initializes the pointer variable. The pointer referral operator provides the contents of the memory area whose address it contains. And the PYTHON language natural works with pointer variables. PYTHON works differently and spectacular with pointers. (March 31, 2022)

Quadratic equation solutions programe

Programe is: # #  Quadratic equation solutions programe #  a*x**2 + b*x + c = 0 # import cmath answer = 'y' while answer == 'y' or answer == 'Y':     a = float(input(' coeficient a is:'))     b = float(input(' coeficient b is:'))     c = float(input(' coeficient c is:'))     gama = 2*a     eta = -b/gama     delta = cmath.sqrt(b*b - 4*a*c)/gama     x_1 = eta - delta     x_2 = eta + delta     print('The solution X1 is:', x_1)     print('The solution X2 is:', x_2)     answer = input(' If you want to continue, type y or Y: ') print(' Thank you for working with this program!') Results are: ============ RESTART: /Users/ionivan/Documents/QuadraticEcuation.py ============  coeficient a is:1  coeficient b is:1  coeficient c is:1 The solution X1 is: (-0.5-0.8660254037844386j) The solution X2 is: (-0.5+0.8660254037844386j)  If you want to continue, type y or Y: y  coeficient a is:1  coeficient b is:5  coeficient c is:

exp(A), A is a matrix

Programe is: # # Calculate A**n, A is a matrix,  n is an integer #                 A has maximum 15 rows and 15 columns # AA =  [[1,2,3,4],       [2,3,4,5],       [3,4,5,6],       [4,5,6,7]] AAA = [[2,2],       [2,2]] BB =  [[1,1,1,1],       [1,1,1,1],       [1,1,1,1],       [2,2,2,2]] NN_row = 4 NN_column = 4 def product_AB(A, B, N_row, N_column):     C =  [[1,1,1,1],       [1,1,1,1],       [1,1,1,1],       [2,2,2,2]]    # C = [[0]*N_row]*N_column     for i in range(N_row):         for j in range(N_column):             C[i][j]=0             for k in range(N_column):                 C[i][j] += A[i][k]*B[k][j]     return C CC = product_AB(AA, BB, NN_row, NN_column) print(CC) def power_matrixAn(A,N_row, n):     An = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],           [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],           [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],           [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],           [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],           [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],           [0,0,0,0,0,0,0,0,

PYTHON comments

 All programmers use program comments. Comments: - shows the significance of the variables, - clarification of the sequences, - state what restrictions must be observed, - specify the date when the program was written, - say who the author of the program is, - define the bibliography used, - helps in the maintenance process. Comments are written as follows: # short comment "" "on the start line of the comment and on the" "" end line of the comment. speed = [1, 2, 3, 4, 5] # speed list A_square = 25 # area of the square for i range (#): # gathers the elements of the X_list      sum + = X_list [i] "" " The program is written on March 30, 2022 The BELMAN KALABA algorithm is used The author is Ion IVAN "" " Including comments in programs is called self-documenting. (March 30, 2022)

lambda keyword usage in PYTHON programe

Programe is: # #       lambda option usage în PYTHON programe #       only one parameter restriction limits the structures #       geometry problems exemples #        A_circle = area of the circle #        L_cilcre = the length of the circle #        A_ square = area of the square #        P_square = square perimeter #        R = radius of the circle #        L = the side of the square of the triangle #        H = height #        P_triangle = the perimeter of the equilateral triangle #        A_triangle = area of the equilateral triangle #        V_cube = volume of the cube #        A_cube = total cube area #        A_sphere = area of the sphere #        V_sphere = volume of the sphere #        H_tetra  = height tetrahedron #        A_tetra = total area of the tetrahedron #        V_tetra = volume of the tetrahedron #        P_hexa = perimeter of the hexagon inscribed in a circle #        A_hexa = area of the hexagon inscribed in a circle #        PI = 3.14 # # r = 10 l = 10 A_square 

Comments in PYTHON

  Programs must contain comments. The comments explain the sequence of instructions. Comments help maintain the program. Comments are written on a line using # Comments are written on multiple lines using "" "and" "" Comments help the programmer who takes over a program from another programmer who has left the team. The comments clarify everything. Comment programs are the best. Comments are written anywhere. Comments precede instruction sequences. Comments are written after each instruction. Comments must be visible. (March 28, 2022)

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_

Formated print

PYTHON programe is: # #   Formated print # X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Y = [[1, 2, 3],      [4, 5, 6],      [7, 8, 9]] a = 7 b = 15 print(" a= %5d b= %5d" %(a, b)) nr = len(X) i = 0  while i < nr:     print("%5d" %(X[i]))     i = i +1 print('List was printed ', ' ') row_number = len(Y) column_number = len(Y[0]) i = 0  while i < row_number:     j = 0      while j < column_number:         print("%5d " %(Y[i][j]), end =" ")         j = j+1     print(" " )     i = i +1 print('List of list (matrix) was printed ', ' ')  Results are: Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. ============== RESTART: /Users/ionivan/Documents/FormatedPrint.py ==============  a=     7 b=    15     1     2     3     4     5     6     7     8    

Arithmetic, geometric, harmonic means calculation

# #    Mean_A is weighted arithmetic mean calculation. #    Mean_G is weighted geometric mean calculation. #    Mean_H is weighted harmonic average calculation. #    f[i] - weight data point x[i] #    x[i] - data point i in dataset #    Nr  - lenght dataset # Nr = int(input('Lenght dataset is:')) # # ********** Initialization list ************ # def Initialization_list(nr, string_mane):     termx = ['0']*nr     print(string_mane)     i = 0     while i < nr:         termx[i] = input('Term in dataset is:')         i = i + 1     return termx # # ********** Harmonic wighted mean calculation ************ # def Harmonic_mean(nr, termx, termf):     suma_xf = 0     suma_f = 0     i = 0     while i < nr:         suma_xf = suma_xf +  float(termf [i])/float(termx [i])         suma_f = suma_f + float(termf [i])         i = i + 1     mean_h =  suma_f / suma_xf     return mean_h # # ********** Geometric wighted mean calculation ************ # def Geometric_mean(nr, term

PYTHON tuple working

Programe is: # #  Tuple #    from operator import itemgetter Workers = [            ('POPESCU Gheorghe',   32,     3200, 'Assembly    '),            ('IONESCU Dumitru ',   40,     4100, 'Mechanics   '),            ('GRIGORECU Ion   ',   27,     2930, 'Tinware     '),            ('GRIGORIU Georgel',   37,     3935, 'Warehouseman')] print(' ******** Initial tuple *****  ') Worker_number = len(Workers) # Tuple number i = 0 ALFA  = ('Worker name ','Age', 'Salary', 'Work place  ') print( ALFA) while i < Worker_number:     print( Workers[i ])     i = i + 1 print('Workers number is:', Worker_number -1) i = 0 while i < Worker_number:     print( Workers[i ][0])     i = i + 1 i = 1 Total_salary = 0 while i < Worker_number:     Total_salary = Total_salary + int(Workers[i ][2])     i = i + 1 print('Total salary  is:', Total_salary) i = 0 while i < Worker_number:     p

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) print('Element number în A list  is:', Length_A) print('Element number în B list  is:', Length_B) print('First list is:

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[i] == 0):             C_zer = C_zer +1     return C_pos, C_neg, C_zer print ('********** List_elements_A **********') Count_positive, Count_negative, Count_zero = Count_list(List_elements_A) print ('Number positive elements ar

Generates M prime numbers

Programe is: # Generates M prime numbers, M > 1 M = int(input('Number of prime numbers  are:')) print ('Prime numbers list is:') print (1,  '  ') print (2,  '  ') print (3,  '  ') # MM = M  K = 1 N = 3 while K <= M:     A = N**0.5     B = int(A)     C = False     D = 2     while (C == False) and (D <= B):         if(N % D) == 0:             C = True         D = D + 1     if (C == False):         print ( N,  '  ')     N = N + 2     K = K + 1 print ('The list was successfully printed') Results are:  Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. ========= RESTART: /Users/ionivan/Documents/GenerareListaNumerePrime.py ======== Number of prime numbers  are:10 Prime numbers list is: 1    2    3    3    5    7    11    13    17    19    The list was successfully

N is a prime number?

 Programe PYTHON checks if the number N is a prime number.  The algorithm consists of dividing the number N by all successive numbers 1, 2, 3,, sqrt (N) as long as the rest of the divisions are nonzero. The PYTHON program is: # Checks if the number N is a prime number N = int(input('Number is:')) # A = sqrt(N) A = N**0.5 B = int(A) C = False D = 2 while (C == False) and (D <= B):     if(N % D) == 0:         C = True     D = D + 1 if (C == False):     print ('Number', N,  'prime') else:     print ('Number', N,  'is not prime') The results of the program are: Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. ================ RESTART: /Users/ionivan/Documents/NumarPrim.py ================ Number is:887 Number 887 prime Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang
Three values a, b and c are entered from the keyboard. The program shows several variables for setting the minimum and maximum value. The PYTHON program is: # Minimum and Maximum element choice between a, b, c a = input('First number:') b = input('Second number:') c = input('Third number:') minim = a if (minim > b):     minim = b if (minim > c):     minim = c print('Minimum from a, b,  c is:', minim) if(a < b):     if(a < c):         minim = a     else:         minim = c elif (b < c):     minim = b else:     minim = c print('Minimum from a, b,  c is:', minim) minim = a if (a < b) else b minim = minim if (minim < c) else c print('Minimum from a, b,  c is:', minim) minim = (a if(a<c) else c) if(a<b) else (b if(b<c) else c) print('Minimum from a, b,  c is:', minim) # Alegere element maxim dintre a, b, c maxim = a if (maxim < b):     maxim = b if (maxim < c):     maxim = c print('Maximum from a,

First PYTHON program

The first program written in PYTHON gathers two numbers entered from the keyboard and displays the result. The program instructions are: #    First program a = input('First number:') b = input('Second number:') c = int(a) + int(b) print('Result c is:', c) IDLE Shell 3.10.2 afișază: Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. ============== RESTART: /Users/ionivan/Documents/Primul program.py ============= First number:4 Second number:11 Result c is: 15 (March, 21, 2022) 

PYTHON Language Programming

PYTHON language programming is: - flexible, - portable, - easy, - free, - accessible,  - intuitive, - versatile, - universal,  - convenient, - ordinary,  - common. PYTHON programming language has taken over the best of all the programming languages before it. It is simple, efficient, dense and attractive. The input() statement is used to enter the data. The print() instruction is used to present the results. Variable names are constructed the same as all other programming languages: - the first character is required a letter, - the other characters are letters, numbers or underscores, - uppercase letters differ from lowercase letters, - any words other than the reserved words of the PYTHON language. - the length of the name does not exceed 31 characters. The string is preceded and followed by quotation marks. The string is preceded and followed by an apostrophe. Correct variable names: a i j temp Triangle surface_cercle  volume_cube Name_Person Age_Student Year2000 Wrong variable na