if so

k = 130
m = 7655

if k > 42:
    print 'Hi'
elif m > 43:
    print 'Hello'
else:
    print 'Bye'
======================== RESTART: C:/Python27/test.py ========================
Hi
>>> 
import numpy

n = numpy.zeros(5)
n[0] = 20.
n[4] = 99.

print n
print n[-1]
import numpy

p = numpy.zeros([2, 3])

p[0, 1] = 7.
p[1, 2] = 8.

print p
p = 1. + 2. * p

print p
def three_times(u):
    r = 2 * u
    return r + u

print three_times(1), three_times(7)
======================== RESTART: C:/Python27/test.py ========================
3 21
>>> 
import math
from xxx import *

def sin_cos():
    num_points = 50

    x = numpy.zeros(num_points)
    sin_x = numpy.zeros(num_points)
    cos_x = numpy.zeros(num_points)

    for i in range(num_points):
        x[i] = 2. * math.pi * i / (num_points - 1.)
        sin_x[i] = math.sin(x[i])
        cos_x[i] = math.cos(x[i])

    return x, sin_x, cos_x

x, sin_x, cos_x = sin_cos()

@show_plot
def plot_me():
    matplotlib_pyplot.plot(x, sin_x)
    matplotlib_pyplot.plot(x, cos_x)
plot_me()

Python Playground

import math

f = math.sqrt(4)
g = radius_of_earth * math.sin(math.pi / 180.0 * 23.4)
g = radius_of_earth * math.sin(math.pi / 180.0 * 23.4)

print f, g
======================== RESTART: C:/Python27/test.py ========================
2.0 2530229.21123
>>> 
for h in range(7):
    i = h**2
    print i
======================== RESTART: C:/Python27/test.py ========================
0
1
4
9
16
25
36

k = 42
m = 43

while k < 130 or m == 140: k += 1 m += k print k, m [/python]

======================== RESTART: C:/Python27/test.py ========================
130 7655
>>> 

velocity

Velocity = Derivative = Rate of Change

v(t)=dx(t)/dt = x'(t) = x(t)
x(t)=(t+1)^3

(t+h+1)^3 – (t+1)^3 /h

Newton’s Second Law of motion
Force = mass * αacceleration
V = m/s / s = m / s^2

answer = 42
radius_of_earth = 6.371e6
#test number = 43.0
#13test = 44.4

a = 3 * answer + 4
b = radius_of_earth ** 2 + (a + 3) ** 0.5
answer += 7

c = 5 / 4
d = 5 / 4.
e = 1.0 * 5 / 4

print a, b, answer, c, d, e
======================== RESTART: C:/Python27/test.py ========================
130 4.0589641e+13 49 1 1.25 1.25
>>> 

Houston We Have a Problem

Apollo 13
TLI, MCC-2, DPS-1

what is important?
mass of the spacecraft, size of the moon, motion of the moon, size of the earth, motion of the earth, 3D

sine & friends
b/c = sinβ
a/c = cosβ

60°=1rad、0.1rad=6°、sin40°=6.4
sin(40)=0.6427876096865393

Motion of the moon around the earth
t = time
4.10^8 2πt*27days

Closures

Closures include:
global functions, nested functions, and closure expressions

Nested functions
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID178

closure expression- an unnamed, self contained block of code
closure expression are used to specify an action to be executed some time in the future.

var bids = [48, 75, 63, 52, 68]
var orderedBids = bids.sort( {(bid1: Int, bid2: Int) -> Bool in
	return bid2 > bid1
})

print(orderedBids)

Closures typically take the form:
{(parameters) -> return type in
statements to execute
}

var birthYears = [2004, 2011, 2007, 2005, 2002]
var reverseChronologicalYears = birthYears.sort({ (year1: Int, year2: Int) -> Bool interface EarthquakeActivity extends Parent {
	return year1 > year2
}
})

Protocols and Extensions

Protocols

Adopting a protocol is like signing a contract to implement every method on the list.

import UIKit

