Page 172 - Ai_V3.0_c11_flipbook
P. 172

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:
               • •    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.

                Program 3: To demonstrate the use of single line comment

                   # This is a single line comment
                   print("Hello, world!")  # This is another single line comment
                    Output:

                   Hello, world!
               • •    Multiline comments: Python does not have a built-in syntax for multiline comments like some other languages.
                  However, multiline strings enclosed within triple quotes (‘‘‘ or “””) are often used for this purpose. Although these are
                  technically strings and not comments, they serve the purpose of commenting out multiple lines of code.

                Program 4: To demonstrate the use of multiline comments

                   '''
                   This program demonstrates basic arithmetic operations without using functions.
                   It performs addition and subtraction of two numbers.
                   '''

                   # Addition
                   a = 10
                   b = 5
                    170     Touchpad Artificial Intelligence (Ver. 3.0)-XI
   167   168   169   170   171   172   173   174   175   176   177