In this post, we will look how to code using for/while loop in python alongwith how to mention comments in the python file.
I. Comments in Python
a. Multiple Line Comments
To mention multiple line comments, we need to use multiple single quotes.
'''' Multiple line comments in Python can be placed like this also. '''
b. Single Line Comments
In python we used to mention single line comments by beginning the line with "#".
II. While and FOR Loop
While and for loops are used to run a specific statement multiple times until the condition is true.
for loop provides more options and is considered more feasible in usage.
# while loop to print i till the condition is true. i=1 while i <=5: print(i) i=i+1 # or i+=1 print("Loop completed") # Loop across a string characters for letter in "ngelinux": print(letter) # Loop in a range for i in range(5,15): print(i) # Loop through an array items=["pencil","rubber","pen"] for i in range(len(items)): print(items[i])
/Users/saket/PycharmProjects/1helloworld/venv/bin/python /Users/saket/PycharmProjects/1helloworld/looping.py 1 2 3 4 5 Loop completed n g e l i n u x 5 6 7 8 9 10 11 12 13 14 pencil rubber pen Process finished with exit code 0