Вы находитесь на странице: 1из 2

Assignment 01 SCS 2004

Introduction to Programming Languages


Y.S.HORAWALAVITHANA : 10002103(2010CS210)

Comment Types in Python


A comment is a note to you & other programmers to identify what is happening in the code. In python programming, different types of commenting types are used to interact with the code in better way. So others who read your program will be cleared what is complicated. Single line & Multi line are most commonly used types allowed in python programming. Single line Commenting In python command, single line comment follows by a hash : #.A comment can include any text or whitespace character. When program is running, text after the # through to the end of that line is ignored. The # does not have to be at the beginning of a new line. A hash character within a string literal is just a hash character. Example code:
# An example Python program with comments # Program name - excomment.py # Written by Y.S. Horawalavithana # Date and version No: 6/14/12 # This is a description of what the program does # and how its should be used. # Get two numbers from the user partner1_age = input("How old are you? "); # should use raw_input? partner2_age = input("How old are your partner? "); agedistance = partner1_age - partner2_age # calculate age distance add_com=# This is not a comment # Display user the result print "You will be exactly older than your partner age in",\ agedistance,\ "years"

But please be considered if you are using Unix system & if you make the program executable with chmod & have the line as this starting with # but including !.
#! /usr/bin/env python3

Assignment 01 SCS 2004


Introduction to Programming Languages
Y.S.HORAWALAVITHANA : 10002103(2010CS210)

Multi line Commenting Texts are enclosed with triple-double quote symbols can be used as multi line comments. Example code:
this is a multiline comment which spawns many lines

Also this multi line comments can be used to embed doc strings in a function. To explain this scenario let me define a class human with some functions. Example code:
class human: this class can be used to define a type of human. It also has following methods and properties Basic_info() Favourites() def Basic_info(name,age,sex): myname=name; myage=age; mysex=sex; def Favourites(activity): print My Favourite activities are ,activity

In above piece of code the triple quoted string not only is a comment but when declaring an object say h=human and then h. __doc__ returns those comments & also help(human) returns same comments.

Вам также может понравиться