Python Basic Syntax

Before you start learning of python programming then the following terminology are very useful. they are directly or indirectly involve of our program.

Interpreters Vs Compiler : Interpreters are more similar to compiler, they are used to convert or translate high level code to machine level code. But compiler are translate source code and generate executable file. generally Interpreters are used to translate and convert source code to relevant of machine code when running of program. Compile code (executable file) by compiler can be used to multiple time. they are not translating the code of again, when execute next time. but Interpreters are need to execute every time. this reason compiled code (executable file) are some fast to execute. but that is are depend to system structure. there is possible to this will not work to another operating system

Statements : Statements is an sequence that are perform some task when execute the program. Statement can be used to control flow and execution of program. for example.

number=10 #assignment statement

#conditional statement if,elif,and else
if(number>5):
  print("number is greater than 5")
elif(number<5) :
  print("number is less than 5")
else:
  print("number is 5")
Output
number is greater than 5

Expression : expression is an type of statement, that are used to express logical, comparison, and mathematical operation. for example.

#Arithmetic expression
result=((10+2)/2)
print(result)
#comparison expression
result=10==10
print(result)

#expression (True==True)
if(True==True):
  print("Is True")
Output
py
6
True
Is True

Indentation : Indentation are used to define block of statements and area of function and control statement. that is similar to curly braces {} of other programming language. Indentation are combination of two and four spaces. this is are used at starting of line.

number=5

if(number==5):
  #this is block of if statement  
  print("True")
else:
  #This is bock of else statement
  print("False")

print(number) 
Output
True
5

Note that, if and else statement are using to Indentation (spaces) to defined block of statement

Source code : source code is combination of single and multiple statements. there are two modes are available to run python code.

Interactive Mode : use of this modes are very simple. open a terminal or command prompt (CMD) and type python. if python are install in your system then it will display following result.

$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Note that if your system are installed both python version (2,3) then python command are display default version (in this case open version 2.7). so when need to working on other version of python do this command "python3". here (3 is relate to version).

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

In similarly use version 2 using of this command. for example.

$ python2
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

And here execute your python code. for example.

>>> print(10+2)
12

Script Mode : in this method are save python source code to a python file. python file extension are .py (dot py). let take a this python code.

number=10+2
print(number)

And save to this file using of name like "example.py". And open a terminal of exmaple.py file location and type this command.

 python example.py

After that displaying this results.

12

Identifiers : this is an name of objects, variables, class and function. for example.

def info(): #info is an identifiers
  pass
  #statements


class Student:
  pass
  #statements
  
obj=Student() #obj is an identifiers  
age=12 #age is an identifiers


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved