How do i print multiple of 3 in python?
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 Show 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 I'm relatively new to python and thus trying to set myself up running some simple algorithms. Here's the first problem from project euler, although there are other solutions available to the same problem in python, but I've tried a different approach. In crux the idea is to find the sum of all multiples of 3 or 5 less than 1000. This is my code.
This is the error that I get
Printing one variable is an easy task with the print() function but printing multiple variables is not a complex task too. There are various ways to print the multiple variables. To print multiple variables in Python, use the print() function. The print(*objects) is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space. There are many ways to print multiple variables. A simple way is to use the print() function. band = 8 name = "Sarah Fier" print("The band for", name, "is", band, "out of 10") OutputThe band for Sarah Fier is 8 out of 10 In this code, we are printing the following two variables using the print() function.
Inside the print() function, we have put the variable name in their respective places, and when you run the program, it reads the values of the variables and print their values. This is the clearest way to pass the values as parameters. Using %-formattingLet’s use the %-formatting and pass the variable as a tuple inside the print() function. band = 8 name = "Sarah Fier" print("The band for %s is %s out of 10" % (name, band)) OutputThe band for Sarah Fier is 8 out of 10 Pass it as a dictionaryYou can pass variables as a dictionary to the print() function. band = 8 name = "Sarah Fier" print("The band for %(n)s is %(b)s out of 10" % {'n': name, 'b': band}) OutputThe band for Sarah Fier is 8 out of 10 Using new-style formattingWith Python 3.0, the format() method has been introduced for handling complex string formatting more efficiently. Formatters work by putting in one or more replacement fields and placeholders defined by a pair of curly braces { } into a string and calling the string.format(). It is a new style of string formatting using the format() method. It is useful for reordering or printing the same one multiple times. band = 8 name = "Sarah Fier" print("The band for {} is {} out of 10".format(name, band)) OutputThe band for Sarah Fier is 8 out of 10 Python f-stringWe can use the f-string to print multiple variables in Python 3. Python f String is an improvement over previous formatting methods. It is also called “formatted string literals,” f-strings are string literals that have an f at the starting and curly braces containing the expressions that will be replaced with their values. band = 8 name = "Sarah Fier" print(f"The band for {name} is {band} out of 10") OutputThe band for Sarah Fier is 8 out of 10 That’s it for this tutorial. Related postsHow to Print Type of Variable in Python How to Print an Array in Python How to Print Bold Python Text How do you print multiples of 3 in Python?As shown in the code list of multiples of 3 will print.. 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}. "). 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 you print multiples of 3 in for loop in Python?>>> def myTest(): ... for x in range(100): ... if x%3==0 and x%5==0: ... print "Both" ... elif x%3==0: ... print "Three" ...
|