Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 16 of 854
Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50
When it is required to print all the elements which can’t be divided by 2 or 3 and lie between 1 and 50, the constraints are mentioned in the form of a ‘while’ loop and ‘if’ condition.Below is a demonstration of the same −Exampleprint("Integers not divisible by 2 and 3, that lie between 1 and 50 are : ") n = 1 while n
Read MorePython Program to Read a Number n And Print the Series "1+2+…..+n= "
When it is required to display the sum all the natural numbers within a given range, a method can be defined that uses a loop to iterate over the elements, and returns the sum of these numbers as output.Below is a demonstration of the same −Exampledef sum_natural_nums(val): my_sum = 0 for i in range(1, val + 1): my_sum += i * (i + 1) / 2 return my_sum val = 9 print("The value is ") print(val) print("The sum of natural numbers upto 9 is : ") print(sum_natural_nums(val))OutputThe value is 9 The sum of ...
Read MorePython Program to Read a Number n and Print the Natural Numbers Summation Pattern
When it is required to read a number and print the pattern of summation of natural numbers, a simple ‘for’ loop can be used.Below is a demonstration of the same −Examplemy_num = int(input("Enter a number... ")) for j in range(1,my_num+1): my_list=[] for i in range(1,j+1): print(i,sep=" ",end=" ") if(i
Read MorePython Program to Check if a Date is Valid and Print the Incremented Date if it is
When it is required to check if a date is valid or not, and print the incremented date if it is a valid date, the ‘if’ condition is used.Below is a demonstration of the same −Examplemy_date = input("Enter a date : ") dd, mm, yy = my_date.split('/') dd=int(dd) mm=int(mm) yy=int(yy) if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12): max_val = 31 elif(mm==4 or mm==6 or mm==9 or mm==11): max_val = 30 elif(yy%4==0 and yy%100!=0 or yy%400==0): max_val = 29 else: max_val = 28 if(mm12 or dd max_val): print("The date is ...
Read MorePython Program to Compute Simple Interest Given all the Required Values
When it is required to compute a simple interest when the amount, rate and interest are given, a simple formula can be defined, and the elements can be plugged into the formula.Below is a demonstration of the same −Exampleprinciple_amt = float(input("Enter the principle amount...")) my_time = int(input("Enter the time in years...")) my_rate = float(input("Enter the rate...")) my_simple_interest=(principle_amt*my_time*my_rate)/100 print("The computed simple interest is :") print(my_simple_interest)OutputEnter the principle amount...45000 Enter the time in years...3 Enter the rate...6 The computed simple interest is : 8100.0ExplanationThe principal amount, the rate of interest, and the time are taken as user inputs.Another formula is defined to ...
Read MorePython Program to Read Height in Centimeters and convert the Height to Feet and Inches
When it is required to read the height in ‘cm’ and convert it into ‘feet’ and ‘inches’, the ‘round’ method can be used.Below is a demonstration of the same −Examplein_cm=int(input("Enter the height in centimeters...")) in_inches=0.394*in_cm in_feet=0.0328*in_cm print("The length in inches is ") print(round(in_inches, 2)) print("The length in feet is") print(round(in_feet, 2))OutputEnter the height in centimeters...178 The length in inches is 70.13 The length in feet is 5.84ExplanationThe input is taken by user, as ‘cm’.It can be converted into inches by multiplying it with 0.394.This is assigned to a variable.It can be converted into feet by multiplying it with 0.0328.This is ...
Read MoreMatrix creation of n*n in Python
When it is required to create a matrix of dimension n * n, a list comprehension is used.Below is a demonstration of the same −ExampleN = 4 print("The value of N is ") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1))) for i in range(N)] print("The matrix of dimension N * 0 is :") print(my_result)OutputThe value of N is 4 The matrix of dimension N * 0 is : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]ExplanationThe value of N is predefined.It ...
Read MoreGet Nth Column of Matrix in Python
When it is required to get the ‘n’th column of a matrix, the ‘any’ method can be used.Below is a demonstration of the same −Examplemy_list = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The list is : ") print(my_list) N = 1 print("The value of N has been initialized to -") print(N) elem = 30 my_result = any(sub[N] == elem for sub in my_list) print("Does the element exist in a particular column ? ") print(my_result)OutputThe list is : [[34, 67, 89], [16, 27, 86], [48, 30, 0]] The value of N has been initialized ...
Read MoreVertical Concatenation in Matrix in Python
When it is required to concatenate a matrix vertically, the list comprehension can be used.Below is a demonstration of the same −Examplefrom itertools import zip_longest my_list = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The list is : ") print(my_list) my_result = ["".join(elem) for elem in zip_longest(*my_list, fillvalue ="")] print("The list after concatenating the column is : ") print(my_result)OutputThe list is : [['Hi', 'Rob'], ['how', 'are'], ['you']] The list after concatenating the column is : ['Hihowyou', 'Robare']ExplanationThe required packages are imported.A list of list is defined, and is displayed on the console.The list comprehension is used to zip ...
Read MorePython Program to Remove the Characters of Odd Index Values in a String
When it is required to remove characters from odd indices of a string, a method is defined that takes the string as parameter.Below is the demonstration of the same −Exampledef remove_odd_index_characters(my_str): new_string = "" i = 0 while i < len(my_str): if (i % 2 == 1): i+= 1 continue new_string += my_str[i] i+= 1 return new_string if __name__ == '__main__': my_string = "Hi there Will" my_string = remove_odd_index_characters(my_string) print("Characters from odd index ...
Read More