Page 273 - Ai_C10_Flipbook
P. 273

Multiline Statements

                 In Python, a statement that doesn't fit on one line can be extended over multiple lines. This can be done in
                 several ways:
                    • Using Parentheses (): You can break a long expression or function call across multiple lines if it is enclosed in
                   parentheses. Python allows this without any additional special characters.

                 [1]:   result = (11 + 15 + 148 +
                                  154 + 87)
                    • Using Braces {} (for dictionaries or sets): Similar to parentheses, curly braces can be used to break long
                   dictionary or set definitions across lines.

                 [1]:   my_dict = {
                            'name': 'John',
                            'age': 30,
                            'city': 'New York'
                        }

                    • Using Square Brackets [] (for lists): You can also break a list definition or list comprehension into multiple
                   lines using square brackets.

                 [1]:   my_list = [
                            1, 2, 3, 4,
                            5, 6, 7, 8
                        ]

                    • Using the Continuation Character (\): A backslash (\) at the end of a line indicates that the statement continues
                   on the next line. This method is used for breaking any statement into multiple lines.

                 [1]:   result = 11 + 15 + 148 + \
                                 154 + 87


                 Multiple Statements

                 Multiple statements mean more than one statement in a single line. This can be done by separating the
                 statements using semicolons (;).

                 [1]:   a=5; b=10; c=a+b; print(c)

                        15

                 Comments

                 Comments are used to increase the readability of the code. We use it to give proper understanding of the code in
                 simple English statements. They are completely ignored by the Python interpreter and do not display or affect the
                 output of the code. It is always a good practice to add comments in your code so that if anybody in future wish to
                 understand or modify your code then through comments it become easy to know what you have written in a code.
                 In Python we can give two different kind of comments. Let us discuss about them.

                 Single Line Comments

                 Starts with a hash symbol ‘#’ followed by the text to be written as a comment that lasts till the end of the line.




                                                                                    Advance Python (Practical)  271
   268   269   270   271   272   273   274   275   276   277   278