Python Tutorial(Technogyyan.tech)

Python Identifiers

Python Identifier is used to identify a function, class, variable, module, or another object, it is a collection of characters, digit, and underscore.

An identifier starts with -
  • letter A to Z 
  • letter a to z 
  • underscores(_)
  • it should start with (one or more characters) or (an underscore) then use digits (0 to 9).
Examples of valid identifiers:
  • var1
  • _var1
  • _1_var
  • var_1
  • Var1
  • ___1var
Python doesn't allow punctuation characters like 'Hash, @, $,! ,and % within identifiers.

Examples of invalid identifiers
  • $var1
  • 2var
  • 1_var
  • var'Hash1

Python is a case-sensitive language. This means Var and var are not the same, A and a are not the same these two are different. Always give the identifiers name which makes sense. c = 10 is a valid name but if you write count = 10 that makes more sense, and it would be easier to find out what it represents when you look at your code after a long gap/time.

Multiple words can be separated using an underscore, like First_Name

Keywords

Python keywords are reserved words that we cannot use as static or variable or any other identifier name. all the keywords are always is in lowercase letters, it is restricted.

The following list shows the Python keywords.

  • and
  • exec
  • not
  • assert
  • finally
  • or
  • break
  • for
  • pass
  • class
  • from
  • print
  • continue
  • global
  • raise
  • def
  • if
  • return
  • del
  • import
  • try
  • elif
  • in
  • while
  • else
  • is
  • with
  • except
  • lambda
  • yield

Python Indentation

Indentation means the spaces at the starting of a code line. In other programming languages, the indentation in the code is only for readability,  in Python the indentation is very important.

  • Python uses indentation to represent a block of code, as shown in the example below

>>>if(10>2):

         print("10 is greater than 2")

Output:-

10 is greater than 2 

Indentation in Python shell (technogyyan.tech)

Indentation in Python Shell 

You can see it in this image, before print() it gives some space in it, it shows an error if you don't give space in it.
  • If you skip the indentation, Python will give you an error:
>>>if(10>2):
print("10 is greater than 2") 
It gives you a syntax error because you skip the indentation. See the below image.
Indentation in python shell(technogyyan.tech)
                                                                                                                     Indentation in python shell
    • The number of spaces depends on you as a programmer, but it must be at least one.
    >>>if(10>2):
              print("10 is greater than 2")
    else:
        print("10 is smaller than 2")  
    output:-
    10 is greater than 2.
    Indentation in python shell(technogyyan.tech)
                                                                                                    Indentation in python shell
      • You need to use the same number of spaces in the same block of code, if not Python will give you an error:
      >>>if(10>2):
                  print("10 is greater than 2")
                          print("2 is smaller than 10")
      Indentation in python shell(technogyyan.tech)
                                                                                                    Indentation in python shell

        Creating a Comment

        Comments in Python start with a hash punctuation mark. Comments do not work, when you run a program you will not see any indication. The comments are in the source code for human reading, not for computer execution. A comment doesn't have to be text that explains the code, it can also be used to prevent Python from executing code
        • Comments start with a hash mark, and the Python will ignore them:
        'hash This is a comment
        print("Welcome to the Technogyyan!")

        comment into Python shell.(Technogyyan.tech)  

        Comment in python shell.

        • Comments can be located at the end of the line and Python ignores the rest of the lines:

        print("Welcome to the Technogyyan!") 'hash This is a comment

        comment at the end of the line into Python shell.(Technogyyan.tech) 

        comment at the end of the line in Python shell.

        Multiline Comment

        Python does not actually have a syntax for multi-line comments.
        • You can add a 'hash for each line to add a multi-line comment:
        'hash This is a comment
        'hash written in
        'hash more than just one line
        print("Welcome to the Technogyyan!")

        Multiline commentinto Python shell.(Technogyyan.tech)

        Multiline comment(Using 'hash) in Python shell.

        Python will ignore non-assigned string literals to the variable.

        • You can add a multiline string in triple quotes(""") to your code and leave your comment in it:

        """

        This is a comment

        written in

        more than just one line

        """

        print("Welcome to the Technogyyan!") 

        Multiline comment(triple quotes) into Python shell.(Technogyyan.tech)

        Multiline comment(triple quotes) in Python shell.

        Python will ignore non-assigned string literals to the variable.

        Thank You!!

        Post a Comment

        Previous Post Next Post