Python Syntax, Comment and Variable





 In this third Blog of Python series, we will learn about Python Syntax, Python Comment, and Python Variable and a little bit about Scope in Python.


1)Python Syntax


Lines of Code that are written in different IDE are known as syntax in Python. Syntax can also be directly run on the command line or python shell. 


eg:  >>> print("Hello World")

       >>>Hello World


#Indentation

Spaces at the beginning of the python code line are known as Indentation in Python. In the case of other programming languages,  the indentation is used for the readability of code only but they are very important in python as python use indentation for indicating block of codes. If you skip indentation, python will show an error message. As a programmer, the number of space is up to you, but it should be at least 1. The same number of spaces should be used for same block of codes.


eg:   if 5>2:                                                                                                                                             print("true")                                                                                                                              else:                                                                                                                                               print("fasle")


2)Python Comments


Comments are used to explain python codes, to make code more readable, and to prevent execution while testing code. Line started with  '#'  as a comment by Python. They can be placed anywhere and rest if the lin eos ignored. For the purpose of multiline comments, python doesn't have special syntax. We can give '#' for each line. If we do not want to use this, we can use a multiline string. As string literals that are not assigned to the variable are ignored, we can use triple quote ("' ...."') write comment inside it.



3)Python Variables

#Creating Variables

Variables are a container for storing data values. Unlike another programming language, Python has no command for declaring a variable. A variable is created when you first assign a value to it.
eg:  x=12    
       y="Roman"
       print(x)
       print(y)
Variables do not need to be declared with any particular type and can be even changed after they have been set. A single or double quote has no difference in Python. "Hello world" is same as 'Hello World'.

#Variable Names

Variable names could be short (as x and y) or a more descriptive one (as firstvalue,lastname). Some Rules for variable names:-
  • Must start with a letter or underscore character.
  • Cannot start with a number.
  • Can contain only alphanumeric (0-9 and A-z) and underscores.
  • They are completely case sensitive(age, Age, and AGE are different). 

Legal Variable Names                                                         Illegal Variable Names
myvar="valid"                                                                       my var="invalid"
MYVAR="VALID"                                                               2myvar="invalid"
my_var1="valid"                                                                   my-var="invalid"

A single value can be assigned to multiple variables.
eg:  Lang1,Lang2,Lang3="Python"  or Lang1=Lang2=Lang3="Python"
Printing Lang1 or Lang2 or Lang3 will give Python as output.


#Global Variables and The global Keyword

Variables that are created outside of a function(all eg: above) are known as global variables. They can be used anywhere either inside or outside a function(We will discuss function in upcoming chapters)

eg: x="awesome"
      def my_func():
           print("Python is " + x)
>>>Python is awesome
Here a variable is created outside a function and used inside a function. But if the variable with the same name is created inside the function ,then it can be used only inside the function as it acts as local variable.

eg: def my_func():
          x="(Python Course on Blogger")

      print(x)
>>>It will say x is not defined because x is declared only as local variable.

To solve this problem , a special keyword is used which is called global keyword.

eg: def my_func():
          global x
          x=("Python Course on Blogger")
          my_func()

       print(x)
>>> Python Course on Blogger

We can also replace variable outside a function with variable inside function with the help of global keyword.


Bonus(Python Scope)

A variable is only available from the region it is created. This region is called Scope.
It says to us that a variable declared inside a function cannot be used outside the function but can be used in function inside the function.



Comments

Popular posts from this blog

Python Booleans

Getting Started With Python