How do i print multiple of 3 in python?

General feedback

I 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

Semantics

You should use the if __name__ == "__main__": module in your answer.

def fizz_buzz(num):
    if num%3==0 and num%5==0:
        return 'FizzBuzz'

    elif num % 3 == 0:
        return 'Fizz'

    elif num % 5==0:
        return 'Buzz'
    else:
        return num

if __name__ == "__main__":

    for n in range(1,100):
        print(fizz_buzz(n))

Which makes your code reusable for later. Eg you can call functions from your file in other programs. Your else clause at the end of the code is useless. You could have written

    elif num % 5==0:
        return 'Buzz'
    return num

Alternatives

One 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 string

def fizz_buzz(num):
    string = ''
    if num%3==0 and num%5==0:
        string = 'FizzBuzz'

    elif num % 3 == 0:
        string = 'Fizz'

    elif num % 5==0:
        string = 'Buzz'

    if string:
       return string
    return num

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 3 and 5, it is divisible by 15. So we can gradually build the string, like shown below

def fizz_buzz(num):
    string = ''

    if num % 3 == 0:
        string += 'Fizz'

    if num % 5==0:
        string += 'Buzz'

    if string:
       return string
    return num

As a last point the return statement could be written using a terniary conditional operator

return string if string else n

Which combines the two exit points into a single one. To sumarize

def fizz_buzz(num):
    string = ''
    if num % 3==0: string +='Fizz' 
    if num % 5==0: string +='Buzz'
    return string if string else num

if __name__ == "__main__":

    for n in range(1, 100):
        print(fizz_buzz(n))

Closing comments

Python 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 FizzBuzz is very, very simple. It can be solved in a number of ways using just a simple line. Syb0rg, showed one way to write this code

for i in range(1,101): print("Fizz"*(i%3==0) + "Buzz"*(i%5==0) or i)

You can even shorten this into

i=0;exec"print i%3/2*'Fizz'+i%5/4*'Buzz'or-~i;i+=1;"*100 

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.

def main():

    num = input('Insert number:')
    output = sumOfMultiples(num)
    print(output)


def sumOfMultiples(param):

    j = 0
    i = 0
    for i in range(i, param):
        if (i % 3 ==0) or (i % 5 == 0) and (i % 15 != 0):
            j = j + i
    return j

if __name__ == '__main__':
    main()

This is the error that I get

Traceback (most recent call last):
  File "/Users/Soumasish/PycharmProjects/MultiplesOf3And5/Main.py", line 21, in 
    main()
  File "/Users/Soumasish/PycharmProjects/MultiplesOf3And5/Main.py", line 7, in main
    output = sumOfMultiples(num)
  File "/Users/Soumasish/PycharmProjects/MultiplesOf3And5/Main.py", line 15, in sumOfMultiples
    for i in range(i, param):
TypeError: 'str' object cannot be interpreted as an integer

Process finished with exit code 1

How do i print multiple of 3 in python?

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")

Output

The band for Sarah Fier is 8 out of 10

In this code, we are printing the following two variables using the print() function.

  1. band
  2. name

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 %-formatting

Let’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))

Output

The band for Sarah Fier is 8 out of 10

Pass it as a dictionary

You 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})

Output

The band for Sarah Fier is 8 out of 10

Using new-style formatting

With 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))

Output

The band for Sarah Fier is 8 out of 10

Python f-string

We 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")

Output

The band for Sarah Fier is 8 out of 10

That’s it for this tutorial.

How 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" ...