You are on page 1of 37

CS7593 - DATA STRUCTURES WITH PYTHON

Anitha.S

Department of Computer Technology


Anna University - MIT Campus
Email: anithacse@gmail.com

August-November 2020

Anitha.S CS7593
Unit I - Python Fundamentals

Syllabus

Basic Programming element Dictionaries


Variables, Operators Control Structures
Python Data types Assignment Statements
Strings Conditional Statements
Sets Looping Statements
Lists Functions

Anitha.S CS7593
Text Books

Lambert, Kenneth. Fundamentals of Python: Data


Structures. Nelson Education, 2014.
Downey, Alley B. ”Think Python: How To Think Like A
Computer Scientist, 2nd Edition, v 2.2.20 Needham,
Massachusetts.”, 2015.

Anitha.S CS7593
Introduction

Python - Guido van Rossum


Python is a high-level, general-purpose, interpreted programming
language

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

The language of Python itself – Python 3


An integrated development environment (IDE) - Jupyter
Notebook
Software platform - Anaconda
www.anaconda.com/distribution/
https://jupyter.org/install
https://www.python.org/downloads/
https://pynative.com/online-python-code-editor-to-execute-
python-code/
colab.research.google.com
$ python
>>>

Anitha.S CS7593
The First Program

Hello.c - Compile - Hello.o - Execute


Output
Hello, World!

Anitha.S CS7593
The First Program

>>> print(’Hello, World!’)

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

>>> # this is a comment


>>>
>>>print(”Hello”) # this is also a comment
>>>Hello

Anitha.S CS7593
Comments & Print Statement

To write multi-line comments so that you may write more


descriptive paragraphs for larger portions of code, we would need
to use three opening and closing double quotes
Example

>>>” ” ”
>>>This is a multi-Line comment
>>>” ” ”
>>>print(”Hello”) # this is also a comment
>>>Hello

Anitha.S CS7593
Variables

− >Reserved memory location to store values

Example

>>>temp=2

>>>temp=’mit’

Anitha.S CS7593
Variables

>>>Sport = ’baseball’
>>>print(sport)

Anitha.S CS7593
Variables

>>>NameError: name ’sport’ is not defined

Anitha.S CS7593
Variables

>>>name = ’John Smith’


>>>favnumber = ’9’
>>>print(name, favnumber)

Anitha.S CS7593
Variables

>>>John Smith9

Anitha.S CS7593
Variables

>>># adding, deleting, multiplying, dividing from a variable


>>>result = 2
>>>num1=4
>>>result += 1 # same as saying result = result + 1
>>>print(result)
>>>result *= num1 # same as saying result = result * num1
>>>print(result)

>>># defining a variable and overwriting it’s value


>>>name = ’John’
>>>name = ’Sam’
>>>print(name)

Anitha.S CS7593
Variables

>>>Sam

Anitha.S CS7593
Variables

>>>name = ’John’
>>>print(name)
>>>name = 10
>>>print(name)

Anitha.S CS7593
Variables

>>>10

Dynamic Typed Language


Static: Types checked before run-time
Dynamic: Types checked on the fly, during execution

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

print(”” and ”mit”)


print(”” or ”mit”)
print(not ””)
print(not ”mit”)

Anitha.S CS7593
Bitwise Operators

Applicable only on int and boolean data types


print(2&6)
print(2|6)
print(2ˆ6)
print(˜3)
print(5<<2)

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

Data types are how we define values, likes words or numbers. It


determines the values that the variable can contain and the
operations that can be performed on it

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

You might also like