Wednesday, July 31, 2019

How to run a program in Python

pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / > cat first-python-program.py
#!/usr/local/bin/python/3.7

import sys
print (sys.platform)
print (2 ** 8)
print (2 ** 9)
print (2 ** 10)
print (3 ** 4)
x = 'Programming '
print (x * 12)
print()
pqr47.us.company.com: / >
pqr47.us.company.com: / > python3 first-python-program.py
linux2
256
512
1024
81
Programming Programming Programming Programming Programming Programming Programming Programming Programming Programming Programming Programming

pqr47.us.company.com: / >
pqr47.us.company.com: / > chmod 755 first-python-program.py
pqr47.us.company.com: / >
pqr47.us.company.com: / > ./first-python-program.py
linux2
256
512
1024
81
Programming Programming Programming Programming Programming Programming Programming Programming Programming Programming Programming Programming

pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / >



Greatest common denominator

Script

#!/usr/local/bin/python3.7

def gcd(n1, n2):
    minimum = n1 if n1 < n2 else n2
    largest_factor = 1
    for x in range(1, minimum + 1):
        if n1 % x == 0 and n2 % x ==0:
            largest_factor = x
    return largest_factor

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

minimum = num1 if num1 < num2 else num2

print(gcd(num1, num2))
print()


Execution

Enter the first number: 68
Enter the second number: 51
17


Enter the first number: 189
Enter the second number: 105
21






Decimal to Binary converter

Script

#!/usr/local/bin/python3.7

print()
inflow = int(input("Please enter an integer in the range 0 to 1023: "))

binary_string = ''

if 0 <= inflow < 1024:
    if inflow >= 512:
        binary_string = binary_string + '1'
        inflow = inflow % 512
    else:
        binary_string = binary_string + '0'

    if inflow >= 256:
        binary_string = binary_string + '1'
        inflow = inflow % 256
    else:
        binary_string = binary_string + '0'

    if inflow >= 128:
        binary_string = binary_string + '1'
        inflow = inflow % 128
    else:
        binary_string = binary_string + '0'

    if inflow >= 64:
        binary_string = binary_string + '1'
        inflow = inflow % 64
    else:
        binary_string = binary_string + '0'

    if inflow >= 32:
        binary_string = binary_string + '1'
        inflow = inflow % 32
    else:
        binary_string = binary_string + '0'

    if inflow >= 16:
        binary_string = binary_string + '1'
        inflow = inflow % 16
    else:
        binary_string = binary_string + '0'

    if inflow >= 8:
        binary_string = binary_string + '1'
        inflow = inflow % 8
    else:
        binary_string = binary_string + '0'

    if inflow >= 4:
        binary_string = binary_string + '1'
        inflow = inflow % 4
    else:
        binary_string = binary_string + '0'

    if inflow >= 2:
        binary_string = binary_string + '1'
        inflow = inflow % 2
    else:
        binary_string = binary_string + '0'

    binary_string = binary_string + str(inflow)

if binary_string != '':
    print()
    print("Binary conversion: ", binary_string)
else:
    print()
    print("Invalid input")
print()


Execution

Please enter an integer in the range 0 to 1023: 555

Binary conversion:  1000101011



Please enter an integer in the range 0 to 1023: 773
1100000101



Please enter an integer in the range 0 to 1023: 281

Binary conversion:  0100011001


Please enter an integer in the range 0 to 1023: -45

Invalid input






Factoring up to 25 --- using a for loop

Script

#!/usr/bin/loca//python3.7

MAXIMUM = 25

for n in range(1, MAXIMUM + 1):
    print(end = str(n) + ': ')
    for factor in range(1, n + 1):
        if n % factor == 0:
            print(factor, end = ' ')
    print()
print()


Execution

1: 1
2: 1 2
3: 1 3
4: 1 2 4
5: 1 5
6: 1 2 3 6
7: 1 7
8: 1 2 4 8
9: 1 3 9
10: 1 2 5 10
11: 1 11
12: 1 2 3 4 6 12
13: 1 13
14: 1 2 7 14
15: 1 3 5 15
16: 1 2 4 8 16
17: 1 17
18: 1 2 3 6 9 18
19: 1 19
20: 1 2 4 5 10 20
21: 1 3 7 21
22: 1 2 11 22
23: 1 23
24: 1 2 3 4 6 8 12 24
25: 1 5 25





