Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Numpy-2

 Indexing and slicing in 2D:

import numpy as np
x=np.arange(20).reshape(4,5)
print(x)
print(x[2][2])
print(x[3][2])
print(x[3,0])
print('--------slicing -------')
print(x[0:2])
print('------------------')
print(x[0:2,0:3])
print('------------------')
print(x[1:4,1:4])
print('------------------')
x=np.arange(20)
print(x)
print('------------------')
print(x>10)
print('------------------')
print(x[x>10])

Output:


[[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] 12 17 15 --------slicing ------- [[0 1 2 3 4] [5 6 7 8 9]] ------------------ [[0 1 2] [5 6 7]] ------------------ [[ 6 7 8] [11 12 13] [16 17 18]] ------------------ [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] ------------------ [False False False False False False False False False False False True True True True True True True True True] ------------------ [11 12 13 14 15 16 17 18 19]

Operations in numpy :

import numpy as np
x=np.arange(10)
print(x)
print('----------------------------------')
y=np.arange(10,20)
print(y)
print('----------------------------------')
print(x+y)
print('----------------------------------')
print(x-y)
print('----------------------------------')
print(x*y)
print('----------------------------------')
print(x/y)
print('---------------------------------------------------')
print(x.max())
print('----------------------------------')
print(y.max())
print('----------------------------------')
print(np.sin(x))
print('----------------------------------')
print(np.cos(x))
print('----------------------------------')
print(np.tan(x))
print('----------------------------------')
print(np.log(y))

Output:


[0 1 2 3 4 5 6 7 8 9] -------------------------------------------------------------- [10 11 12 13 14 15 16 17 18 19] --------------------------------------------------- [10 12 14 16 18 20 22 24 26 28] --------------------------------------------------- [-10 -10 -10 -10 -10 -10 -10 -10 -10 -10] --------------------------------------------------- [ 0 11 24 39 56 75 96 119 144 171] --------------------------------------------------- [0. 0.09090909 0.16666667 0.23076923 0.28571429 0.33333333 0.375 0.41176471 0.44444444 0.47368421] --------------------------------------------------- 9 --------------------------------------------------- 19 --------------------------------------------------- [ 0. 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427 -0.2794155 0.6569866 0.98935825 0.41211849] --------------------------------------------------- [ 1. 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219 0.96017029 0.75390225 -0.14550003 -0.91113026] --------------------------------------------------- [ 0. 1.55740772 -2.18503986 -0.14254654 1.15782128 -3.38051501 -0.29100619 0.87144798 -6.79971146 -0.45231566] --------------------------------------------------- [2.30258509 2.39789527 2.48490665 2.56494936 2.63905733 2.7080502 2.77258872 2.83321334 2.89037176 2.94443898]

Operations in numpy 2D:

import numpy as np
x=np.arange(20).reshape(4,5)
print(x)
print('----------------------------')
y=np.arange(20,40).reshape(4,5)
print(y)
print('----------------------------')
print(x+y)
print('----------------------------')
print(x-y)
print('----------------------------')
print(x*y)
print('----------------------------')
print(x/y)
print('----------------------------')
print(x.max())
print('----------------------------')
print(y.max())

Output:


[[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] ---------------------------- [[20 21 22 23 24] [25 26 27 28 29] [30 31 32 33 34] [35 36 37 38 39]] ---------------------------- [[20 22 24 26 28] [30 32 34 36 38] [40 42 44 46 48] [50 52 54 56 58]] ---------------------------- [[-20 -20 -20 -20 -20] [-20 -20 -20 -20 -20] [-20 -20 -20 -20 -20] [-20 -20 -20 -20 -20]] ---------------------------- [[ 0 21 44 69 96] [125 156 189 224 261] [300 341 384 429 476] [525 576 629 684 741]] ---------------------------- [[0. 0.04761905 0.09090909 0.13043478 0.16666667] [0.2 0.23076923 0.25925926 0.28571429 0.31034483] [0.33333333 0.35483871 0.375 0.39393939 0.41176471] [0.42857143 0.44444444 0.45945946 0.47368421 0.48717949]] ---------------------------- 19 ---------------------------- 39

these are the basis of numpy hope you understand ,share your thoughts in comments sections

Comments

Popular Posts