Skip to content

Instantly share code, notes, and snippets.

@AglaianWoman
Created October 26, 2017 01:40
Show Gist options
  • Save AglaianWoman/5739e613f541de90f0eed6f16be8cb92 to your computer and use it in GitHub Desktop.
Save AglaianWoman/5739e613f541de90f0eed6f16be8cb92 to your computer and use it in GitHub Desktop.
MITx: 6.00.1x Introduction to Computer Science and Programming Using Python
#
### Week 1: Python Basics
#
#
# 1. Introduction to Python
#
#
# 2. Core Elements of Programs
#
#
### Week 2: Simple Programs
#
#
# 3. Simple Algorithms
#
# Reviewing Strings
s = "abc"
# 012 indexing starts at 0
len(s) # 3
s[0] # a
s[1] # b
s[3] # index out of bounds error!
# Slice string with [start:stop:step]
s = "abcdefgh"
s[::-1] # hgfedcba
s[3:6] # def
s[-1] # h
# Strings are immutable
s = "hello"
s[0] = 'y' # error!
s = 'y' + s[1:len(s)] # is allowed; creates a new object
# Strings and Loops
s = "abcdefgh"
for index in range(len(s)):
if s[index] == 'i' or s[index] =='u':
print("There is an i or u")
# Can also be written
for char in s:
if char == 'i' or char == 'u'
#
# 4. Functions
#
""" Decomposition break problem into different, self-contained, pieces. Abstraction suppresses details of method to compute something
from use of that computation
Create structure with decomposition. Divide code into modules that are self-contained, used to break up code, intended to be
reusable, keep code organized and keep code coherent. Suppress details with abstraction.
Suppress details with abstraction. Think of a piece of code as black box that cannot see details, does not need/want to see details,
and hides details.
Functions have: name, parameters, docstring and body. Returns None if no return statement is given.
Keyword arguments can have default values to use if no value is supplied. """
def printName(firstName, lastName, reverse = False):
# some code
printName('Eric', 'Grimson') # is a valid call
""" Recursion is a programming technique where a function calls itself. Must have one or more base cases that are easy to solve. """
# Multiplication example
def mult(a, b):
if b == 1:
return a
else:
return a + mult(a, b-1)
# Factorial example
def factorial(n):
if n == 1:
return 1
else:
return n* factorial(n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment