Professional Documents
Culture Documents
Anitha.S
August-November 2020
Anitha.S CS7593
Unit I - Python Fundamentals
Syllabus
Anitha.S CS7593
Text Books
Anitha.S CS7593
Introduction
Features of Python
Simple
Open Source
Portability
Extensive Library
Anitha.S CS7593
Applications of Python
Front-end development
Back-end development
Full-stack
Testing
Data analytics
Data science
Web design
Anitha.S CS7593
Applications of Python
Instagram
Pinterest
Dropbox
Google
Facebook
IBM
Anitha.S CS7593
Software Needed & Installation
Anitha.S CS7593
The First Program
Anitha.S CS7593
The First Program
Anitha.S CS7593
The First Program
Output
Hello, World!
Addition/Subtraction of 2 numbers
>>> 40 + 2
42
>>> 43 - 1
42
Anitha.S CS7593
Basic Programming elements
Identifiers
Comments & Print Statement
Variables
Operators
Data Types
Anitha.S CS7593
Identifiers
Definition
Identifier refers to name given to entities such as variables,
functions, structures etc
Rules
Only alphabets, digits and underscore symbol is allowed
(starting with underscore is private)
Should not start with digit
Case Sensitive
Reserved words cannot be used
Example
234mit
mit123
for
mit@au
Anitha.S CS7593
Comments & Print Statement
Comments are like notes that you leave behind, either for
yourself or someone else to read.
They are not read in by the interpreter, meaning that you can
write whatever you want, and the computer will ignore it.
In Python, we can write comments using the hash (#) symbol.
Any text that follows this symbol will be commented out
Example
Anitha.S CS7593
Comments & Print Statement
>>>” ” ”
>>>This is a multi-Line comment
>>>” ” ”
>>>print(”Hello”) # this is also a comment
>>>Hello
Anitha.S CS7593
Variables
Example
>>>temp=2
>>>temp=’mit’
Anitha.S CS7593
Variables
>>>Sport = ’baseball’
>>>print(sport)
Anitha.S CS7593
Variables
Anitha.S CS7593
Variables
Anitha.S CS7593
Variables
>>>John Smith9
Anitha.S CS7593
Variables
Anitha.S CS7593
Variables
>>>Sam
Anitha.S CS7593
Variables
>>>name = ’John’
>>>print(name)
>>>name = 10
>>>print(name)
Anitha.S CS7593
Variables
>>>10
Anitha.S CS7593
Operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
Anitha.S CS7593
Arithmetic Operators
a=8
b=4
a*b 32
a/b 2.0
a//b (Floor Division) 2
a%b 0
a**b 4096
a/0 ZeroDivisionError
a% 0 ZeroDivisionError
Anitha.S CS7593
Relational Operators
a=”mit”
b=”mit”
print(”a>b is”,a>b)
print(”a>=b is”,a>=b)
print(True > True)
print(25 > True)
print(15 > ’mit’)
print(False > True)
print(10 < 20 < 30)
print(1 < 2 < 3 > 4)
print(False == False)
Anitha.S CS7593
Logical Operators
Anitha.S CS7593
Bitwise Operators
print(5>>2)
Check print(5&7) Vs print(5 and 7)
Anitha.S CS7593
Special Operators
Identity Operators
is
is not
Membership Operators
in
not in
a=10
b=10
print(a is b)
x=”hello”
print(’h’in x)
Anitha.S CS7593
Operator Precedence
Parenthesis
Exponential Operator
Bitwise Complement,Unary minus operator
Multiplication,Division,Modulo,Floor Division
Additon, Subtraction
Left and Right Shift Operator
Bitwise AND
Bitwise XOR
Bitwise OR
Relational Operators
Assignment Operators
Identity Operators
Membership Operators
Logical not
Logical and
Logical or
Anitha.S CS7593
Python Data Types
Anitha.S CS7593
Int Data Types
a=5
type(a) #int
id(a) #0X459010
Representation
Decimal Form (eg. 10)
Binary Form (eg. 0B10)
Octal Form (eg. 0O10)
Hexa Decimal Form (eg. 0X10)
Base Conversion
print(bin(15))
print(oct(15))
print(hex(15))
Anitha.S CS7593
Complex Data Type
a+bj
eg:
a=2+4j
b=4+3j
c=a+b
print(c) #6+7j
type(c) # <class ’complex’>
print(c.real) #6
print(c.imag) #7
Anitha.S CS7593
Boolean,Range,Set Data Type
Boolean
print(True+True)
print(True-False)
Range
a=range(10) # 0,1,2,...9
a=range(10,15) # 10,11,...14
a=range(2,10,2) # 2,4,6,8
Set
a={ 20,0,100,’mit’,200 }
print(a) # { 0,100,’mit’,20,200 }
a.add(40)
Anitha.S CS7593
Mutable Vs Immutable
Mutable objects:
If the value can change, the object is called mutable
eg: list, dict, set
Immutable objects:
If the value cannot change, the object is called immutable
eg: int, float, complex, string, tuple
Type Casting
Conversion of one type value to another type
eg:
int(32.998)
int(True)
int(”5”)
int(”five”)
str(5) # ’5’
Anitha.S CS7593
Anitha.S CS7593
END OF SESSION
Anitha.S CS7593