Python
Python is an interpreted, high-level, general-purpose programming language. It was created by Guido van Rossum and first released in 1991. he started Python as a hobby project to keep him occupied in the week around Christmas. Got its name from the name of British comedy troupe Monty Python. It is used in.
- Software Development
- Web Development
- System Scripting
- Mathematics
Features of Python
- Easy to use: Due to simple syntax, it is very easy to implement anything and everything
- Interpreted language: In this, every code is executed line by line.
- Cross-platform language: It can run on Windows, Linux, Macintosh, Raspberry Pi, etc.
- Expressive language: It supports a wide range of library
- Free & open source: It can be downloaded free of cost and the source code can be modified for improvement
Shortcomings of Python
- Lesser libraries: As compared to other programming languages like C++, java, .Net, etc. it has a lesser number of libraries, but libraries of other libraries can easily be implemented and imported
- Slow language: Since being an interpreted language, the code is executed slowly
- Weak on type-binding: A variable that is initially declared int can be changed to string at any point in the program without any typecasting.
Installing Python
- Python's latest version can be downloaded from https://www.python.org/downloads/.
- At the time of writing this, the latest version was 3.7.4.
- Download the installer file according to your operating system and execute the installer commands according to your Operating System.
Python 2 vs Python 3
| Python 2 | Python 3 |
|---|---|
| The output of the division operator is an integer | The output of the division operator is Float |
| print "hello" is the implementation of the print function here | print("hello") is the implementation of the print function here |
| Here Bytes and String are the same | Here Bytes and String are different |
| The implicit String type here is ASCII | The implicit String type here is Unicode |
| raise IOError, “your error message” is used to raise an exception | raise IOError(“your error message”) is used for raising an exception |
| The syntax here is comparatively difficult to understand. | The syntax is simpler and easily understandable. |
| The xRange() function is used to perform iterations. | The new Range() function was introduced to perform iterations. |
| The value of the global variable will change while using it inside the for-loop. | The value of variables never changes. |
| Python 2 code can be converted to Python3 code | It is not backward-compatible |
| It uses raw_input() for taking inputs from the command line | It uses input() for taking inputs from the command line |
Flavors of Python
- CPython: Written in C, the most common implementation of Python.
- JPython: Written in Java, compiles to bytecode.
- IronPython: Implemented in C#, an extensibility layer to frameworks written in. NET.
- Brython: Browser Python, runs in the browser.
- RubyPython: A bridge between Python and Ruby interpreters.
- PyPy: Implementation in Python.
- MicroPython: Runs on a Microcontroller.
Python character set
The following are the character sets recognized by Python. It uses the traditional ASCII character set.
- Letters: A-Z, a-z
- Digits: 0-9
- Special symbols: Special Symbols available over the Keyboard (some of the symbols like the rupee symbol may not be available)
- White spaces: Blank space, tab, carriage return, newline, form feed
- Other characters: Unicode
Python tokens
The smallest individual unit in a program is known as a token.
- Keywords: There are in total of 31 reserved keywords. Ex: and, finally, not, etc.
- Identifiers: A Python identifier is a name used to identify a variable, function, class, module, or other objects. Ex: MyBook, myBook, my book, etc.
- Literals: Literals in Python can be defined as numbers, text, or other data that represent values to be stored in variables. Ex: age = 22, escape sequence, etc.
- Operators: These can be defined as symbols that are used to perform operations on operands. Ex: arithmetic operators, etc.
- Punctuators: Used to implement the grammatical and structure of a Syntax. Ex: &=, >>=, <<=, etc.
Python keywords

Rules to define Python Identifiers
- An identifier starts with a letter A to Z or A to Z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
- Python does not allow special characters
- An identifier must not be a keyword of Python.
- Python is a case-sensitive programming language.
- Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
- Starting an identifier with a single leading underscore indicates that the identifier is private.
- Starting an identifier with two leading underscores indicates a strongly private identifier.
- If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Python punctuators

