Check Valid Date and Print Incremented Date in Python

AmitDiwan
Updated on 16-Apr-2021 12:11:08

581 Views

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 −Example Live Demomy_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 ... Read More

Print Natural Numbers Summation Pattern in Python

AmitDiwan
Updated on 16-Apr-2021 12:09:38

419 Views

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 −Example Live Demomy_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

Python Program to Print Series 1 + 2 + ... + n

AmitDiwan
Updated on 16-Apr-2021 12:06:38

392 Views

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 −Example Live Demodef 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 ... Read More

Print Integers Not Divisible by 2 or 3 Between 1 and 50

AmitDiwan
Updated on 16-Apr-2021 12:06:16

2K+ Views

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 −Example Live Demoprint("Integers not divisible by 2 and 3, that lie between 1 and 50 are : ") n = 1 while n

Find Smallest Divisor of an Integer in Python

AmitDiwan
Updated on 16-Apr-2021 12:05:59

3K+ Views

When it is required to find the smallest divisor of a an integer, a simple ‘for’ loop is used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter a number...")) my_list = [] print("The number is ") print(first_num) for i in range(2, first_num+1):    if(first_num%i==0):       my_list.append(i) my_list.sort() print("The smallest divisor is : ") print(my_list[0])OutputEnter a number...56 The number is 56 The smallest divisor is : 2ExplanationThe number is taken as an input from the user.An empty list is defined.The number taken from user is displayed on the console.The number range is iterated over.It is ... Read More

Read Two Numbers and Print Their Quotient and Remainder in Python

AmitDiwan
Updated on 16-Apr-2021 12:05:07

2K+ Views

When it is required to read two numbers and print the quotient and remainder when they are divided, the ‘//’ and ‘%’ operators can be used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) print("The first number is ") print(first_num) print("The second number is ") print(second_num) quotient_val = first_num//second_num remainder_val = first_num%second_num print("The quotient is :") print(quotient_val) print("The remainder is :") print(remainder_val)OutputEnter the first number...44 Enter the second number...56 The first number is 44 The second number is 56 The quotient is : 0 The remainder is : ... Read More

Print All Numbers in a Range Divisible by a Given Number in Python

AmitDiwan
Updated on 16-Apr-2021 12:00:39

2K+ Views

When it is required to print all the elements in a given range that is divisible by a specific number, a simple for loop can be used.Below is a demonstration of the same −Example Live Demolower_num = int(input("Enter lower range limit...")) upper_num = int(input("Enter upper range limit...")) div_num = int(input("Enter the number that should be divided by...")) for i in range(lower_num, upper_num+1):    if(i%div_num==0):       print(i)OutputEnter lower range limit...3 Enter upper range limit...8 Enter the number that should be divided by...2 4 6 8ExplanationThe upper and lower range of numbers is taken as input from the user.The number by ... Read More

Compute n + nn + nnn in Python

AmitDiwan
Updated on 16-Apr-2021 12:00:20

2K+ Views

When it is required to take a number and compute a specific pattern, the value of n is taken from the user. Next, two variables are assigned this specific pattern and their sum is calculated.Below is a demonstration of the same −Example Live Demomy_input = int(input("Enter a value for n...")) temp_val = str(my_input) t_1=temp_val+temp_val t_2=temp_val+temp_val+temp_val my_result = my_input+int(t_1)+int(t_2) print("The computed value is : ") print(my_result)OutputEnter a value for n...4 The computed value is : 492ExplanationAn input from the user is taken.It is converted into a string and assigned to a variable.The value of ‘n*n’ and the value of ‘n*n*n’ is calculated.Their ... Read More

Sort Elements of an Array in Descending Order using Python

AmitDiwan
Updated on 16-Apr-2021 12:00:00

2K+ Views

When it is required to sort the elements of an array in descending order, the ‘sort’ method can be used by specifying a parameter named ‘reverse’ to True.Below is a demonstration of the same −Example Live Demomy_list = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print("The list is :") print(my_list) my_list.sort(reverse = True) print("The list after sorting is :") print(my_list)OutputThe list is : [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] The list after sorting is : [89, 78, 56, 44, 42, 31, 23, 11, 9, 0]ExplanationA list is defined, and is displayed on the console.The ... Read More

Sort Elements of an Array in Ascending Order Using Python

AmitDiwan
Updated on 16-Apr-2021 11:58:28

831 Views

When it is required to sort the elements of an array in ascending order, the ‘sort’ method can be used. It helps sort the elements in ascending order by default. If we want it to be sorted in descending order, a parameter named ‘reverse’ can be set to True.Below is a demonstration of the same −Example Live Demomy_list = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print("The list is :") print(my_list) my_list.sort() print("The list after sorting is :") print(my_list)OutputThe list is : [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] The list after sorting is : ... Read More

Advertisements