7. Before We
Get Started
○ https://goo.gl/6wcQKU
OR
○ https://github.com/itzpc/Introduction-To-Python-
Programming
7
Introduction To Python Programming /in/pcakhilnadh
Fork the Repository
14. 14
According to Official Documentation of Python
“ Python is considered as Interpreted Language ”
It's worth noting that languages are not interpreted or
compiled, but rather language implementations either
interpret or compile code.
Introduction To Python Programming /in/pcakhilnadh
Python : Interpreted or Compiled ?
15. Introduction
○ From Shell
○ From File
python3 filename.py
15
Introduction To Python Programming /in/pcakhilnadh
Ways to Run a Py File
16. Introduction
16
Introduction To Python Programming /in/pcakhilnadh
Introduction to Memory Management
X=10
10 X
X=10
Y=X
10 X
Y
10
10
“ Everything in Python is an Object ”
Other Prog Lang C/JAVA
17. 17
Introduction To Python Programming /in/pcakhilnadh
Introduction to Memory Management
X=11 Y
X
“ Python is Dynamically Typed Language ”
10
11
Z=10 Y
X
10
11
Z
What Happens X=11 ?
Python Optimizes Memory
19. Introduction
19
Introduction To Python Programming /in/pcakhilnadh
Sample Python File
Syntax Function Can Pass Arguments
Note the Space .
Indentation – Technically !
NO semi colon ?
20. Conditional
Statements
○ if statement
○ if else statement
○ Chained Conditional (~Switch)
○ Nested Conditional
20
Introduction To Python Programming /in/pcakhilnadh
26. 26
Arithmetic Operator
○ Addition +
○ Subtraction -
○ Multiplication *
○ Division /
○ Modulus %
○ Exponent **
○ Floor Division //
Exercise 4
Make a Simple Calculator Program . Fill this code snippet and pull a request in GitHub
Introduction To Python Programming /in/pcakhilnadh
27. 27
Comparision Operator
○ Equal to ==
○ Not Equal to !=
○ Greater Than >
○ Less Than <
○ Greater than Equal to >=
○ Less than Equal to <=
Introduction To Python Programming /in/pcakhilnadh
28. 28
Logical Operator
○ Logical AND AND
○ Logical OR OR
○ Logical NOT NOT
Introduction To Python Programming /in/pcakhilnadh
Exercise 5
Let x be a two digit number. Print Grade according to the following condition
x >90 print Eligible for Research .
x between 70 and 90 print Distinction
if X is exactly 75 and above print Eligible for Placement
if X is less than 70 print Fail
Fill this code snippet and pull a request in GitHub
29. 29
Bitwise Operator
○ Binary AND &
○ Binary OR |
○ Binary XOR ^
○ Binary Complement ~
○ Binary Left Shift <<
○ Binary Right Shift >>
Introduction To Python Programming /in/pcakhilnadh
Exercise 6
Run this code snippet . To Demonstrate working of Bitwise Operator
32. 32
None
○ Denote lack of value
a = None
Introduction To Python Programming /in/pcakhilnadh
33. 33
Number
○ Boolean - immutable
○ Integers - immutable
○ Float - immutable
○ Long - immutable
○ Complex - immutable
Introduction To Python Programming /in/pcakhilnadh
34. 34
Boolean
○ bool(expression) -> Convert the expression to Boolean value
○ bool(1) -> True
○ Following are considered False
○ False
○ None
○ Numeric Zero
○ Empty Sequences and Collections
Introduction To Python Programming /in/pcakhilnadh
35. 35
Integers
○ int (number, base) -> Convert the string ‘number’ from ‘base’ to decimal value
○ base is optional if given ‘number’ must be of type <class string>
○ Python consider Integers as BigInt by default .
○ Can handle as many long integers as possible
○ a = int(70)
Introduction To Python Programming /in/pcakhilnadh
36. 36
Float
○ Float (number) -> Convert the string or integer ‘number’ to float value
○ number can take
○ Sign. : ‘+’ or ‘-’
○ Infinity : ‘inf’ or ‘Infinity’
○ NaN : ‘nan’
○ a =float(‘ 1e-003’) -> 0.001
Introduction To Python Programming /in/pcakhilnadh
37. 37
Complex
○ complex (real, imaginary) -> return complex number of form real + j imaginary
○ a = complex(1) -> 1+j 0
Introduction To Python Programming /in/pcakhilnadh
39. 39
String
Introduction To Python Programming /in/pcakhilnadh
○ Hold string type value
○ No char type in python
○ It also represent array of bytes (hold data from files)
○ a = str(object) -> Return a string containing a printable representation of an object
40. 40
String
Introduction To Python Programming /in/pcakhilnadh
○ Str_object.split(separator,maxsplit)
“ab cd ef”.split() -> [‘ab’, ‘cd’, ‘ef’]
○ input() -> To read input from user
a = input(“ Enter a String ” )
○ “joinString”.join(sequence)
“-”.join([‘a’, ‘b’ ,‘c’ ,‘d’]). -> a-b-c-d
41. 41
Tuple
Introduction To Python Programming /in/pcakhilnadh
○ Ordered and indexed collection of objects
○ Tuples of two or more items separated by comma
○ tuple(iterable). -> Convert an iterable into a tuple
○ Packing
my_tuple=tuple(3,4,’dog’)
○ Unpacking
a,b,c = my_tuple
42. 42
List
○ Ordered and indexed collection of objects
○ Mutable
○ list(sequence) -> convert the objects into list
a=list(‘foo’) -> [‘f’, ‘o’, ‘o’]
○ List Indexing
Introduction To Python Programming /in/pcakhilnadh
Forward Indexing
Backward Indexing
a[0] -> 1
a[-1] ->5
43. 43
List Slicing
○ listObject[ <start> : <end> : <step> ]
a[1:3] -> 2 3 (Everything from 1 to 3 excluding the 3rd position)
a[1:] -> 2 3 4 5 (Everything from 1 to end)
Introduction To Python Programming /in/pcakhilnadh
Exercise 7
Read string from user and store it in list .Sort and Reverse the list
and print the elements in the even position
Fill this code snippet and pull a request in GitHub
46. 46
Dictionary
○ Mutable
○ Unordered Collection
○ Keys must be hashable and unique
○ List and Dict cannot act as keys
○ ~ Hash Table in other Languages
○ dict( iterable ) -> Returns a dictionary object
○ A= { 0: ‘a’,1: ‘b’, 2: ‘c’ }
Introduction To Python Programming /in/pcakhilnadh
47. 47
For Loops
○ Used for sequential traversal
○ Syntax
for iterator_variable in sequence:
statements(s)
○ It can be used to iterate over iterators and a range.
○ Sample Code Snippet
for i in range(10) :
print(i)
○ range(start, stop[, step])
Introduction To Python Programming /in/pcakhilnadh
It is sequence or List
48. 48
While Loop
○ Execute a block of statement as long as a given condition is true.
○ If condition is False . Come out of Loop
○ syntax
while (condition) :
statements
○ x = 0
while (x < 10) :
print(x)
x = x + 1
Introduction To Python Programming /in/pcakhilnadh
49. Functions
49
○ Function is a group of related statements that perform a specific task.
○ Variables inside function has local scope
○ Syntax
def function_name(parameters):
statement(s)
return
○ Function Call
function_name(parameters)
Introduction To Python Programming /in/pcakhilnadh
Optional
50. 50
Functions
○ In Python, we can return multiple values from a function !!
def square(x,y):
return x**2, y**2
xsq, ysq = square(2,3)
Introduction To Python Programming /in/pcakhilnadh
Exercise 8
Read a number from user and find factorial using recursive
function
Fill this code snippet and pull a request in GitHub
51. 51
Lambda Function
○ The small anonymous function
○ Take ‘n’ number of arguments but only one return type
○ Syntax
lambda arguments : expression
○ lambda function to add two numbers
add = lambda x,y : x+y
add(5,6)
○ s
Introduction To Python Programming /in/pcakhilnadh
52. 52
Map Function
○ Expects a function object and any number of iterables like list, dictionary, etc. It
executes the function_object for each element in the sequence and returns a list of
the elements modified by the function object
○ Syntax
map( functionObject , iterables )
○ Example
map( square, [2,3,4,5] ) -> 4,9,16, 25
○ Add two list values
○ list_a = [1, 2, 3]
list_b = [10, 20, 30]
map(lambda x, y: x + y, list_a, list_b)
# Output: [11, 22, 33]
Introduction To Python Programming /in/pcakhilnadh
53. 53
Modules
○ A code library.
○ A file containing a set of functions you want to include in your application
○ To create a module just save the code you want in a file with the file extension .py
○ Example
save this as mymodule.py
def greeting(name):
print("Hello, " + name)
○ import and use the functions
import mymodule
mymodule.greeting("Jonathan")
○ import math
○ Import numpy
Introduction To Python Programming /in/pcakhilnadh