Page 54 - Touhpad Ai
P. 54
Example:
name = input("Enter your name: ")
u The print() Function: The print() function prints or sends the output to the standard output device, which is usually
a monitor. This function auto converts the items to strings, i.e., if you try printing a numeric value, the print()
function will automatically convert it into an equivalent string and print it. The print() function also introduces
spaces between items automatically with the help of the comma operator.
The syntax of the print() function is as follows:
print (<expression 1> , <expression 2>...)
Example:
print ("Python is easy")
Program 1: To demonstrate the use of the input( ) and print( ) functions
name =input("Enter your name: ")
age =input("Enter your age: ")
print(name,"your age is", age)
Output:
Enter your name: Yash
Enter your age: 25
Yash your age is 25
The format() method in Python is used to print the values in the specified format and insert them into a string's placeholders,
which are defined using curly brackets {}. The detailed explanation along with examples demonstrating how to use the
format() method with integers, strings, and decimals. For float, two decimal places with .2f is used. The syntax of the
format() is as follows:
string.format(value1, value2, ...)
Program 2: To demonstrate the use of format()
name = input("Enter Name: ")
age = int(input("Enter Age: "))
percentage=float(input("Enter Your Percentage in Previous Class: "))
print("My name is {}.".format(name))
print("I am {} years old.".format(age))
print("My percentage is {:.2f}".format(percentage))
Output:
Enter Name: Yash
Enter Age: 26
Enter Your Percentage in Previous Class: 86.5
My name is Yash.
I am 26 years old.
My percentage is 86.50
Comments in Python
In Python, comments are used to annotate code with explanations, documentation, or notes. Comments are ignored by
the Python interpreter during execution and are solely for human readers. Python supports two types of comments:
u Single line comments: Single line comments start with the hash character # and continue until the end of the line.
Anything following the # on that line is considered a comment and is ignored by the interpreter.
52 Touchpad Artificial Intelligence - XI

