Python Tutorial#
Introduction#
Python is a great general-purpose programming language on its own, but with the help of a few popular libraries (NumPy, SciPy, Matplotlib, OpenCV) it becomes a powerful environment for scientific computing.
This tutorial is very brief introduction to the Python programming language. We personally believe that this tutorial will give you an insight to a new programming language. This tutorial will serve as a quick crash course both on the Python programming language and on the use of Python for scientific computing.
Some of you may have previous knowledge in MATLAB, in which case we also recommend the NumPy for Matlab users page.
In this tutorial, we will cover:
Basic Python:
Basic data types (Containers, Lists, Dictionaries, Sets, Tuples),
Functions,
Classes
How to quick start in python?#
I personally recommend to use Anaconda for quick start in python specially for beginners. Start with the following steps:
Download Anaconda from https://www.anaconda.com/download (Choose according to your operating system)
Anaconda is completely free and includes more than 300 python packages. Both python 2.7 and 3.7 options are available.
Anaconda provides excellent binding of packages and their use. Usually, I recommend to use virtual environment for any kind of project.
A virtual environment can be created from the terminal using the command
conda create -n yourenvname python=x.x anacondaNote: I personally recommend python 3.12.
Example:
conda create -n py312 python=3.12 anacondaIt creates a virtual environment by name
py312through anaconda where python 3.12 will be installed along with many basic packages (numpy, scipy, scikit-learn, scikit-image, Pillow, Panda, jupyter, spyder, etc.).Start jupyter from the terminal by running
jupyter notebook
Basics of Python#
Python is a high-level, dynamically typed multiparadigm programming language. Python code is often said to be almost like pseudocode, since it allows you to express very powerful ideas in very few lines of code while being very readable.
As an example, here is an implementation of the classic quicksort algorithm in Python:
[1]:
def quickSort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2] # Pick median as pivot
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quickSort(left) + middle + quickSort(right)
print(quickSort([3,6,8,10,1,2,1,1]))
[1, 1, 1, 2, 3, 6, 8, 10]
Python versions#
Any one can check your Python version at the command line by running python --version.
Alternatively we can check using the library platform in jupyter itself.
[2]:
from platform import python_version
print(python_version())
3.12.1
Basic data types#
Numbers#
Integers and floats work as you would expect from other languages:
[3]:
x = 3
print(x, type(x))
3 <class 'int'>
[4]:
print(x + 1) # Addition
print(x - 1) # Subtraction
print(x * 2) # Multiplication
print(x ** 2) # Exponentiation
4
2
6
9
[5]:
pow(x,2)
[5]:
9
[6]:
x += 1 # x = x+1
print(x) # Prints "4"
x *= 2
print(x) # Prints "8"
4
8
[7]:
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
<class 'float'>
2.5 3.5 5.0 6.25
Note that unlike many languages, Python does not have unary increment (x++) or decrement (x–) operators.
Python also has built-in types for long integers and complex numbers; you can find all of the details in the documentation.
Booleans#
Python implements all of the usual operators for Boolean logic, but uses English words rather than symbols (&&, ||, etc.):
[8]:
t, f = True, False
print(type(t)) # Prints "<class 'bool'>"
<class 'bool'>
Now let’s look at the operations:
[9]:
print(t and f) # Logical AND;
print(t or f) # Logical OR;
print(not t) # Logical NOT;
print(t != f) # Logical XOR;
False
True
False
True
Strings#
[10]:
hello = 'hello' # String literals can use single quotes
world = "world" # or double quotes; it does not matter.
print(hello, len(hello))
hello 5
[11]:
hw = hello + ' ' + world # String concatenation
print(hw) # Prints "hello world"
hello world
[12]:
i=12
hw12 = '{} {} {}'.format(hello, world, i)
print(hw12) # prints "hello world 12"
hello world 12
String objects have a bunch of useful methods; for example:
[13]:
s = "hello"
print(s.capitalize()) # Capitalize the first character; prints "Hello"
print(s.upper()) # Convert a string to uppercase; prints "HELLO"
print(s.rjust(7)) # Right-justify a string, padding with spaces; prints " hello"
print(s.center(7)) # Center a string, padding with spaces; prints " hello "
print(s.replace('l', '(ell)')) # Replace all instances of one substring with another;
# prints "he(ell)(ell)o"
print(' world '.strip()) # Strip leading and trailing whitespace; prints "world"
Hello
HELLO
hello
hello
he(ell)(ell)o
world
You can find a list of all string methods in the documentation.
Containers#
Python includes several built-in container types: lists, dictionaries, sets, and tuples.
Lists#
A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:
[14]:
xs = [3, 1, 2] # Create a list
print(type(xs))
print(xs, xs[2])
print(xs[-2]) # Negative indices count from the end of the list; prints "2"
<class 'list'>
[3, 1, 2] 2
1
[15]:
xs[2] = 'foo' # Lists can contain elements of different types
print(xs)
[3, 1, 'foo']
[16]:
xs.append('bar') # Add a new element to the end of the list
print(xs)
[3, 1, 'foo', 'bar']
[17]:
x = xs.pop(2) # Remove and return the last element of the list
print(x, xs)
foo [3, 1, 'bar']
Nested list#
[18]:
x = [2, 'wo', [3, 3], ['bo', 3]]
print(x)
[2, 'wo', [3, 3], ['bo', 3]]
[19]:
x[3][0] # how access an element in nested list
[19]:
'bo'
You can find all of the methods of list objects in the documentation.
Slicing#
In addition to accessing list elements one at a time, Python provides concise syntax to access sublists; this is known as slicing:
[20]:
nums = list(range(5))
print(nums) # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4]) # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:]) # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:2]) # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(nums[:]) # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print(nums[:-1]) # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print(nums) # Prints "[0, 1, 8, 9, 4]"
[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]
Loops#
You can loop over the elements of a list like this:
[21]:
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print(animal)
cat
dog
monkey
If you want access to the index of each element within the body of a loop, use the built-in enumerate function:
[22]:
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print('#{}: {}'.format(idx + 1, animal))
#1: cat
#2: dog
#3: monkey
List comprehensions:#
When programming, frequently we want to transform one type of data into another. As a simple example, consider the following code that computes square numbers:
[23]:
nums = [0, 1, 2, 3, 4]
squares = []
for num in nums:
squares.append(num ** 2)
print(squares)
[0, 1, 4, 9, 16]
You can make this code simpler using a list comprehension:
[24]:
nums = [0, 1, 2, 3, 4]
squares = [num ** 2 for num in nums]
print(squares)
[0, 1, 4, 9, 16]
List comprehensions can also contain conditions:
[25]:
nums = [0, 1, 2, 3, 4]
even_squares = [num ** 2 for num in nums if num % 2 == 0]
print(even_squares)
[0, 4, 16]
Dictionaries#
A dictionary stores (key, value) pairs, similar to a Map in Java or an object in JavaScript. You can use it like this:
[26]:
data = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
print(data['cat']) # Get an entry from a dictionary; prints "cute"
print('cat' in data) # Check if a dictionary has a given key; prints "True"
cute
True
[27]:
data['fish'] = 'wet' # Add a new key\value pair to a dictionary
print(data['fish']) # Prints "wet"
wet
[28]:
print(data['monkey']) # KeyError: 'monkey' not a key of data
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[28], line 1
----> 1 print(data['monkey']) # KeyError: 'monkey' not a key of data
KeyError: 'monkey'
[29]:
print(data.get('monkey', 'N/A')) # Get an element with a default; prints "N/A"
print(data.get('fish', 'N/A')) # Get an element with a default; prints "wet"
N/A
wet
[30]:
del data['fish'] # Remove an element from a dictionary
print(data.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"
N/A
You can find all you need to know about dictionaries in the documentation.
It is easy to iterate over the keys in a dictionary:
[31]:
data = {'person': 2, 'cat': 4, 'spider': 8}
for animal in data:
legs = data[animal]
print('A {} has {} legs'.format(animal, legs))
A person has 2 legs
A cat has 4 legs
A spider has 8 legs
If you want access to keys and their corresponding values, use the items() method:
[32]:
data = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in data.items():
print('A {} has {} legs'.format(animal, legs))
A person has 2 legs
A cat has 4 legs
A spider has 8 legs
Dictionary comprehensions: These are similar to list comprehensions, but allow you to easily construct dictionaries. For example:
[33]:
nums = [0, 1, 2, 3, 4]
even_num_to_square = {num: num ** 2 for num in nums if num % 2 == 0}
print(even_num_to_square)
{0: 0, 2: 4, 4: 16}
Sets#
A set is an unordered collection of distinct elements. As a simple example, consider the following:
[34]:
animals = {'cat', 'dog'}
print('cat' in animals) # Check if an element is in a set; prints "True"
print('fish' in animals) # prints "False"
True
False
[35]:
animals.add('fish') # Add an element to a set
print('fish' in animals)
print(len(animals)) # Number of elements in a set;
True
3
[36]:
animals.add('cat') # Adding an element that is already in the set does nothing
print(len(animals))
animals.remove('cat') # Remove an element from a set
print(len(animals))
3
2
Loops: Iterating over a set has the same syntax as iterating over a list; however since sets are unordered, you cannot make assumptions about the order in which you visit the elements of the set:
[37]:
animals = {'cat', 'dog', 'fish'}
print(type(animals))
for idx, animal in enumerate(animals, start=1):
print('#{}: {}'.format(idx, animal))
<class 'set'>
#1: dog
#2: fish
#3: cat
Set comprehensions: Like lists and dictionaries, we can easily construct sets using set comprehensions:
[38]:
from math import sqrt
print({int(sqrt(x)) for x in range(30)})
{0, 1, 2, 3, 4, 5}
Tuples#
A tuple is an (immutable) ordered list of values. A tuple is in many ways similar to a list; one of the most important differences is that tuples can be used as keys in dictionaries and as elements of sets, while lists cannot. Here is a trivial example:
[39]:
data = {(x, x + 1): x for x in range(6)} # Create a dictionary with tuple keys
tup = (5, 6) # Create a tuple
print(data)
print(type(tup))
print(data[tup])
print(data[(1, 2)])
{(0, 1): 0, (1, 2): 1, (2, 3): 2, (3, 4): 3, (4, 5): 4, (5, 6): 5}
<class 'tuple'>
5
1
[40]:
data[(0,1)]
[40]:
0
[41]:
data = (2,2,3)
print(type(data))
<class 'tuple'>
[42]:
data[0]
[42]:
2
[43]:
tup[0] = 1 # TypeError 'tuple' object does not support item assignment
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[43], line 1
----> 1 tup[0] = 1 # TypeError 'tuple' object does not support item assignment
TypeError: 'tuple' object does not support item assignment
Functions#
Python functions are defined using the def keyword. For example:
[44]:
def sign(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'
for x in [-1, 0, 1]:
print(sign(x))
negative
zero
positive
We will often define functions to take optional keyword arguments, like this:
[45]:
def hello(name, loud=False):
if loud:
print('HELLO, {}'.format(name.upper()))
else:
print('Hello, {}!'.format(name))
hello('Bob')
hello('Fred', loud=True)
Hello, Bob!
HELLO, FRED
Classes#
The syntax for defining classes in Python is straightforward:
[46]:
class Greeter:
# Constructor
def __init__(self, name):
self.name = name # Create an instance variable
# Instance method
def greet(self, loud=False):
if loud:
print('HELLO, {}!'.format(self.name.upper()))
else:
print('Hello, {}'.format(self.name))
g = Greeter('Fred') # Construct an instance of the Greeter class
g.greet() # Call an instance method; prints "Hello, Fred"
g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"
Hello, Fred
HELLO, FRED!
This tutorial is modified version of the CS231n Python NumPy Tutorial by Justin Johnson.