Barebone of a Python program
- Expression is evaluated and hence produces results. Ex: (20=4)/4
- Statements Instructs that we are doing something. Ex: a=20 print("Calling in proper sequence")
- Comments This is readable for a programmer but ignored by the Python interpreter. a. Single line comment (#) b. Multi-line comment (''' ''') Ex: #this is a single-line comment ''' this is multiple '''
- Function a code that has some name and it can be reused. Ex: KetArgFunc()
- Block & Indentation A group of statements in block indentation at the same level creates a block. Ex: all the statements that are under the function KetArgFunc()
Python variables
A variable can be considered as a container that holds value. Python is a type-infer language i.e. one which can detect data types automatically in other words it means you don't need to specify the data type of variable.
Note. Although Python supports unlimited variable length, following PEP-8, we keep the maximum length to 79 characters.
For example
name = 'python' # String Data Type
sum = None # a variable without a value
a = 23 # Integer
b = 6.2 # Float
sum = a + b
print(sum)
Multiple assignments
- a =b =c =1 #single value to multiple variables
- a,b = 1, 2 #multiple value to multiple variable
- a,b = b,a #value of a and b is swapped
Variable scope and lifetime
- Local Variable
def fun(): x=8 print(x) fun() print(x) #error will be shown - Global Variable
x = 8 def fun(): print(x) #Calling variable 'x' inside fun() fun() print(x) #Calling variable 'x' outside fun()
Python Data Types
- Number: It is used to store numeric values. Python has 3 numeric types.
- Integers
Ex. a = 10 - Floating Point numbers
Ex. a = 10.0 - Complex numbers
Ex. a = complex(10,5) # 10+5j
- Integers
- String: A string is a sequence of characters. In Python, we can create a string using a single ( ' ' ) or double quotes (" "). Both are the same in Python.
str = 'computer science' print('str- ', str) # print string print('str[0]-', str[0]) # print 1st char print('str[1:3]-', str[1:3]) # print string from position 1 to 3 print('str[3:]-', str[3:]) # print string starting from 3rd char print('str*2-', str*2) # print string two times print('str + yes-', str + 'yes') # concatenated string - Boolean: It is used to store two possible values either true or false.
str = "comp sc" b = str.isupper() print(b) - List: Lists are collections of items and each item has its index value. Denoted by [].
list = [6,9] list[0] = 55 print(list[0]) print(list[1]) - Tuple: A tuple is an immutable Python Object. Immutable Python objects mean you cannot modify the contents of a tuple once it is assigned. Denoted by ().
tup = (66,99) Tup[0] = 3 #error will be displayed print(tup[0]) print(tup[1]) - Set: It is an unordered collection of unique and immutable items. Denoted by {}.
set1 = {11, 22, 33, 22} print(set1) - Dictionary: It is an unordered collection of items and each item consists of a key and a value.
dict = {'subject' : 'comp sc", 'class' : '11'} print(dict) print( "Subject :", dict["subject"]) print("class :", dict.get('class'))
Python operators
- The Precedence of Operators in Python can be found in the rule PEMDAS ( Parenthesis, Exponential, Multiply, Division, Addition, Subtraction). Operators with the same precedence are evaluated from left to right.
- Python Arithmetic Operator It is used for mathematical operations.
x = 5 y = 4 print('x + y =',x+y) #Addition print('x - y =',x-y) #Substraction print('x * y =',x*y) #Multiplication print('x / y =',x/y) #Division print('x // y =',x//y) #Floor Division print('x ** y =',x**y) #Exponent - Python Comparison Operators are used to compare values.
x = 101 y = 121 print('x > y is',x>y) #Greater than print('x < y is',x<y) #Less than print('x == y is',x==y) #Equal to print('x != y is',x!=y) #Not Equal to print('x >= y is',x>=y) #Greater than equal to print('x <= y is',x<=y) #Less than equal to - Python Logical Operators.
x = True y = False print('x and y is',x and y) #if both are true print('x or y is',x or y) #if either one is true print('not x is',not x) #returns the complement - Python Bitwise Operators are Used to manipulate bit values.
a = 6 b = 3 print ('a=',a,':',bin(a),'b=',b,':',bin(b)) c = 0 c = a & b print ("result of AND is ", c,':',bin(c)) c = a | b print ("result of OR is ", c,':',bin(c)) c = a ^ b print ("result of EXOR is ", c,':',bin(c)) c = ~a print ("result of COMPLEMENT is ", c,':',bin(c)) c = a << 2 print ("result of LEFT SHIFT is ", c,':',bin(c)) c = a>>2 print ("result of RIGHT SHIFT is ", c,':',bin(c)) - Python Membership Operators Test for membership in a sequence.
a = 5 b = 10 list = [1, 2, 3, 4, 5 ] if ( a in list ): print ("Line 1 - a is available in the given list") else: print ("Line 1 - a is not available in the given list") if ( b not in list ): print ("Line 2 - b is not available in the given list") else: print ("Line 2 - b is available in the given list") - Python Identity Operators They check if values on either side of the equality operator point to the same object.
a = 10 b = 10 print ('Line 1','a=',a,':',id(a), 'b=',b,':',id(b)) if ( a is b ): print ("Line 2 - a and b have same identity") else: print ("Line 2 - a and b do not have same identity")
| Unary Operator | Method |
|---|---|
| - | __neg__(self) |
| + | __pos__(self) |
Python Operator Overloading
In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.





Govardhan PPosted Dec 16, 2019, 11:25 PM
Excellent one!
Dipa MehtaPosted Nov 27, 2019, 2:11 AM
Thanks for sharing.
Sourav Kumar DasPosted Nov 27, 2019, 12:39 AM
Nice article Sir.
Onkar SharmaPosted Oct 14, 2019, 12:27 AM
Nice Article, Very informative... ..
sreenivasa kPosted Sep 23, 2019, 8:20 AM
Nice article
Rajanikant HawaldarPosted Aug 29, 2019, 9:44 AM
Nice article
Mahesh ChandPosted Aug 29, 2019, 7:45 AM
Nicely done, Rohit.