Insert, Count, Remove, Append, Index, Reverse, Add and Pop from a list

Script

#!/usr/local/bin/python3.7

grocery_list = ['banana', 'apple', 'tomato', 'potato', 'cabbage', 'radish', 'cucumber', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

print()
print(grocery_list)

print()
print("Insert Spinach in position 3")
grocery_list.insert(3, 'spinach')

print(grocery_list)

print()
print("Display the number of occurrences of Radish and Ginger")
print("Entries for Radish: ", grocery_list.count('radish'), "     Entries for Ginger:" , grocery_list.count('ginger'))

grocery_list.remove('cucumber')

print()
print("Remove Cucumber from the list")
print(grocery_list)
print()

grocery_list.append('okra')

print("Append Okra to the list")
print(grocery_list)
print()

print("Index Capsicum in the list")
print(grocery_list.index('capsicum'))
print()

grocery_list.reverse()

print("Reverse the list")
print(grocery_list)
print()

print("Add Beetroot and Lady's Finger to the list")
x = ['beetroot', 'ladys-finger']
grocery_list.extend(x)

print(grocery_list)
print()

grocery_list.pop()
print("Pop out the last item in the list")

print(grocery_list)
print()

Execution

['banana', 'apple', 'tomato', 'potato', 'cabbage', 'radish', 'cucumber', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

Insert Spinach in position 3
['banana', 'apple', 'tomato', 'spinach', 'potato', 'cabbage', 'radish', 'cucumber', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

Display the number of occurrences of Radish and Ginger
Entries for Radish:  1      Entries for Ginger: 1

Remove Cucumber from the list
['banana', 'apple', 'tomato', 'spinach', 'potato', 'cabbage', 'radish', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

Append Okra to the list
['banana', 'apple', 'tomato', 'spinach', 'potato', 'cabbage', 'radish', 'capsicum', 'ginger', 'carrot', 'bean', 'onion', 'okra']

Index Capsicum in the list
7

Reverse the list
['okra', 'onion', 'bean', 'carrot', 'ginger', 'capsicum', 'radish', 'cabbage', 'potato', 'spinach', 'tomato', 'apple', 'banana']

Add Beetroot and Lady's Finger to the list
['okra', 'onion', 'bean', 'carrot', 'ginger', 'capsicum', 'radish', 'cabbage', 'potato', 'spinach', 'tomato', 'apple', 'banana', 'beetroot', 'ladys-finger']

Pop out the last item in the list
['okra', 'onion', 'bean', 'carrot', 'ginger', 'capsicum', 'radish', 'cabbage', 'potato', 'spinach', 'tomato', 'apple', 'banana', 'beetroot']







Factoring up to 25 --- using a while loop

Script

#!/usr/bin/local/python3.7

MAXIMUM = 25
n = 1

while n <= MAXIMUM:
    factor = 1
    print(end = str(n) + ': ')
    while factor <= n:
        if n % factor == 0:
            print(factor, end =' ')
        factor = factor + 1
    print()
    n = n + 1

print()


Execution

1: 1
2: 1 2
3: 1 3
4: 1 2 4
5: 1 5
6: 1 2 3 6
7: 1 7
8: 1 2 4 8
9: 1 3 9
10: 1 2 5 10
11: 1 11
12: 1 2 3 4 6 12
13: 1 13
14: 1 2 7 14
15: 1 3 5 15
16: 1 2 4 8 16
17: 1 17
18: 1 2 3 6 9 18
19: 1 19
20: 1 2 4 5 10 20
21: 1 3 7 21
22: 1 2 11 22
23: 1 23
24: 1 2 3 4 6 8 12 24
25: 1 5 25





Average of five numbers

Script

#!/usr/bin/local/python3.7

count = 0
sum = 0

print("Please input five numbers when prompted ")
print()
while count < 5:
    value = float(input("Enter number: "))
    if value < 0:
        print("Negative numbers not accepted.")
        break
    count = count + 1
    sum = sum + value
else:
    print("Average of these five numbers = ", sum/5)
print()


Execution

Please input five numbers when prompted

Enter number: 73
Enter number: 91
Enter number: 53
Enter number: 59
Enter number: 37
Average of these five numbers =  62.6








Simpler method: Decimal to Binary conversion

Script

#!/usr/local/bin/python3.7

print()
inflow = int(input("Please enter a number between 0 and 1023: "))
print()

binary_string = ''

if 0 <= inflow <= 1024:
    binary_string = binary_string + str(inflow//512)
    inflow = inflow % 512
    binary_string = binary_string + str(inflow//256)
    inflow = inflow % 256
    binary_string = binary_string +str(inflow//128)
    inflow = inflow % 128
    binary_string = binary_string + str(inflow//64)
    inflow = inflow % 64
    binary_string = binary_string + str(inflow//32)
    inflow = inflow % 32
    binary_string = binary_string + str(inflow//16)
    inflow = inflow % 16
    binary_string = binary_string + str(inflow//8)
    inflow = inflow %8
    binary_string = binary_string + str(inflow//4)
    inflow = inflow % 4
    binary_string = binary_string + str(inflow//2)
    inflow = inflow % 2
    binary_string = binary_string + str(inflow)

if binary_string != '':
    print(binary_string)
else:
    print("Invalid data.")
print()


Execution

Please enter a number between 0 and 1023: 649

1010001001


Please enter a number between 0 and 1023: 111

0001101111


Please enter a number between 0 and 1023: 971

1111001011








Maximum of five numbers

Script

#!/usr/bin/local/python3.7

print()
print("Please enter four integer values.")

num1 = int(input("Enter Number 1:  "))
num2 = int(input("Enter Number 2:  "))
num3 = int(input("Enter Number 3:  "))
num4 = int(input("Enter Number 4:  "))
num5 = int(input("Enter Number 5:  "))

maximum = num1
if num2 >= maximum:
    maximum = num2
if num3 >= maximum:
    maximum = num3
if num4 >= maximum:
    maximum = num4
if num5 >= maximum:
    maximum = num5

print("The maximum number is: ", maximum)
print()
print()


Execution


Please enter four integer values.
Enter Number 1:  54
Enter Number 2:  32
Enter Number 3:  71
Enter Number 4:  49
Enter Number 5:  37
The maximum number is:  71


Please enter four integer values.
Enter Number 1:  91
Enter Number 2:  59
Enter Number 3:  73
Enter Number 4:  27
Enter Number 5:  97
The maximum number is:  97





Prime numbers

Script

#!/usr/local/bin/python3.7

print()

maximum = int(input("Enter the maximum number you are checking for in the prime list: "))

for x in range(1, maximum + 1):
    for trailfactor in range(2, x):
        if x % trailfactor == 0:
            break
    else:
            print(x, end = ' ')
print()
print()


Execution


Enter the maximum number you are checking for in the prime list: 73
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73






Star tree - Method 3

Script

#!/usr/local/bin/python3.7

print()
height = int(input("Please enter the height of the tree: "))

for row in range(height):
    for count in range(height - row):
        print(end = ' ')
    for count in range(2 * row - 1):
        print(end = '*')
    print()
print()


Execution


Please enter the height of the tree: 15

              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
   ***********************
  *************************
 ***************************





Star tree - Method 2

Script

#!/usr/local/bin/python3.7

print()
height = int(input("Please enter the height of the tree: "))

row = 0

while row < height:
    count = 0
    while count < height - row:
        print(end = ' ')
        count = count + 1
    count = 0
    while count < 2 * row + 1:
        print(end = '*')
        count = count + 1
    print()
    row = row + 1
print()
print()


Execution


Please enter the height of the tree: 15
               *
              ***
             *****
            *******
           *********
          ***********
         *************
        ***************
       *****************
      *******************
     *********************
    ***********************
   *************************
  ***************************
 *****************************






Multiplication tables in a neat format

Script

#!/usr/local/bin/python3.7

print()
size = int(input("Please enter the size of the table: "))
print()

print("   ", end='')
for column in range(1, size + 1):
    print('{0:4}'.format(column), end='')
print()

print("    +", end='')
for column in range(1, size + 1):
    print('----', end='')
print()

for row in range(1, size + 1):
    print('{0:3} |'.format(row), end='')
    for column in range(1, size + 1):
        product = row * column
        print('{0:4}'.format(product), end='')
    print()

print()
print()


Execution

Please enter the size of the table: 15

      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
    +------------------------------------------------------------
  1 |   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  2 |   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30
  3 |   3   6   9  12  15  18  21  24  27  30  33  36  39  42  45
  4 |   4   8  12  16  20  24  28  32  36  40  44  48  52  56  60
  5 |   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75
  6 |   6  12  18  24  30  36  42  48  54  60  66  72  78  84  90
  7 |   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105
  8 |   8  16  24  32  40  48  56  64  72  80  88  96 104 112 120
  9 |   9  18  27  36  45  54  63  72  81  90  99 108 117 126 135
 10 |  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150
 11 |  11  22  33  44  55  66  77  88  99 110 121 132 143 154 165
 12 |  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180
 13 |  13  26  39  52  65  78  91 104 117 130 143 156 169 182 195
 14 |  14  28  42  56  70  84  98 112 126 140 154 168 182 196 210
 15 |  15  30  45  60  75  90 105 120 135 150 165 180 195 210 225


Please enter the size of the table: 20

      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
    +--------------------------------------------------------------------------------
  1 |   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
  2 |   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40
  3 |   3   6   9  12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60
  4 |   4   8  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80
  5 |   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100
  6 |   6  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 102 108 114 120
  7 |   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119 126 133 140
  8 |   8  16  24  32  40  48  56  64  72  80  88  96 104 112 120 128 136 144 152 160
  9 |   9  18  27  36  45  54  63  72  81  90  99 108 117 126 135 144 153 162 171 180
 10 |  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190 200
 11 |  11  22  33  44  55  66  77  88  99 110 121 132 143 154 165 176 187 198 209 220
 12 |  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240
 13 |  13  26  39  52  65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260
 14 |  14  28  42  56  70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280
 15 |  15  30  45  60  75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300
 16 |  16  32  48  64  80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320
 17 |  17  34  51  68  85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340
 18 |  18  36  54  72  90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360
 19 |  19  38  57  76  95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380
 20 |  20  40  60  80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400





Tuesday, July 30, 2019

Simple math

Script

#!/usr/local/bin/python3.1

print("73 + 61 = ", end="")
print(73 + 61)
print("44 - 17 = ", end="")
print(44 - 17)
print("100/20 = ", end="")
print(100/20)
print("100/27 = ", end="")
print(100/27)
print("100.0/27 = ", end="")
print(100.0 / 27)
print("6 times 25 = ", end="")
print(6 * 25)
print("2 to the power 8 = ", end="")
print(2 ** 8)
print("2 to the power 32 = ", end="")
print(2 ** 32)
print("2 to the power 128 = ", end="")
print(2 ** 128)
print("37 modulus 8 = ", end="")
print(37 % 8)
print("-5 to the power of 4 = ", end="")
print(-5 **4)
print("-5 to the power of -4 = ", end="")
print(-5 ** -4)

Execution

73 + 61 = 134
44 - 17 = 27
100/20 = 5.0
100/27 = 3.7037037037
100.0/27 = 3.7037037037
6 times 25 = 150
2 to the power 8 = 256
2 to the power 32 = 4294967296
2 to the power 128 = 340282366920938463463374607431768211456
37 modulus 8 = 5
-5 to the power of 4 = -625
-5 to the power of -4 = -0.0016

Draw a tree of required height

Script 

#!/usr/local/bin/python3.7

def tree(height):

    """

    Draws a tree of a given height.

    """

    row = 0

    while row < height:

        count = 0

        while count < height - row:

            print(end = " ")

            count = count + 1

        count = 0

        while count < (2*row + 1):

            print(end = "*")

            count = count + 1

        print ()

        row = row + 1


def main():

    """ Allows users to draw trees of various heights """

    height = int(input("Enter the height of the tree: "))

    tree(height)


main()


Execution


Enter the height of the tree: 25

                         *

                        ***

                       *****

                      *******

                     *********

                    ***********

                   *************

                  ***************

                 *****************

                *******************

               *********************

              ***********************

             *************************

            ***************************

           *****************************

          *******************************

         *********************************

        ***********************************

       *************************************

      ***************************************

     *****************************************

    *******************************************

   *********************************************

  ***********************************************

 *************************************************

















Count characters, digits, uppercase and lowercase in a string

Script

#!/usr/local/bin/python3

print()
string1 = "Rahul Dravid scored 233 and 72 not out to win the 2003-04 Adelaide Test for India by 4 wickets."
digitCount = 0
alphabetCount = 0
uppercaseCount = 0
lowercaseCount = 0

for x in range(0, len(string1)):
    char = string1[x]
    if (char.isdigit()):
        digitCount += 1
    elif (char.isalpha()):
        alphabetCount += 1
        if (char.upper() == char):
            uppercaseCount += 1
        else:
            lowercaseCount += 1

print("Original string: ", string1)
print("Number of digits = ", digitCount)
print("Number of alphabets = ", alphabetCount)
print("Number of uppercase characters = ", uppercaseCount)
print("Number of lowercase characters = ", lowercaseCount)
print()


Execution

Original string:  Rahul Dravid scored 233 and 72 not out to win the 2003-04 Adelaide Test for India by 4 wickets.
Number of digits =  12
Number of alphabets =  63
Number of uppercase characters =  5
Number of lowercase characters =  58





Greater than or less than 100

Script

#!/usr/local/bin/python3.7

print()
value = int(input("Please enter an integer in the range 0 to 100: "))

print()

if value >= 0:
    if value <= 100:
        print(value, "is between 0 and 100 and is in the acceptable range")
    else:
        print(value, "is greater than 100 and outside the acceptable range")
else:
    print(value, "is too small")

print()
print("Done")
print()


Execution

Please enter an integer in the range 0 to 100: 69

69 is between 0 and 100 and is in the acceptable range

Done



Please enter an integer in the range 0 to 100: -34

-34 is too small

Done



Please enter an integer in the range 0 to 100: 529

529 is greater than 100 and outside the acceptable range

Done





Dividend by divisor

Script

#!/usr/local/bin/python3.7

print()
dividend = int(input("Please enter the number to be divided: "))
divisor = int(input("Please enter the number to perform the division: "))

print()
if divisor != 0:
    print(dividend, "/", divisor, " = ", dividend/divisor)
else:
    print("Division by zero is not allowed")
print()


Execution

Please enter the number to be divided: 37
Please enter the number to perform the division: 5

37 / 5  =  7.4


Please enter the number to be divided: 73
Please enter the number to perform the division: 3

73 / 3  =  24.333333333333332


Please enter the number to be divided: 97
Please enter the number to perform the division: 0

Division by zero is not allowed




Print input numbers in five-digit format

Script

#!/usr/local/bin/python3.7

# The program treats all numbers less than 0 as 0, and all numbers greater than 99999 as 99999

print()
number = int(input("Please enter an integer between 0 and 99999: "))

if number < 0:
    number = 0
if number > 99999:
    number = 99999

print()
print(end = "--->   ")

digit = number //10000
print(digit, end = "")
number = number % 10000

digit = number // 1000
print(digit, end = "")
number = number % 1000

digit = number // 100
print(digit, end = "")
number = number % 100

digit = number // 10
print(digit, end = "")
number = number % 10

print(number, end = "")
print(end = "   <---")
print()
print()


Execution


Please enter an integer between 0 and 99999: 5

--->   00005   <---



Please enter an integer between 0 and 99999: 763

--->   00763   <---



Please enter an integer between 0 and 99999: 56231

--->   56231   <---



Please enter an integer between 0 and 99999: 654321

--->   99999   <---




Count from 1 to 37 using the def functionality

 Script

#!/usr/local/bin/python3.7


def count_to_n(n):

    for i in range(1, n + 1):

        print(i, end = " ")

    print()

for i in range(1,37):

    count_to_n(i)


Execution

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10 11

1 2 3 4 5 6 7 8 9 10 11 12

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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

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

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

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

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

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

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

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

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

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

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

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








Time to add up the first100000000 numbers

 Script

#!/usr/local/bin/python3.7

from time import perf_counter


sum = 0

start = perf_counter()

for n  in range(1, 100000000):

    sum = sum + n

elapsed = perf_counter() - start

print("Sum: ", sum, "           Time in seconds: ", elapsed)


Execution

Sum:  4999999950000000            Time in seconds:  11.580363074









Print lists --- manipulate positions

Script

#!/usr/local/bin/python3.4

list = [ 'VVSL', 281 , 99.94, 'Kumble', 15921 ]
smalllist = [13288, 'Dravid']

print(list)                    # Prints complete list
print(list[0])                 # Prints the first element of the list
print(list[1])                 # Prints the second element of the list
print(list[2])                 # Prints the third element of the list
print(list[1:3])               # Prints the elements starting from 2nd till 3rd
print(list[4:])                # Prints the elements starting from fourth element
print(smalllist * 3)           # Prints the small list three times
print(list + smalllist)        # Prints the concatenated lists


Execution

['VVSL', 281, 99.94, 'Kumble', 15921]
VVSL
281
99.94
[281, 99.94]
[15921]
[13288, 'Dravid', 13288, 'Dravid', 13288, 'Dravid']
['VVSL', 281, 99.94, 'Kumble', 15921, 13288, 'Dravid']



List all possible IPs in a Subnetwork

 Script

#!/usr/bin/env python

# python cidr.py <Network-address>/<Subnet mask in short notation>


import sys, struct, socket


(ip, cidr) = sys.argv[1].split('/')

cidr = int(cidr)

host_bits = 32 - cidr

i = struct.unpack('>I', socket.inet_aton(ip))[0] # note the lastianness

first = (i >> host_bits) << host_bits # clear the host bits

last = first | ((1 << host_bits) - 1)


# excludes the first and last address in the subnet

for i in range(first, last):

    print(socket.inet_ntoa(struct.pack('>I',i)))


Execution

python3 ip-list-all-IPs-in-the-subnetwork.py 10.133.72.0/28 | tail -n +2 | cat -n | awk '{print $1 ": " $2}'

1: 10.133.72.1

2: 10.133.72.2

3: 10.133.72.3

4: 10.133.72.4

5: 10.133.72.5

6: 10.133.72.6

7: 10.133.72.7

8: 10.133.72.8

9: 10.133.72.9

10: 10.133.72.10

11: 10.133.72.11

12: 10.133.72.12

13: 10.133.72.13

14: 10.133.72.14



python3 ip-list-all-IPs-in-the-subnetwork.py 10.133.72.0/27 | tail -n +2 | cat -n | awk '{print $1 ": " $2}'

1: 10.133.72.1

2: 10.133.72.2

3: 10.133.72.3

4: 10.133.72.4

5: 10.133.72.5

6: 10.133.72.6

7: 10.133.72.7

8: 10.133.72.8

9: 10.133.72.9

10: 10.133.72.10

11: 10.133.72.11

12: 10.133.72.12

13: 10.133.72.13

14: 10.133.72.14

15: 10.133.72.15

16: 10.133.72.16

17: 10.133.72.17

18: 10.133.72.18

19: 10.133.72.19

20: 10.133.72.20

21: 10.133.72.21

22: 10.133.72.22

23: 10.133.72.23

24: 10.133.72.24

25: 10.133.72.25

26: 10.133.72.26

27: 10.133.72.27

28: 10.133.72.28

29: 10.133.72.29

30: 10.133.72.30












Enhanced time seconds converter in HH:MM:SS format

Script

#!/usr/local/bin/python3.7

seconds = int(input('Please enter the number of seconds: '))

hours = seconds // 3600

seconds = seconds % 3600

minutes = seconds // 60

seconds = seconds % 60

print(hours, ":", sep = "", end = "")

tens = minutes // 10

ones = minutes % 10

print(tens, ones, ":", sep = "", end = "")

tens = seconds // 10

ones = seconds % 10

print(tens, ones, sep = "")


Execution

Please enter the number of seconds: 5321
1:28:41


Please enter the number of seconds: 9857
2:44:17








Count to n --- with variable function

Script

#!/usr/local/bin/python3.7

def count_to_n(n):
    for x in range(1, n + 1):
        print(x, end = '  ')
    print()

for x in range(1, 26):
    count_to_n(x)
print()


Execution

1
1  2
1  2  3
1  2  3  4
1  2  3  4  5
1  2  3  4  5  6
1  2  3  4  5  6  7
1  2  3  4  5  6  7  8
1  2  3  4  5  6  7  8  9
1  2  3  4  5  6  7  8  9  10
1  2  3  4  5  6  7  8  9  10  11
1  2  3  4  5  6  7  8  9  10  11  12
1  2  3  4  5  6  7  8  9  10  11  12  13
1  2  3  4  5  6  7  8  9  10  11  12  13  14
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24
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






Count to 25 --- function

Script

#!/usr/local/bin/python3.7

def count_to_25():
    for x in range(1,26):
        print(x, end = '  ')
    print()


print("Counting up to 25.....")
count_to_25()
print("Counting up to 25 again.....")
count_to_25()


Execution

Counting up to 25.....
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
Counting up to 25 again.....
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





Monday, July 29, 2019

Centigrade to Fahrenheit conversion and vice versa

Script

#!/usr/local/bin/python3.1

celsius = float(input("Enter the degrees in Celsius: "))

fahrenheit = 9.0/5.0 * celsius + 32

print ("The temperature in Fahrenheit is: ", fahrenheit)

print ()
print ()

fahrenheit = float(input("Now enter the degrees in Fahrenheit: "))

celsius = (fahrenheit - 32) * 5.0/9.0

print ("The temperature in Celsius is: ", celsius)



Execution

Enter the degrees in Celsius: 100
The temperature in Fahrenheit is:  212.0


Now enter the degrees in Fahrenheit: 212
The temperature in Celsius is:  100.0





Enter the degrees in Celsius: 125.48
The temperature in Fahrenheit is:  257.864


Now enter the degrees in Fahrenheit: 300
The temperature in Celsius is:  148.888888889





Listing files in a directory: ls -altr

Script

#!/usr/local/bin/python3.5

import subprocess

subprocess.call(["ls", "-altr"])


Execution

total 128
-rwxr-xr-x.  1 root root   358 Feb  8 17:57 temperature-converter.py
-rwxr-xr-x.  1 root root  4035 Feb  8 18:20 k.py
-rwxr-xr-x.  1 root root  3437 Feb  9 06:30 sniff.py
-rwxr-xr-x.  1 root root   130 Feb  9 07:45 continue-example.py
dr-xr-xr-x. 23 root root  4096 Feb 17 05:06 ..
-rwxr-xr-x.  1 root root   146 Feb 17 05:22 a1
-rwxr-xr-x.  1 root root   250 Feb 17 06:50 fib
-rwxr-xr-x.  1 root root   195 Feb 17 06:51 alphabets
-rwxr-xr-x.  1 root root   250 Feb 17 06:55 prime-numbers
-rwxr-xr-x.  1 root root   229 Feb 17 07:06 if-else-then
-rwxr-xr-x.  1 root root   151 Feb 17 07:07 if-else-break
-rwxr-xr-x.  1 root root   173 Feb 17 07:25 for-loop
-rwxr-xr-x.  1 root root    89 Feb 17 17:27 whilingaway
-rwxr-xr-x.  1 root root   112 Feb 17 18:37 x
-rwxr-xr-x.  1 root root    87 Feb 18 05:26 hello.py
-rwxr-xr-x.  1 root root   377 Feb 19 20:53 inch-to-cm-using-functions.py
-rw-r--r--.  1 root root 12288 Feb 19 21:01 .inch-to-cm-using-functions.py.swp
-rwxr-xr-x.  1 root root   485 Feb 21 01:59 bowler-contribution.py
-rwxr-xr-x.  1 root root   348 Feb 21 04:18 yes-no-may-be.py
-rwxr-xr-x.  1 root root   778 Feb 21 04:52 local-and-global-variables-for-functions.py
-rwxr-xr-x.  1 root root   307 Feb 21 07:10 playing-with-functions.py
-rwxr-xr-x.  1 root root   158 Feb 21 22:29 one-to-25-nested-triangle.py
-rwxr-xr-x.  1 root root   366 Feb 22 03:26 christmas-tree.py
lrwxrwxrwx.  1 root root    30 Feb 25 20:46 localtime -> /usr/share/zoneinfo/US/Pacific
-rwxr-xr-x.  1 root root   492 Feb 25 21:00 timer.py
-rwxr-xr-x.  1 root root   711 Feb 25 22:01 extendtimer.py
-rwxr-xr-x.  1 root root   377 Feb 26 04:58 most-common-word-in-a-file.py
-rw-r--r--.  1 root root  6533 Feb 26 05:03 cricket-article.txt
-rwxr-xr-x.  1 root root    80 Feb 26 07:02 list-files.py
drwxr-xr-x.  2 root root  4096 Feb 26 07:02 .