How do i print multiple of 5 in python?
In this programming article, we are going to learn Show
Python program to print multiples of a given numberThe program to find the multiple sof a number in python is as follows:
The output of the program to print multiples of a given number in python is as follows:
Few important tips about the program1. In the program we take a user input and convert it to integer data type. 2. We have found out the first ten multiples of the number. 3. We have to run a loop from 1 to 10 and have multiplied it with the number to get its multiples. Program to print multiples of a given number in python snapshot:
Given the above approach, try to print the first n multiples of a given number in python. Try to do it as hands-on. Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com About the Author Devjeet Roy Page Views : Published Date : Dec 30,2021 General feedbackI will try to go over some points that can be useful to note. Firstly there are several things I like about your code. Firstly it is very readable. Secondly I like that you split your logic. You also split finding the string and printing it. This is good. With this being said there are always things which could and should be improved SemanticsYou should use the
Which makes your code reusable for later. Eg you can call functions from your file in other programs. Your
AlternativesOne problem with your code is that you have multiple exit points. Now this is not something to sweat too hard over,
and it is not a goal to always try have a single exit. However it can be easier to debug a code with fewer exit points. This is of course much more relevant in longer and more complex code. Though it is a good thing to always have in mind. One way to do this is to define a new variable
The code now only has two exit points however it can still be improved. One key point is that if a number is divisible by
As a last point the
Which combines the two exit points into a single one. To sumarize
Closing commentsPython has a style guide PEP 8 which explains in excruciating detail how to structure your code. I whole heartily recommend skimming through it and follow it. The problem
You can even shorten this into
Using some cryptic pythonic voodoo. However as I said in the introductory I like your code, because it is easy to understand. Almost always it is better to have clear, readable code than cryptic code which is a few lines shorter. This of course disregards any speed improvements and such How do you check for multiples of 5 in Python?In Python, the remainder operator (“%”) is used to check the divisibility of a number with 5. If the number%5 == 0, then it will be divisible.
How do I print multiples of a number in Python?We can use range() function in Python to store the multiples in a range. First we store the numbers till m multiples using range() function in an array, and then print the array with using (*a) which print the array without using loop.
How do I print multiple of 3 in Python?Here's a Python program to do that, n = int(input('Enter any number: ')) print(list(range(0,n+1,3))). n = int(input(" Enter the value of n : "). list1 = [ ]. for i in range( 1 , n ):. if ( i % 3 == 0 ):. list1.append(i). print (f" This are multiples of 3 : \n{list1}. "). |