protocol Souschef {
	func chop(vegetable: String) -> String
	func rinse(vegetable: String) -> String
}

class Roommate: Souschef {
	var hungry = true
	var name: String

	init(hungry:Bool, name: String){
		self.hungry = hungry
		self.name = name
	}

	func chop(vegetable: String) -> String {
		return "She's choppin' \(vegetable)!"
	}

	func rinse(vegetable: String) -> String {
		return "The \(vegetable) is so fresh and so clean"
	}
}
class DinnerCrew {
	var members: [Souschef]

	init(members: [Souschef]){
		self.members = members
	}
}
protocol DirtyDeeds {
	func cheat()
	func steal()
}

class Minion: DirtyDeeds {
	var name: String

	init(name:String){
		self.name = name
	}

	func cheat(){
		println("Mwa haha!")
	}

	func steal(){
		println("Mwa haha!")
	}
}

class DinnerCrew {
	var members: [Souschef]

	init(members: [Souschef]){
		self.members = members
	}
}

protocol Souschef {
	func chop(vegetable: String) -> String
	func rinse(vegetable: String) -> String
}

var deviousDinnerCrew = DinnerCrew(members: [Minion]())

Andrew Bancroft’s Swift blog
http://www.andrewcbancroft.com/

Enums and Structs

Enums and Structs
{
case red
case blue
case green
}

import UIKit

enum PrimaryColor {
	case red
	case blue
	case yellow
}
enum Aunties {
	case Aime, Billie, Diane, Gail, Janie, Pam
}
enum AmericanLeagueWest: String {
	case As = "Oakland"
	case Astros = "Houston"
	case Angels = "Los Angeles"
	case Mariners = "Seatle"
	case Rangers = "Arlington"
}
enum CaliforniaPark {
	case Yosemite, DeathValley, Lesson, Sequoia
}

var warning = ""
var destination = CaliforniaPark.Yosemite

switch destination {
	case.Yosemite:
		warning = "Beware of agressive bears!"
	case.DeathValley:
		warning = "Beware of dehydration!"
	case.Lasson:
		warning = "Watch out for boiling pools!"
}
struct PictureFrame {
	var width = 5
	var height = 7
	var thickness: Double = 1.5

	var area: Int {
		get {
			return width * height
		}
	}
}
var frame = PictureFrame(width: 3, height: 5, thickness: 0.5)
var frameForMom = frame
frameForMom.width = 5
frameForMom.height = 7
frame.width
frame.height

class ClassyPictureFrame {
	var width = 5
	var height = 7
	var thickness: Double = 1.5

	var area: Int {
		get {
			return (width * height) / 2
		}
	}

	init(width: Int, height: Int, thickness: Double){
		self.width = width
		self.height = height
		self.thickness = thickness
	}
}

var classyFrame = ClassyPictureFrame(width: 3, height: 5, thickness: 0.5)
var classyFrameForMom = classyFrame
var darkColors = Laundry(temperature: 70, speed: "medium")
var darkDelicates = darkColors
darkDelicates.speed = "low"
darkColors.speed
 Triangle {
 	let angles = [30, 60, 90]
 	let sides = [3,4,5]
 }

 UIImagePickerControllerSourceType : Int {
 	case PhotoLibrary
 	case Camera
 	case SavedPhotosAlbum
 }

 struck Name {
 	var firstName: String
 	var lastName: String
 }

 enum Subject {
 	case Math
 	case English
 	case Spanish
 	case Science
 }

Classes, Properties, and Methods

import UIKit

class Movie {
	let title: String
	let director: String
	let releaseYear: Int

	init(title: String, director: String, releaseYear: Int){
		self.title = title
		self.director = director
		self.releaseYear = releaseYear
	}
}
class MovieArchive {
	var movies:[Movie]

	func filterByYear(year:Int) -> [Movie]{
		var filteredArray = [Movie]()
		for movie in self.movies {
			if movie.releaseYear == year {
				filteredArray.append(movie)
			}
		}
		return filteredArray
	}

