Professional Documents
Culture Documents
INTRODUCTION TO PYTHON
1.OVERVIEW
Python is a high-level, interpreted, general-purpose, dynamic
programming language.
It is a widely used programming language.
Python possesses a property of code termed reusability.
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 2
XXXXA11– Python Programming
2.FEATURES
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 3
XXXXA11– Python Programming
3. IDLE
IDLE (short for Integrated Development
Environment or Integrated Development and
Learning Environment) is an integrated development
environment for Python.
It is packaged as an optional part of the Python
packaging with many Linux distributions.
IDLE is intended to be a simple IDE and suitable for
beginners, especially in an educational environment.
According to the included README, its main features
are:
4. PYTHON INTERPRETER
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 4
XXXXA11– Python Programming
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 5
XXXXA11– Python Programming
>>> 8+9
Press Enter:
17
>>>
5.1 COMMENTS
Just like other programming languages, Python
allows you to add comments in the code.
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 6
XXXXA11– Python Programming
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 7
XXXXA11– Python Programming
5.4 VARIABLES
A variable represent a name that holds a value.
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 9
XXXXA11– Python Programming
Variable = expression
Memory
2016 Albert
Year name
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 10
XXXXA11– Python Programming
Example
Example
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 11
XXXXA11– Python Programming
>>> type(age)
<type „str‟>
>>> age = int(raw_input(“What is your age? “))
What is your age? 46
>>> type(age)
<type „int‟>
5.5.1 Numeric
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 12
XXXXA11– Python Programming
>>>print num2
2.5
5.5.2 String
>>> string1=”Hello”
>>> print string1[1]
„e‟
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 14
XXXXA11– Python Programming
5.5.3 List
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 15
XXXXA11– Python Programming
>>>first=[1,”two”,3.0,”four”]
>>>second=[“five”, 6]
>>>print first
[1, „two‟, 3.0, „four‟]
>>>print first+second
[1, „two‟, 3.0, „four‟, „five‟, 6]
>>>print second * 3
[„five‟, 6, „five‟, 6, „five‟, 6]
>>>print first[0:2]
[1, „two‟]
>>>
5.5.4 Tuple
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 17
XXXXA11– Python Programming
memory as compared to
the list
6 occur place.
5.5.5 Dictionary
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 18
XXXXA11– Python Programming
Example of dictionary:
>>> dict1 = {1:"first line", 2:"second"}
>>> print dict1
>>>{1: 'first line', 'second': 2}
5.5.6 Sets
Example
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 19
XXXXA11– Python Programming
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 20
XXXXA11– Python Programming
5.5.7 Boolean
Example
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 21
XXXXA11– Python Programming
>>> a = True
>>> type(a)
<type „bool‟>
>>> x = False
>>> type(x)
<type „bool‟>
6. type() Function
Example
>>> x = 10
>>> type(x)
<type „int‟> >>> type(„hello‟)
<type „str‟>
>>> import os
>>> type (os)
<type „module‟>
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 22
XXXXA11– Python Programming
7. OPERATORS
Operators are constructs used to modify the values of
operands. Consider the following expression:
3+4=7
In the above expression, 3 and 4 are the operands
whereas + is operator.
Based on functionality, operators are categories into
following seven types:
1. Arithmetic operator
2. Comparison operator
3. Assignment operator
4. Logical operator
5. Bitwise operator
6. Membership operator
7. Identity operator
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 23
XXXXA11– Python Programming
Example
>>> z = x / y
>>> x = 10 >>> print z
>>> y = 12 0 #Ouput
>>> z = 0
>>> z = x + y >>> z = x % y
>>> print z >>> print z
22 #Ouput 10 #Ouput
>>> z = x - y >>> z = x ** y
>>> print z >>> print z
-2 #Ouput 1000000000000 #Ouput
>>> z = x * y >>> z = x // y
>>> print z >>> print z
120 #Ouput 0 #Ouput
Example
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 24
XXXXA11– Python Programming
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 25
XXXXA11– Python Programming
Example
>>> x = 10 22 #Output
>>> y = 12
>>> y %= x
>>> y += x >>> print y
>>> print y 2 #Output
22 #Output
>>> y **= x
>>> y *= x >>> print y
>>> print y 1024 #Output
220 #Output
>>> y //= x
>>> y /= x >>> print y
>>> print y 102 #Output
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 26
XXXXA11– Python Programming
Example
>>> x = 10 # 10 = 0000 1010 6 # 6 = 0000 0110
>>> y = 12 # 12 = 0000 1100
>>> z = 0 # Bitwise inverse
>>> z = ~x
# Bitwise AND >>> print z
>>> z = x & y -11 # -11 = 1111 0101
>>> print z
8 # 8 = 0000 1000 # Left shift
>>> z = x << 2
# Bitwise OR >>> print z
>>> z = x | y 40 # 40 = 0010 1000
>>> print z
14 # 14 = 0000 1110 # Right shift
>>> z = x >> 2
# Bitwise XOR >>> print z
>>> z = x ^ y 2 # 2 = 0000 0010
>>> print z
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 27
XXXXA11– Python Programming
Example
>>> x = True True #Output
>>> y = False
>>> print (not x)
>>> print (x and y) False #Output
False #Output
>>> print (not y)
>>> print (x or y) True
Example
>>> x = 10 >>> list = [21, 13, 10, 17]
>>> y = 12
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 28
XXXXA11– Python Programming
Example
>>> x = 12 >>> y = 10
>>> y = 12 >>> if ( x is not y):
print “x is not same as y”
>>> if ( x is y): else:
print “x is same as y” print “x is same as y”
else: x is not same as y #Output
print “x is not same as y”
x is same as y #Output
8. PRECEDENCE OF OPERATORS
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr
Calicut University 29
XXXXA11– Python Programming
9. Associativity
Example
>>> 3 * 4 // 6 >>> 3 ** 4 ** 2 # 3^16
2 #Output 43046721 #Output
Tinoj Thomas |Asst. Prof, Dept. Computer Applications | Sree KeralaVarma College | Tcr