Skip to content

Instantly share code, notes, and snippets.

View fabsta's full-sized avatar

Fabian Schreiber fabsta

View GitHub Profile

CONDITIONAL STATEMENTS

if statement

if x > 0:
    print 'positive'

if/else statement

if x > 0:

COMPARISONS AND BOOLEAN OPERATIONS

comparisons (these return True)

5 > 3
5 >= 3
5 != 3
5 == 5

FOR LOOPS AND WHILE LOOPS

range returns a list of integers

range(0, 3)     # returns [0, 1, 2]: includes first value but excludes second value
range(3)        # same thing: starting at zero is the default
range(0, 5, 2)  # returns [0, 2, 4]: third argument specifies the 'stride'

DEFINING FUNCTIONS

define a function with no arguments and no return values

def print_text():
    print 'this is text'

call the function

Regex

import re
pattern = re.compile("\w.?\w+@\w[.\w+]")

finds subpatterns

Exceptions

try - except

try:
    image_data = (ndimage.imread(image_file).astype(float) - 
                    pixel_depth / 2) / pixel_depth
      if image_data.shape != (image_size, image_size):
        raise Exception('Unexpected image shape: %s' % str(image_data.shape))
 dataset[num_images, :, :] = image_data

MATH

basic operations

10 + 4          # add (returns 14)
10 - 4          # subtract (returns 6)
10 * 4          # multiply (returns 40)
10 ** 4         # exponent (returns 10000)
10 / 4          # divide (returns 2 because both types are 'int')
10 / float(4) # divide (returns 2.5)

Preliminaries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame, Series
@fabsta
fabsta / spark_basic_build.sbt
Created October 25, 2016 18:02 — forked from vgiri2015/spark_basic_build.sbt
spark_final_build_sbt
name := "Spark2.0-and-greater"
version := "1.0"
//Older Scala Version
scalaVersion := "2.11.8"
val overrideScalaVersion = "2.11.8"
val sparkVersion = "2.0.0"
val sparkXMLVersion = "0.3.3"
@fabsta
fabsta / pandas cheat sheet.md
Created March 5, 2017 21:46
pandas cheat sheet

[TOC]

Preliminaries/Import

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame, Series