Recents in Beach

print function and Comments

Previous                                                          Next⏩ 

       
  The print function in Python is a function that outputs to your console window whatever you say you want to print out. At first blush, it might appear that the print function is rather useless for programming, but it is actually one of the most widely used functions in all of python. The reason for this is that it makes for a great  tool.
"Debugging" is the term given to the act of finding, removing, and fixing errors and mistakes within code.
If something isn't acting right, you can use the print function to print out what is happening in the program. Many times, you expect a certain variable to be one thing, but you cannot see what the program sees. If you print out the variable, you might see that what you thought was, was not.

The print Function
Most notable and most widely known change in Python 3 is how the print function is used. Use of parenthesis () with print function is now mandatory. It was optional in Python 2.


1
2
print "Hello World" #is acceptable in Python 2
print ("Hello World") # in Python 3, print must be followed by ()

The print() function inserts a new line at the end, by default. In Python 2, it can be suppressed by putting ',' at the end. In Python 3, "end =' '" appends space instead of newline.


1
2
print x,           # Trailing comma suppresses newline in Python 2
print(x, end=" ")  # Appends a space instead of a newline in Python 3

Python Commenting Basics


Comments are for developers. They describe parts of the code where necessary to facilitate the understanding of programmers, including yourself.
To write a comment in Python, simply put the hash mark # before your desired comment:
         # this is a comment
Python ignores everything after the hash mark and up to the end of the line. You can insert them anywhere in your code, even inline with other code:
                    
        print("Hello friends !") # this is won't run



Example: 1
To print the Welcome to programokey, use the print () function as follows:
1
print ("Welcome to programokey")

Output :
          Welcome to programokey



Previous                                                          Next⏩ 

Post a Comment

0 Comments