Skip to main content

Python Comments

 

Python Comments




Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.


Creating a Comment

Comments starts with a #, and Python will ignore them:

Example

#This is a comment
print("Hello, World!")

Comments can be placed at the end of a line, and Python will ignore the rest of the line:

Example

print("Hello, World!") #This is a comment

A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code:

Example

#print("Hello, World!")
print("Cheers, Mate!")


Comments

Popular posts from this blog

Python Tuples

  Python  Tuples mytuple = ( "apple" ,  "banana" ,  "cherry" ) Tuple Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and  unchangeable . Tuples are written with round brackets. Example Create a Tuple: thistuple = ( "apple" ,  "banana" ,  "cherry" ) print (thistuple) Tuple Items Tuple items are ordered, unchangeable, and allow duplicate values. Tuple items are indexed, the first item has index  [0] , the second item has index  [1]  etc. Ordered When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. Unchangeable Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. Allow Duplicates Since tuples...

Python Math Module

  Python  Math Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers. Built-in Math Functions The  min()  and  max()  functions can be used to find the lowest or highest value in an iterable: Example x =  min ( 5 ,  10 ,  25 ) y =  max ( 5 ,  10 ,  25 ) print (x) print (y) The  abs()  function returns the absolute (positive) value of the specified number: Example x =  abs (- 7.25 ) print (x) The  pow( x ,  y )  function returns the value of x to the power of y (x y ). Example Return the value of 4 to the power of 3 (same as 4 * 4 * 4): x =  pow ( 4 ,  3 ) print (x) The Math Module Python has also a built-in module called  math , which extends the list of mathematical functions. To use it, you must import the  math  module: import  math When you have imported the  math  module,...

Python Loops

  While Loops Python Loops Python has two primitive loop commands: while  loops for  loops The while Loop With the  while  loop we can execute a set of statements as long as a condition is true. Example Print i as long as i is less than 6: i =  1 while  i <  6 :    print (i)   i +=  1 Note:  remember to increment i, or else the loop will continue forever. The  while  loop requires relevant variables to be ready, in this example we need to define an indexing variable,  i , which we set to 1. The break Statement With the  break  statement we can stop the loop even if the while condition is true: Example Exit the loop when i is 3: i =  1 while  i <  6 :    print (i)    if  i ==  3 :      break   i +=  1 The continue Statement With the  continue  statement we can stop the current iteration, and continue...