├── ASCII value ├── LCM of a number ├── addition of two matrices ├── addition of two numbers ├── concatenate two lists ├── count nueesent togethermberof digits pr ├── multiplication of matrices ├── odd or even ├── prime or not ├── python program to find armstrong number between certain numbers ├── reverse a number ├── shuffle a deck of cards ├── sort words in ascendinng order ├── swapping of two numbers └── to merge two dictionaries /ASCII value: -------------------------------------------------------------------------------- 1 | 2 | 3 | c = input("enter a character") 4 | print("The ASCII value of '" + c + "' is", ord(c)) 5 | -------------------------------------------------------------------------------- /LCM of a number: -------------------------------------------------------------------------------- 1 | 2 | 3 | def compute_lcm(x, y): 4 | 5 | # choose the greater number 6 | if x > y: 7 | greater = x 8 | else: 9 | greater = y 10 | 11 | while(True): 12 | if((greater % x == 0) and (greater % y == 0)): 13 | lcm = greater 14 | break 15 | greater += 1 16 | 17 | return lcm 18 | 19 | num1 = input("enter value1:") 20 | num2 = input("enter value2:") 21 | 22 | print("The L.C.M. is", compute_lcm(num1, num2)) 23 | -------------------------------------------------------------------------------- /addition of two matrices: -------------------------------------------------------------------------------- 1 | # Program to add two matrices using nested loop 2 | 3 | X = [[12,7,3], 4 | [4 ,5,6], 5 | [7 ,8,9]] 6 | 7 | Y = [[5,8,1], 8 | [6,7,3], 9 | [4,5,9]] 10 | 11 | result = [[0,0,0], 12 | [0,0,0], 13 | [0,0,0]] 14 | 15 | # iterate through rows 16 | for i in range(len(X)): 17 | # iterate through columns 18 | for j in range(len(X[0])): 19 | result[i][j] = X[i][j] + Y[i][j] 20 | 21 | for r in result: 22 | print(r) 23 | -------------------------------------------------------------------------------- /addition of two numbers: -------------------------------------------------------------------------------- 1 | print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: ')))) 2 | -------------------------------------------------------------------------------- /concatenate two lists: -------------------------------------------------------------------------------- 1 | list_1 = [1, 'a'] 2 | list_2 = [3, 4, 5] 3 | 4 | list_joined = list_1 + list_2 5 | print(list_joined) 6 | -------------------------------------------------------------------------------- /count nueesent togethermberof digits pr: -------------------------------------------------------------------------------- 1 | num = 3452 2 | count = 0 3 | 4 | while num != 0: 5 | num //= 10 6 | count += 1 7 | 8 | print("Number of digits: " + str(count)) 9 | -------------------------------------------------------------------------------- /multiplication of matrices: -------------------------------------------------------------------------------- 1 | # Program to multiply two matrices using nested loops 2 | 3 | # 3x3 matrix 4 | X = [[12,7,3], 5 | [4 ,5,6], 6 | [7 ,8,9]] 7 | # 3x4 matrix 8 | Y = [[5,8,1,2], 9 | [6,7,3,0], 10 | [4,5,9,1]] 11 | # result is 3x4 12 | result = [[0,0,0,0], 13 | [0,0,0,0], 14 | [0,0,0,0]] 15 | 16 | # iterate through rows of X 17 | for i in range(len(X)): 18 | # iterate through columns of Y 19 | for j in range(len(Y[0])): 20 | # iterate through rows of Y 21 | for k in range(len(Y)): 22 | result[i][j] += X[i][k] * Y[k][j] 23 | 24 | for r in result: 25 | print(r) 26 | -------------------------------------------------------------------------------- /odd or even: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a number: ")) 2 | if (num % 2) == 0: 3 | print("{0} is Even".format(num)) 4 | else: 5 | print("{0} is Odd".format(num)) 6 | -------------------------------------------------------------------------------- /prime or not: -------------------------------------------------------------------------------- 1 | num = 11 2 | # If given number is greater than 1 3 | if num > 1: 4 | # Iterate from 2 to n // 2 5 | for i in range(2, (num//2)+1): 6 | # If num is divisible by any number between 7 | # 2 and n / 2, it is not prime 8 | if (num % i) == 0: 9 | print(num, "is not a prime number") 10 | break 11 | else: 12 | print(num, "is a prime number") 13 | else: 14 | print(num, "is not a prime number") 15 | -------------------------------------------------------------------------------- /python program to find armstrong number between certain numbers: -------------------------------------------------------------------------------- 1 | 2 | 3 | lower = 100 4 | upper = 2000 5 | 6 | for num in range(lower, upper + 1): 7 | 8 | # order of number 9 | order = len(str(num)) 10 | 11 | # initialize sum 12 | sum = 0 13 | 14 | temp = num 15 | while temp > 0: 16 | digit = temp % 10 17 | sum += digit ** order 18 | temp //= 10 19 | 20 | if num == sum: 21 | print(num) 22 | -------------------------------------------------------------------------------- /reverse a number: -------------------------------------------------------------------------------- 1 | num = 1234 2 | reversed_num = 0 3 | 4 | while num != 0: 5 | digit = num % 10 6 | reversed_num = reversed_num * 10 + digit 7 | num //= 10 8 | 9 | print("Reversed Number: " + str(reversed_num)) 10 | -------------------------------------------------------------------------------- /shuffle a deck of cards: -------------------------------------------------------------------------------- 1 | # Python program to shuffle a deck of card 2 | 3 | # importing modules 4 | import itertools, random 5 | 6 | # make a deck of cards 7 | deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club'])) 8 | 9 | # shuffle the cards 10 | random.shuffle(deck) 11 | 12 | # draw five cards 13 | print("You got:") 14 | for i in range(5): 15 | print(deck[i][0], "of", deck[i][1]) 16 | -------------------------------------------------------------------------------- /sort words in ascendinng order: -------------------------------------------------------------------------------- 1 | # Program to sort alphabetically the words form a string provided by the user 2 | 3 | my_str = "Hello this Is an Example With cased letters" 4 | 5 | # To take input from the user 6 | #my_str = input("Enter a string: ") 7 | 8 | # breakdown the string into a list of words 9 | words = [word.lower() for word in my_str.split()] 10 | 11 | # sort the list 12 | words.sort() 13 | 14 | # display the sorted words 15 | 16 | print("The sorted words are:") 17 | for word in words: 18 | print(word) 19 | -------------------------------------------------------------------------------- /swapping of two numbers: -------------------------------------------------------------------------------- 1 | x = input('Enter value of x: ') 2 | y = input('Enter value of y: ') 3 | 4 | # create a temporary variable and swap the values 5 | temp = x 6 | x = y 7 | y = temp 8 | 9 | print('The value of x after swapping: {}'.format(x)) 10 | print('The value of y after swapping: {}'.format(y)) 11 | -------------------------------------------------------------------------------- /to merge two dictionaries: -------------------------------------------------------------------------------- 1 | dict_1 = {1: 'a', 2: 'b'} 2 | dict_2 = {2: 'c', 4: 'd'} 3 | 4 | print(dict_1 | dict_2) 5 | --------------------------------------------------------------------------------