Python Basics

About Python:

Python is a programming language with a clean syntax that is easy to learn. Python programs can be run under all desktop computers. It used in many application domains including: Web and Internet Development,Scientific apps,Desktop apps, education and general software applications.

Syntax :

Python programs can be written using any text editor and should have the extension .py. Python programs do not have a required first or last line, but can be given the location of python as their first line: #!/usr/bin/python and become executable. Otherwise, python programs can be run from a command prompt by typing python file.py. There are no braces {} or semicolons ; in python. It is a very high level language. Instead of braces, blocks are identified by having the same indentation.

Comments :

Comments are supported in the same style as Perl:
print(“This is a test”) #This is a comment.
#This is also a comment. There are no multi-line comments.

Example :

#!/usr/bin/python (The shebang line in any script determines the script’s ability to be executed like a standalone executable without typing python beforehand in the terminal)
if (x > y):
print(“x is greater than y”)
x = x -1
else:
print(“x is less than or equal to y”)

Variables and Datatype :

Variables in Python follow the standard nomenclature of an alphanumeric name beginning in a letter or underscore. Variable names are case sensitive. Variables do not need to be declared and their datatype are inferred from the assignment statement.

Python supports the following data types:

  • Boolean (True/False)
  • Integer (10/100)
  • Long (51924361L/-0x19323L)
  • Float (1.23/200.125)
  • String (“Python Programming”/’Hello World!’)
  • List (list1 = [‘physics’, ‘chemistry’, 1997, 2000])
  • Tuples (tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 ))
  • Dictionary (dict = {‘name’: ‘john’,’code’:6734, ‘dept’: ‘sales’})
  • Object (we will explain object in later)
  • None (myVar = None)

Types of Operator:

Python language supports the following types of operators.

  • Arithmetic Operators (+ Addition ; – Subtraction ; * Multiplication ; / Division ; % Modulus ; ** Exponent ; // Floor Division)
  • Comparison (Relational) Operators ( == ; != ; <> ; > ; < ; >= ; <= )
  • Assignment Operators (=  ;  += Add AND  ;  -= Subtract AND  ;  *= Multiply AND  ;  /= Divide AND  ;  %= Modulus AND  ;  **=Exponent AND  ;  //= Floor Division )
  • Logical Operators (and Logical AND ; or Logical OR ; not Logical NOT )
  • Bitwise Operators  (& Binary AND ;  | Binary OR  ; ^ Binary XOR ;  ~ Binary Ones Complement  ; << Binary Left Shift  ;  >> Binary Right Shift )
  • Membership Operators (in ; not in)
  • Identity Operators (is ; is not)