Page 307 - Artificial Intellegence_v2.0_Class_9
P. 307
For example,
# assigning a value to a variable
num1 = 10
num2 = 20
# calculating the average
(num1 + num2) / 2
Multiple Line Comments
When we add up comments which occupy two or more lines then we begin the comment with either 3 times
single quotes ''' or double quotes """, followed by text on multiple lines and end with single quotes ''' or double
quotes """ to mark the end of the comment. For example,
"""This program calculates
the average of
two numbers stored in
two different variables"""
a = 10
b = 20
c = (a + b) / 2
The print() Function
The print() function is used to print an output on the screen. It converts the expressions into a string before
writing to the screen. Syntax to use the print( ) function is:
print(object(s), sep = separator, end = end)
Where,
• object can be one or more separated by comma and it can be a variable, literal, expression. An object will be
converted to string before printed.
• sep is used as a separator if there are more than one objects.
• end specifies what to print at the end. Default is newline '\n'.
Following are some of the examples of the print( ) function:
Commands Output
print("hello") hello
print("hello", "friends") hello friends
print("hello" + "friends") hellofriends
(This is concatenation of string using "+"
operator)
print("hello", "friends", sep="$") hello$friends
print("hello", "friends", sep="$", end="!") hello$friends!
print("hello", "friends", end="!") hello friends!
Introduction to Python 305

