Skip to content

Instantly share code, notes, and snippets.

View dazcona's full-sized avatar
🤹‍♂️
Juggling with Data

David Azcona dazcona

🤹‍♂️
Juggling with Data
View GitHub Profile
@dazcona
dazcona / blue.py
Created November 7, 2018 16:57
Determine whether a list contains the string "blue".
# Assume some existing list a, a list of strings.
# Determine whether a contains the string "blue".
a = ["red", "orange", "yellow",
"green", "blue", "indigo", "violet"]
i = 0
while i < len(a) and a[i] != "blue":
i = i + 1
@dazcona
dazcona / add-one-to-list-elements.py
Created November 7, 2018 16:48
Add one to the elements of a list
i = 0
while i < len(a):
a[i] = a[i] + 1
i = i + 1
@dazcona
dazcona / list-iterate-reverse.py
Created November 7, 2018 16:47
Iterating Over the Elements of Lists (Reverse)
i = 0
while i < len(a):
print a[len(a) - i - 1]
i = i + 1
@dazcona
dazcona / list-iterate.py
Created November 7, 2018 16:46
Iterating Over the Elements of Lists
a = ["red", "orange", "yellow",
"green", "blue", "indigo", "violet"]
i = 0
while i < len(a):
print a[i]
i = i + 1
@dazcona
dazcona / mutability-lists.py
Created November 7, 2018 16:45
Mutability Lists
a = ["red", "pink", "yellow",
"green", "blue", "indigo", "violet"]
b = a
a[1] = "orange"
print a[1] # "orange"
print b[1] # also "orange" !
@dazcona
dazcona / mutability.py
Created November 7, 2018 16:43
Strings Mutability
# Strings are immutable.
# Lists are mutable.
# "Immutable" means not able to change, and "mutable" means able to change.
# Given a string s, replace the character at position 4 with a "4".
s = "0123056789"
s = s[:4] + "4" + s[5:]
# Instead, we have to build a new string.
# This distinction is important: in Python, strings are immutable, we cannot change string values, we can, however, create a new string and assign that to s.
@dazcona
dazcona / list-examples-2.py
Created November 7, 2018 16:39
List examples
# In literals:
# we use square brackets denote a list, and the elements are comma separated.
a = [] # An empty list.
b = [1]
c = [3, 2, 1]
d = ["ab", "cd", "ef"]
a = ["red", "orange", "yellow",
"green", "blue", "indigo", "violet"]
@dazcona
dazcona / list-examples.py
Last active November 7, 2018 16:35
Examples of lists in Python
# The syntax for accessing the individual elements of a list is the same as that for accessing the characters of a string.
s = "Mary had a little lamb."
print a[4] # " "
print a[6] # "a"
# The elements of a list can be values of any type.
a = [1, 1, 2, 3, 5, 8, 13, 21]
@dazcona
dazcona / character-classes.py
Created November 7, 2018 16:30
Character classes
s = raw_input()
# Test whether s[i] is a digit.
print "0" <= s[i] and s[i] <= "9"
# Test whether s[i] is not a digit.
print s[i] < "0" or "9" < s[i]
# Test whether s[i] is not a digit (alternative, possibly better).
print not ("0" <= s[i] and s[i] <= "9")
@dazcona
dazcona / is-prime.py
Created November 7, 2018 14:51
Is this number prime?
#!/usr/bin/env python
n = input()
i = 2 # Linear search.
while i < n and n % i != 0: #
i = i + 1 #
if n < 2 or i < n:
print "not prime"