Skip to content

Instantly share code, notes, and snippets.

@CaryInVictoria
CaryInVictoria / farenheit_to_celcius.py
Last active December 25, 2015 17:39
Farenheit to celcius
def farenheit_to_celsius(f):
return ((f-32)*5/9)
f = 70
# Was print(str(f) + 'F -> ' + '%2.1f' % (farenheit_to_celsius(f)) + 'C') # => 70F -> 21.1C
# Now, as per Victor's suggestion:
print('%2.1fF -> %2.1fC' % (f, farenheit_to_celsius(f))) # => 70.0F -> 21.1C
@CaryInVictoria
CaryInVictoria / is_a_vowel.py
Last active December 25, 2015 17:39
is_a_vowel problem
def is_a_vowel(c):
return c.lower() in "aeiou"
# or
def is_a_vowel(c):
return "aeiou".find(c.lower()) != -1
# or
def is_a_vowel(c):
import re # Can be before def
@CaryInVictoria
CaryInVictoria / leap_year.py
Last active December 25, 2015 17:48
leap year problem
def leap_year(y):
if y % 400 == 0:
return True
if y % 100 == 0:
return False
if y % 4 == 0:
return True
return False
# or
@CaryInVictoria
CaryInVictoria / future_value.py
Created October 16, 2013 22:01
Future value problem
def future_value(pv, r, n):
return (pv * (1 + 0.01 * r) ** n)
pv = 100
r = 0.5
n = 12
print("future_value(pv = {0:5.2f}, r = {1:3.2f}, n = {2:2}) = {3:5.2f}".format (pv, r, n, future_value(pv, r, n)))
# => future_value(pv = 100.00, r = 0.50, n = 12) = 106.17
@CaryInVictoria
CaryInVictoria / suggested_changes_part_1.txt
Created October 17, 2013 00:53
Suggested changes to text prior to first four exercises
A Quick Look at Python
p. 5 mention parallel assignment
p. 6 naming convention: lower case
p. 8 Note how the start is always included...+ word[4:] ’Python’
Should be after the following para.
p. 8 delete "Also remember that" before "Python strings cannot be changed - they are immutable".
@CaryInVictoria
CaryInVictoria / buggy_code.py
Last active December 25, 2015 19:49
Buggy Python code. Objective is to spot errors without running.
>>> def get_dow:
>>> dow = input('Enter day of week (0=Sunday, 1=Monday, ...):)'
>>> print ("you entered " + str(dow))
>>> if Dow == 2:
>>> print("It's Tuesday")
>>> else
>>>
>>> # We won't bother
... # checking to see what
... # the day of week
@CaryInVictoria
CaryInVictoria / py_interpreter.py
Created October 18, 2013 05:13
Python Interpreter
>>> def a():
... print("a")
...
>>> a()
a
>>>
>>> def b():
... print("b")
...
... b()