Python Programming for Computer Science

This document provides an overview of various functions for Python programming

Andrew L. Mackey

Overview

Statistics and probability are important tools for the analysis of data.

 

Python Basics

The following code is useful for general data wrangling tasks.

 

Python Data Structures

The following are examples of the four built-in data types in Python.


List Example

# list
list1 = ["a", "b", "c"]

print( list1[0], list1[1], list1[2])

list1.append("d")

n = size(list1)
print( list1[n - 1] )   #print last element

Tuple Example

# *** Tuple ***
tuple1 = ("a", "b", "c")

print(tuple1[0], tuple1[1])
n = len(tuple1)
print(tuple1[n-1])

Set Example

# *** Set ***

set1 = { "a", "b", "c" }

# add a member
set1.add("d")

# remove a member
set1.remove("c")

# add items to set1 from any iterable object
set2 = { "m", "n", "o" }
set1.update(set2)
set1.update(["x", "y"])

print(set1)

result1 = "a" in set1    #is "a" in the set?
result2 = "z" in set1    #is "z" in the set?

print(result1, result2)

Dictionary Example

# *** Dictionary ***
test = {
        "a" : 123,
        "b" : 456,
        "c" : 789
}

 

Classes

Python supports object-oriented programming.

class Car:

	def __init__(self):
		self.var1 = []


To create an instance of the given class, you can simply execute c = Car(). The method __init()__ is automatically executed when the instance is created. This is the constructor in the Python programming language. The purpose of the constructor is typically to assign values to instance variables of the class when an object is defined. You can also add arguments to this function.

class Car:

	def __init__(self, color, doors):
		self.var1 = []
		self.main_color = color
		self.num_doors = doors

	def change_color(self, color):
		self.main_color = color

	def get_color(self):
		return self.main_color

Inheritance

Python also supports inheritance. The parent class(es) should be placed in parentheses next to the class name.

class Blue_Car(Car):

__call__ Method

One interesting feature for C/C++ and Java programmers that are just starting with Python is the built-in __call__ method. This method makes it possible for developers to create objects of functions that act as functions.

class Test:

	def __init__(self):
		self.x = 123

	def __call__(self, newval):
		print(f"Old value = {self.x}      New Value = {self.x + newval}")
		self.x = self.x + newval
		
		return self 		#optional return statement


# Create an instance of Test
t = Test()

# This will invoke the __call__ method and output 223
t(100)

 

Data Wrangling

The following code is useful for general data wrangling tasks.

 

Zip Function

The zip() function accepts zero or more iterable data structures (e.g. list, string, dict, etc.), aggregates them into a tuple, and then returns an object.

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

# Result:  [('a', 1), ('b', 2), ('c', 3)]
result1 = list(  zip(list1, list2)  )

# Result:  {('a', 1), ('b', 2), ('c', 3)}
result2 = set(  zip(list1, list2)  )

 

Pandas

Merge two lists into a single Pandas dataframe

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

# Result:  [('a', 1), ('b', 2), ('c', 3)]
result1 = list(  zip(list1, list2)  )

df = pd.DataFrame(data=result1, columns=["Column1", "Column2"])

 

Imaging

The following code represents a few functions that are useful for various tasks.

 

Pillow Images and Numpy Arrays

from PIL import Image, ImageDraw

# open an image
img1 = Image.open("img1.jpg")

# convert image to numpy array
array1 = np.asarray(img1)

# convert numpy array to image
img2 = Image.fromarray(array1)