A Short Introduction to Python
A Short Introduction to Python
THE RUNDOWN
• Introduction to Python
• Python Basics
THE RUNDOWN
• Introduction to Python
• Python Basics
Introduction to Python
• Introduction to Python
• Python Basics
Installing Python
• Major releases
• Python 2.x
• Python 3.8.x
• Installing the Python from the above link installs a Python interpreter
along with a (simple) IDE.
• There are a few Python IDEs that you can choose from
• Each IDE offers a set of features while some features maybe absent
• NumPy:
• For numerical/mathematical processing pip install numpy
• OpenCV:
• For image processing pip install opencv-python
• Matplotlib:
• For plotting graphs and visualizing data pip install matplotlib
• Pandas:
• For handling/analyzing data pip install pandas
• Scipy:
• For scientific/technical computing pip install scipy
• Scikit-learn:
• For machine learning pip install scikit-learn
Some Commonly Used Python Packages
(Cont’d)
• Some functionality may be replicated across different packages
• Fourier transform present in both OpenCV and NumPy
• The alias acts as a nickname and can be used throughout the code
import numpy as np
import cv2 as opencv
import random as rand
import time
import csv
THE RUNDOWN
• Introduction to Python
• Python Basics
Python Basics
• Variables:
• No need to explicitly declare a variable (data type)
• However, variables need to be initialized
Variable1 = 4.5 Variable2 = “Fortis Fortuna Audivat”
Variable3 = True Variable4 = None
• Conditional Statements:
if counter == 10:
…do something…
elif:
…do something else…
else:
…do something else entirely…
Python Basics
• Repetition Structures:
• for and while loops are used most commonly
• The body of the loop is indented one level more the loop control statement
While loop For loop
while (condition): for counter in range (10):
…do something…
…do something…
condition increment/decrement
• Range is a built in function that produces an “iterable” that is used for the
total number of the iterations in for loop.
Python Basics
• Comments:
• Comments are started with #
• Block comments (also known as Documentation Strings) are encased within “““ ”””
• A good practice to give some details of what a particular block of code does using
documentation string
Python Basics: Lists, Tuples and Dictionaries
• Indexing starts at 0
print(my_list1[0]) #1
• Lists are mutable – meaning that the value of any element (or sub-element)
can be changed at a later stage
• my_list4[3]= [10,12]
• my_list4 = [[1,2],[3,4],[5,6],[10,12]]
Python Basics: Lists (Cont’d)
• Tuples are immutable – meaning that their value cannot be changed after it
has been declared
• Dictionary keys can only be immutable so that they don’t change over time
Python Basics: Dictionary (Cont’d)
• To declare a dictionary, we use braces
my_dictionary1 = { }
my_dictionary2 = {1 : “Asad”, 2 : “Saad”, 3 : “Sara” } #Each pair is in key : data form
my_dictionary3 = { ‘Batman’ : ‘Bruce Wayne’, ‘Superman’ : ‘Clark Kent’}
• Any change to nums or tri will change the list that exists in the memory
• my_dictionary5=copy.deepcopy(my_dictionary3)
The above line of code creates a deep copy of my_dictionary3 and stores it in
my_dictionary5
Any changes to either one of the dictionaries defined above with be restricted to those
dictionaries only
Python Basics: Useful In-Built Functions
Python Basics: (Some) Useful In-Built Functions
• range(starting_point,terminating_point,increment/decrement)
• At least, the terminating point needs to be provided
• range produces an open ended interval
• The last element in the list produced by range(10) will be 9
• Keeping with the spirit of “quick and dirty” scripting, the goal is to get
the job done and then worry about optimization
QUESTIONS?