Python Identifiers ,Keywords ,Indentation , Variables ,Comments || Building blocks MSBTE

Python Identifiers ,Keywords ,Indentation , Variables ,Comments || Building blocks

Python Identifiers ,Keywords ,Indentation , Variables ,Comments || Building blocks

    Python building blocks 

    Language basic building blocks like identifiers keywords variables their types and commenting section. This basic contains are used and applied in almost every program. In this section we will see and discuss this Python building blocks. 

    Python identifiers 

    Any name is called as identifier. This names include variables name, functions name, class name, object name, package name  and module name , etc. 
    Like other programming knowledge, python also has same rules of giving identifier to an entity:such as, 
    • They start with letter A-Z or a-z or underscore [ _ ]. Internally, they can have digits but not to start with. 
    • Their length is not Limited. But referred to be meaningful. 
    • They should not contain whitespaces. 
    • They are case sensitive. That is 'num' and 'NUM' are different. 
    • They should not be keywords.(it will show error, saying "Syntax error: Invalid syntax") .
    Some example of valid and invalid identifiers : 
    • num                            #valid 
    • wordCount                #valid 
    • account_number       #valid 
    • x_co-ordinate           #valid 
    • _4                             #valid, but meaningless 
    • ErrorNumber4         #valid 
    • Plot#3                     #invalid because of special symbol # 
    • account number     #invalid because of space 
    • 255                         #invalid because starts with digit 
    • empno.                   #invalid because of special symbol . 
    • and                       #invalid because and is a keyboard 

    And one more important thing, while using underscore [ _ ] ine identifiers for class members, be careful. 
    Because the count and appearance of underscore[ _ ] identifiers for class members make it behave like access specifier. 
    • _name means, not allowed to import[ like friendly/ default member in Java] 
    • __ name means, system name 
    • __ name means,private member

    Python keywords 

    Keywords are the words whose meaning are alread  known to the compiler and also called as reserved words.
    Python has total 35 keywords.
































    Python Indentation

    Most of the previous programming languages uses pair of curly braces { } to define a logical block. But Python uses indentation.
    A logical block of code (such as, body of a function, loop, class, etc.) starts with indentation and ends with the first unindented line. 

    For ease of programming most of the programmers prefers single space or tab. (PyCharm IDE uses a single Tab, by default). See following example.

    def test function():
    for i in range(1,11):
    if i % 3== 0:
    print("out of for loop")
    Python also allows writing the code in same line, instead of indenting. See the following code.

    if True:
    #can also be written as:
    if True: print("Hello"); a=5
    But the indenting style is more preferred and convenient. We found  many C++ and Java programmers giving additional spaces at the beginning of any statement. But if you try do the same as java ,c++ in Python, it will give you an error "Indetation-Error".

    Python Variables

    Variables are the reserved memory locations to store a runtime value that has an identifier (name). It means when we create a variable, it reserves a memory location. In previous programming languages, we also said that variable is the type of memory block that allows changing its value during runtime.
    Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in variable's memory. Therefore, by assigning different data types to variables, we can store integers, decimals or characters in variable's memory.

    The best part of Python variable is, It do not need explicit declaration of variables. The declaration and creation happens automatically when we assign a value to variable. i.e. the identifier that appears on left side of assignment operator (=) creates memory location.

    For example:
    num = 45            # will make "integer" assignment to "num"

    This will automatically create a memory location named "num" and assigns 45 to it. Remember, Python don't have any explicit declaration of variable. But based on the value we assign to variable, it automatically assigns that datatype. In above statement, Python will automatically make assign "integer" datatype to "num".

    • avg= 50.25         # makes "float" assignment to "avg"
    • website = "chromecoder"        # makes "string type" assignment[=] to "website"
    • con=True             # makes "boolean" assignment to "con"

    Python also allows to assign single value to multiple variables.
    For example:

    • a = b = c = 50.45

    Here, an float object is created with the value 50.45, and all three variables are assigned to the same memory location. One more interesting valid assignment statement is :

    • n1, n2, n3 = 10, 20, 30

    will follow the assignment sequence and hence assigns 10 to n1, 20 to n2 and 30 to n3. The same thing is also possible while assigning values of different data types.
    For example : 

    • avg, name, con = 65.55, "Rahul", True

    And most important thing is follow the identifier rules that we discussed Above.

    Comments in Python

    Comments are very important while writing a program. It describes the purpose and application of written code. So that anyone else, who is updating or understanding the written code, does not have hard time to figure it out. In Python we have two types of comments.
    We know that comment is the section in Python code that will neither be compiled nor executed. It is only for understanding and testing purpose. 
    Python provides two types of comments.

    1. Single-line comment
    2. Multi-line comment

    1. Single-line comment is the comment that comments a single line of code and extends up to the newline character.We have hash (#) symbol as single-line comment. Refer following examples.

    Example 1:

    # this is single line comment
    print("Hello all")
    Example 2:

    # this is single line comment
    # this will display hello all
    print("Hello all")
    Example 3:
    print("Hello all")     #this will display Hello all
    Example 4:
    if n %2 ==0:
    # means, if number is divisible by 2
    print("This number is even")
    2. Multi-line comments are the comments that extend up to multiple lines. Obviously we can keep using # for multiple continue lines. Refer following examples.

    Example 1:
    # this can also be done
    # to make a multiline
    # comment section
    print("Hello all")
    But Python typically offers multiple line commenting facilities using triple quotes using single (''') or double (""").For example, above multi-line comment section can be written as :

    Example 2:
    '''this can also be done
    to make a multiline
    comment section'''
    print("Hello all")
    Example 3:
    """ this can also be done
    to make a multiline
    comment section """
    print("Hello all")
    The triple quotes double (“””) comments can be used as a doc-string. DocString is a short documentation string.

    We can write a DocString as the first statement of any function or class or module.

    For example,
    def square( num):
    """this method returns square of parameter passed""" 
    sq = num * num
    return sq

    Above prepared DocString can be retrieved anytime by using the system attribute _ _ doc_ _  of the function. 

    For example, you can obtain above DocString by using following statement.
    print(square._doc_) .


    1. How to play slots from Microgaming with promo code PLAY2021
      Top 구리 출장마사지 Microgaming casinos 평택 출장안마 where you can play slot machines with bonuses from 거제 출장마사지 Microgaming 이천 출장샵 and more. You may play 전라남도 출장마사지 the original games in demo mode.


    Post a Comment

    Post a Comment

    Previous Post Next Post