Python
Python is High level, object oriented, and interpreted programming language. Like Perl it is a
scripting language.
History:
Python was designed by GuidoVanRossum, while working at Center for wisdom and
informatics (CWI) in the Netherlands.
Python is as a successor to the ABC language
Python was released to the world in 20 February 1991
It was named after 1970s BBC comedy series Monty python’s flying circus
Python Interpreter: The Python interpreter is usually installed with the python home directory.
To call python interpreter we use the following command
python <Source file Name>
Ex python Hello.py
The python interpreter converts the source code into object code
Python shell: A 'shell' is a user interface. A python shell is a way for a user to interact with
the python interpreter.
There are several Python shells, with varying features:
Ex: The default Python shell, Ipython, IPython qtconsole
IDLE is more an IDE than a shell, but it does have a shell besides an editor where results are
displayed when code in the editor is run.
SSpyder is a full featured IDE, and like IDLE, it also has a shell. This shell can be used for
interactive computations.
In Ubuntu we need to open terminal and type command python it will open the python shell
Python Shell in Ubuntu
Python Shell in Windows
1
https://gvsnarasimha.blogspot.com/
Comments in Python: Comments are used to describe the purpose of the code. Comments are ignored
by the Python system. They are used purely to communicate information to a human reader.
# is used to write a comment
Ex: >>> 8 / 5 # division always returns a floating point number
1.6
Documenting Code: Python documentation strings (or docstrings) provide a convenient way of
associating documentation with Python modules, functions, classes, and methods.
We use triple quotes like ‘‘‘ or “ “ “.
Ex:
def add( ):
""" This function will add two numbers """
a = int(input(“Enter The first number ”))
b = int(input(“Enter the second number”))
print(“Sum of two numbers is”, a+b)
Using Python as a Calculator: Python interpreter acts as a simple calculator. Which can perform all the
arithmetic operations.
>>> 2 + 2
4
>>> 50 - 5
45
>>> 8 / 5 # division always returns a floating point number
1.6
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # result * divisor + remainder
17
With Python, it is possible to use the ** operator to calculate powers
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
2
https://gvsnarasimha.blogspot.com/
String: A string is a sequence of characters .They can be enclosed in single quotes (’...’) or double quotes
("...") with the same result. \ can be used to escape quotes: for a multiline string we use triple quotes like
‘‘‘ or “ “ “.
Ex:
>>> 'python is easy to learn'
'python is easy to learn'
>>> "String"
'String'
Variable: A variable is a name given to memory location during execution it changes the data values.
Ex: a = int (input (“Enter a value”))
Indentation: Indent is cursor position used to represent group of statements. At the interactive prompt, we
have to type a tab or space(s) for each indented line.
All decent text editors have an auto-indent facility. Each line within a basic block will have same
amount of indent.
Ex:
def add ( ):
a = int (input (“Enter first value”))
b = int (input (“Enter second value”))
print (“Sum of two numbers”, a+b)
add ( )
Sum.py
Identifiers: Identifier is the name given to entities like class, functions, variables etc . In
Python, It helps in differentiating one entity from another.
Rules for writing identifiers in Python:
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z)
or digits (0 to 9) or an underscore (_).
Names like student, sno, marks, stu_mno, all are valid example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly
fine.
3. Keywords cannot be used as identifiers.
4. We cannot use special symbols like! , @, #, $, % etc. in our identifier.
5. Identifier can be of any length.
Keywords: Keywords are the reserved words. We cannot use a keyword as variable name, function name
or any other identifier. They are used to define the syntax and structure of the Python language. In Python,
keywords are case sensitive.
python 3.4.1 has the following keyword
3
https://gvsnarasimha.blogspot.com/
Operators: An operator is a symbol that tells the computer to perform certain mathematical
or logical manipulations. Operators are used in programs to manipulate data and variables.
Ex:
c=a+b
In this statement +, = are operators and a, b, c are operands (variables).in this instruction
the sum of two variables a, b is carried out and the result is stored in variable c.
Operators in Python
1. Arithmetic Operators
2. Relational Operators (Comparison Operators)
3. Logical Operators
4. Bitwise operators
5. Assignment Operators
6. Special Operators
Arithmetic Operators: All the arithmetic operators will perform basic calculations. The
operators +,-,*, /, //, % all work the same way as in other languages.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor Division
% Modulo Division
** Exponent
Table: Arithmetic Operators
Python Program Demonstrating the use of arithmetic operators
a = int (input ("Enter First Value"))
b = int (input ("Enter Second Value"))
print("sum", a+b)
print("difference", a-b)
print("product", a*b)
print("division", a/b)
print("modulus", a%b)
print("floor division", a//b)
print("exponent", a**2)
Arith.py
Output:
4
https://gvsnarasimha.blogspot.com/
Relational Operators: Relational Operators used in comparing the quantities and to formulate
some conditions. The value of a relational expression is either true or false.
Ex: 10 < 100 is true
100 < 10 is false
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Table: Relational Operators
Python Program Demonstrating the use of Relational operators
a = int (input ("Enter First Value"))
b = int (input ("Enter Second Value"))
print("less than: ",a<b)
print("less than or equal to: ",a<=b)
print("greater than: ",a>b)
print("greater than or equal to: ",a>=b)
print("equal to:" ,a==b)
Relational.py
Output:
Logical Operators: In order to evaluate more than one condition we use logical operators. in
python we use and, or, not as logical operators
Operator Meaning
and logical And
or logical Or
not logical Not
Table: Logical Operators
Python Program Demonstrating the use of Logical operators
5
https://gvsnarasimha.blogspot.com/
a = True
b = False
print(a and b)
print(a or b)
print(not a)
Logical.py
Output:
Bitwise operators: we use bitwise operators to manipulate data at bit level.
Operator Meaning
& Bitwise AND
| Bitwise Or
^ Bitwise Exclusive Or
<< Shift left
>> Shift Right
~ Bitwise Inverse
Table: Bitwise operators
Python Program Demonstrating the use of Bitwise operators
a = int(input ("Enter First Value"))
b = int(input ("Enter Second Value"))
print("Bitwise AND :",a&b)
print("Bitwise Or :",a|b)
print("Bitwise Exclusive Or :",a^b)
print("Bitwise Inverse :",~a)
print("Shift left :",a<<b)
print("Shift Right :",a>>b)
Bitwise.py
Output:
6
https://gvsnarasimha.blogspot.com/
Assignment Operators: Assignment operator used to store some value (right side)into
variable(left side). Compound-assignment operators provide a shorter syntax for assigning the result of
an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the
result to the first operand.
Simple assignment Short Hand Operator
statement
a = a+1 a += 1
a = a-1 a -=1
a = a* 1 a *=1
a=a/1 a /=1
a=a%b a %= b
a = a&b a &= b
a=a|b a |= b
a=a^b a ^= b
a = a << b a <<= b
a = a >> b a >>= b
Table: Assignment Operators
Python Program Demonstrating the use of Assignment operators
a = int(input ("Enter First Value"))
b = int(input ("Enter First Value"))
a += b
print("+=",a)
a -= b
print("-=",a)
a *= b
print("*=",a)
a /= b
print("/=",a)
a %= b
print("%=",a)
a //=b
print("//=",a)
a **=b
print("**=",a)
a &= b
print("&=",a)
a |= b
print("|=",a)
a ^= b
print("^=",a)
a <<= b
print("<<=",a)
a >>= b
print(">>=",a)
Assignment.py
7
https://gvsnarasimha.blogspot.com/
Output:
Special Operators:
Membership operators: “in” and “not in” are the membership operators in Python. They are used to test
whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
Operator Meaning
in Member of sequence
not in Not a member of sequence
Python Program Demonstrating the use of membership operators
list1 = [1,2,3,'Ram','Rani',3.2]
print(1 in list1)
print(1 not in list1)
print('Ram' in list1)
print(5.5 not in list1)
Output:
8
https://gvsnarasimha.blogspot.com/
Identity operators: “is” and “is not” are the identity operators in Python. They are used to check if two
values (or variables) are located on the same part of the memory. Two variables that are equal does not
imply that they are identical.
Operator Meaning
is Same memory location
is not Not same memory location
Python Program Demonstrating the use of Identity operators
#for integer values
a = int(input ("Enter First Value"))
b = int(input ("Enter Second Value"))
print("IS",a is b)
print("iS NOT :",a is not b)
#for string values
s1 = 'Hello'
s2 = 'Hello'
print(s1 is s2)
#for list
L1 = [1,2,3]
L2 = [1,2,3]
Print(L1 is L2)
Output:
9
https://gvsnarasimha.blogspot.com/