	init(movies:[Movie]){
		self.movies = movies
	}
}
import UIKit

class ViewController: UIViewController {

	var count = 0
	var label:UILabel!

	override func viewDidLoad(){
		super.viewDidLoad()

		// Label
		var label = UILabel()
		label.frame = CGRectMake(150, 150, 60, 60)
		label.text = "0"
		self.view.addSubview(label)
		self.label = label
		// Button
		var button = UIButton()
		button.addTarget(self, action: "incrementCount", forControlEvents: UIControlEvents.TouchUpInside)
		self.view.addSubview(button)
		button.frame = CGRectMake(150, 250, 60, 60)
		button.setTitle("Click", forState: .Normal)
		button.setTitleColor(UIColor.blueColor(), forState:.Normal)
	}

	@IBAction func incrementCount(){
		self.count++
		self.label.text = "\(self.count)"
	}
}

Access Control
Level, Class, App/Framework, Work
public: Y Y Y
internal: Y Y N
private Y N N

class Movie {
	let title: String
	let director: String
	let releaseYear: Int

	init(title: String, director: String, releaseYear: Int){
		self.title = title
		self.director = director
		self.releaseYear = releaseYear
	}
}

var thursdayNightMovie = Movie(title: "Point Break", director: "Kathryn Bigelow", releaseYear: 1991)
thursdayNightMovie.title

Functions

Global Functions
the functions print, min, and abs are a few examples of global functions.

print("I'm a global function!", terminator:"")

var initialPrice = 50
var bestOffer = 45
var finalPrice = min(bestOffer, initialPrice)

var negativeSeven = -7
abs(negativeSeven)

Anatomy of a function

let array = ["A", "13", "B", "5", "87", "t", "41"]

class Arithmetic {
	func sumOfStrings(aBunchOfStrings: [String]) -> Int {
		let array = aBunchOfStrings
		var sum = 0
		for string in array {
			if Int(string) != nil {
				let intToAdd = Int(string)!
				sum += intToAdd
			}
		}
		return sum
	}
}

func functionName (parameterName: parameterType) -> returnType
statements to execute
return object
}

let stringToReverse = "Mutable or Immutable? That is the question."

func reverseString(stringToReverse: String){
	var reversedString = ""
	for character in stringToReverse.characters {
		reversedString = "\(character)" + reversedString
	}
	print(reversedString)
}

func functionName(externalParamName localParamName: paramType) -> returnType {
statements to execute
return object
}

func firstCharaterOf(word: String) -> Character {
	return word[word.startIndex]
}

firstCharacterOf("Mom")
func placeFirstLetterLast(var myString: String) -> String {
	myString.append(firstCharacterOf(word: myString))
	myString.removeAtIndex(myString.startIndex)
	return "placeholder"
}

placeFirstLetterLast("Mom")
class MovieArchive {
	func filterByYear(year: Int, movies: Dictionary<String, Int> -> [String]{
		var filteredArray = [String]()
		for (movie, releaseYear) in movies {
			if year == releaseYear{
				filteredArray.append(movie)
			}
		}
		return filteredArray
	})
}

var aiThemedMovies = ["Metropolis":1927, "2001:A Space Odessey":1968, "Blade Runner":1982, "War Games":1983, "Terminator":1984, "The Matrix":1999, "A.I.":2001, "Her":2013, "Ex Machina":2015]

var myArchive = MovieArchive()
myArchive.filterByYear(2013, movies: aiThemedMovies)

Switch statement

var birthYear = 1992

if birthYear == 1992 || birthYear == 1980 || birthYear == 1968 {
	println("You were born in the year of the monkey.")
} else if birthYear == 1991 || birthYear == 1979 || birthYear == 1967 {
	println("You were born in the year of the goat")
} else {
	println("You were born in some other animal's year.")
}

switch birthYear {
	case 1992, 1980, 1968:
	println("You were born in the year of the monkey.")
	case 1991, 1979, 1967:
	println("You were born in the year of the goat")
	default:
	println("You were born in some other animal's year.")
}