Beginner · First steps in Python · topic 2 of 50 · 15 min
print() and comments
Explanation
Printing text and numbers
print is a function. A function is a named action you can use. You use (or "call") a function by writing its name followed by round brackets. Whatever you put inside the brackets is what print shows.
print("Good morning")
print(42)
print(10 + 5)Printing several things at once
Separate items with commas. print puts one space between them automatically.
print("Total:", 450, "rupees")Changing the separator and the ending
print has two optional settings. sep is what goes between items (a space by default). end is what goes after the last item (a new line by default).
print("2024", "06", "15", sep="-")
print("Loading", end="...")
print("done")Comments: notes for humans
A comment is text that Python skips. It starts with the # sign and runs to the end of the line. Use comments to explain why your code does something.
# Price of one samosa in rupees
print(15) # this comment is at the end of a linePrinting versus returning
In the practice tasks on this site you will often write a function that returns a value instead of printing it. Returning hands the value back so the checker can test it. You will learn functions fully later; for now, read return as "give back this answer".
Watch the lesson
print() and comments · 1/6
In this lesson you will learn to show things on the screen with print, and to leave notes in your code with comments.
Prefer a video by a teacher? Find free videos on “python print function and comments tutorial” on YouTube ↗
In real life: Printing a restaurant bill
Four friends eat at a dhaba. The owner wants a neat bill on screen with the item names and prices. print with commas and sep makes a clean, readable bill in a few lines.
print("Paneer tikka", 220, sep=" .... Rs ")
print("Butter naan x4", 160, sep=" .... Rs ")
print("Lassi x4", 200, sep=" .... Rs ")
print("Total", 580, sep=" .... Rs ")Practice (0/2 done)
Write a function bill_line() that returns the string "Samosa: 15 rupees". (Return the text; do not print it.)
Someone turned an important line into a comment by mistake, so answer() returns 0. Remove the # from the right line so that answer() returns 42. Keep the first comment, it is a useful note.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.