0% found this document useful (0 votes)
46 views21 pages

L2 PythonBasics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views21 pages

L2 PythonBasics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ED5340 - Data Science: Theory at h y

and Practise a p
gan
th u
M u
a n
ath
a n
a m
R

Ramanathan Muthuganapathy ([Link]


Course web page: [Link]
Moodle page: Available at [Link]
Python identifiers and keywords

• P.I. is a name to identify variable, function, class, module etc.


h y
at
• rules for identifiers an
a p
u g
uth
• starts with an alphabet or _ th a n M
n a
am
• followed by zero or more a
letters,
R _ or digits

• keywords cannot be used

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Datatypes in Python
Recall Datatypes in C / C++ (if you know)

• int
h y
at
• float an
a p
u g
uth
• complex th a n M
n a
m a
• bool R a

• string
• bytes

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some differences with C

• int is of arbitrary precision (as you type, it will allocate memory!)


h y
at
• floats are 64-bit double precision values (max.
an
a p value?) - fractional or
exponential form u g
uth
n M
th a
• Only strings and no chars a m a n a
R
• complex contains real and imag.
• bool takes only true or false
• bytes, containers and user-defined ones (classes) - we will see later.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L2_demodatatypes.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Identifiers / Variables
variable names follow similar rules in C

• No need to define a variable (dynamic typing / allocation)


h y
at
• `type’ function is used to find out an objectantype
ap
u g
uth
• multiple assignments can be donethin
an different ways
M
n a
m a
• a=b=3 R a

• a = 3; b = 3 # semicolon as a separator
• a, b = 3, 4.45 # This is a very interesting way.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Arithmetic Operators

• Unary operators (+, -, ????)


h y
at
• Binary operators (+, - , *, / , //, **, %) an
a p
u g
uth
• Ternary operator (remember the ? t:han) M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Unary Operators

• Unary + and - are same as in C/C++


h y
at
• No unary increment in decrement operators
an
a p
u g
uth
• Typecasting (more of C++ style) th a n M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Binary operators, precedence, assignment expression
[Link]

• + , - and * are as in C
h y
at
• / is True division (No integer division as in aC).
na p 3/5 will not yield 0.
u g
uth
• ** is the exponentiation i.e. a ** b ishaequal
t
n M to ab
n a
m a
• // return quotient (without theRafractional part, like a ‘floor’ operation)

• % gives remainder - could get unexpected result!


• Operator precedence - PEMDAS
• +=, -=, *=, /=, //=, **=, %=
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
PEMDAS
Operator precedence

h y
pat
a
gan
th u
M u
a n
ath
a n
a m
R
y

Demo using
at h
a p
gan
th u
u

L2_Operatordemo1.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


What will be the answer to the following?

• 10 // 3
h y
at
• -10 // 3 an
a p
u g
uth
• 10 // -3 th a n M
n a
m a
• 10 % 3 R a

• -10 % 3
• 10 % -3

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Conversions between datatypes

• Mixed mode
h y
• op. between int and float will yieldanfloat ap
a t
u g
• op. between int and complex a n M u t
willh
yield complex
a th
• op. between complexRaand
m a n float will yield complex
• Convert one to another using built-in functions int( ), float( ),
complex( ), bool( )
Built-in math functions

• E.g. abs(x), pow(x,y) etc.


h y
at
• Qn3: Find the min and max values of a given
an
a pset of values
u g
uth
• Qn5: Given x and y, find the quotient
th a n and remainder. Round them to TWO
M
digits after decimal point. n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Object - data and member functions
`complex’ object

a = 1+ 2j
h y
print([Link]) # Data pat
a
gan
th u
u
print([Link]) # Data a n M
ath
a n
a m
print([Link]()) # Function R

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L2_Complexdemo.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Library functions (called as modules)
E.g. for more sophisticated mathematical functions

• Look at modules such as math, cmath, random, decimal etc.


h y
at
• Use `import math’ for using math module an
a p
u g
uth
• math module has mathematical functions,
th a n trigonometric
M functions etc.
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Module - How to put into use?
math module as example

import math
h y
pi = [Link]; r = 5 pat
a
gan
th u
u
area = pi * r ** 2 a n M
ath
a n
a m
print(area) R

area1 = [Link] * r ** 2

print(area1)

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


importing function from a module
cos function in math

from math import cos


h y
x = [Link] / 3 pat
a
gan
th u
u
y = cos(x) # No need to use [Link]
h a n M
a t
an
m
Ra

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L2_import_example.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some questions to explore

• Qn6: Find out the regular math function (not in modules)


h y
at
• Qn7: Find out the trigonometric functions available
a na p in math module.
u g
uth
• Qn8: Find out the various roundingthafunctions
n M for a number.
n a
a
m
• Qn9: Find out the various a
styles
R of commenting

• Qn10: How do you write a `multi-line’ statement?

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras

You might also like