• Tutorials Logic, IN
  • +91 8092939553
  • info@tutorialslogic.com

Comments and Variables in Python

Comments

Comments are annotations in the source code, which are ignored by the interpreter. Comments are the most helpful stuff of any programming language, which help us to explain code, make the code more readable, and prevent the execution of code when testing code. There are three types of comments available in Python which are given below-

Single Line Comments:- Single line Python comments are marked with # character.

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

Multiline Comments:- Python does not really have a multi line comments system, but it can be easily achieved by inserting a # character for each line.

										    
											#This is a multiline comment
											#This is a multiline comment
											#This is a multiline comment
											print("Hello, Tutorials Logic!")
											
										

Docstring Comments:- Python has the docstrings or documentation strings feature, which occurs as the first statement in the module, function, class, or method definition. We must write what a function/class does in the docstring.

										    
											def add(value1, value2):
											    """Calculate the sum of value1 and value2."""
												return value1 + value2
											
										

Docstring can be easily accessed at run time using __doc__ and the dot operator.

										    
											>>> print add.__doc__
											# Calculate the sum of value1 and value2.
											
										

Since Python will ignore string literals, which are not assigned to any variable, so it can be easily used to write multiline comment. To write comment, we can use """ or ''' at the begining and end of comment.

										    
											"""
											This is a multiline comment
											This is a multiline comment
											This is a multiline comment
											"""
											
											print("Hello, Tutorials Logic!")
											
										

Variables

Variables are the containers in the memory which holds the data value. Unlike other programming languages, Python has no command for declaring a variable, it is created the moment you first assign a value to it.

Standard Rules for Python Variable Names:-

  • The variable name in Python must begin with an english alphabet (i.e. A-Z, a-z) or the underscore (i.e _) character.
  • A variable name cannot start with a number (i.e. 0 to 9).
  • A Python variable name should not contain blank spaces.
  • Python variables are case sensitive.
										    
											x = 5 # int
											y = "Tutorials Logic!" # str
											x, y = 5, "Tutorials Logic!" # Assign value to multiple variables in a single Line
											x = y = "Tutorials Logic!" # Assign the same value to multiple variables in a single line