Skip to content

Instantly share code, notes, and snippets.

@adaptives
adaptives / ex7.py
Created August 4, 2011 12:25
Exercise 7
# print a string
print "Mary had a little lamb."
# print a string that has formatting characters in it
print "Its fleece was white as %s." % 'snow'
# print a string
print "And everywhere that Mary went."
# print a string and print it 10 times
@adaptives
adaptives / ex6_b.py
Created August 4, 2011 10:59
Exercise 6 (with comments)
#assign a String to the variable x. The String is a formatted String
#where we replace %d with 10
x = "There are %d types of people." % 10
#assign the variable 'binary' with a value which is a String 'binary'
binary = "binary"
#assign a String to the variable do_not
do_not = "don't"
@adaptives
adaptives / ex6.py
Created August 4, 2011 10:49
Exercise 6 (as is)
#assign a String to the variable x. The String is a formatted String
#where we replace %d with 10
x = "There are %d types of people." % 10
#assign the variable 'binary' with a value which is a String 'binary'
binary = "binary"
#
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
@adaptives
adaptives / ex5_c.py
Created August 4, 2011 10:22
Exercise 5 (with Kilogram and Centimeter conversions)
kg_to_pound = 2.20462262185
inch_to_cm = 2.54
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
height_in_cm = height * inch_to_cm
weight = 180 # lbs
weight_in_kg = weight/kg_to_pound
eyes = 'Blue'
teeth = 'White'
@adaptives
adaptives / ex5_b.py
Created August 4, 2011 10:00
Exercise 5 (without 'my' in variable names)
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print "Let's talk about %s." % name
print "He's %d inches tall." % height
@adaptives
adaptives / ex5_a.py
Created August 4, 2011 09:57
Exercise 5 (as is)
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
@adaptives
adaptives / ex4.py
Created August 3, 2011 08:43
Exercise 4
#Define a variable called cars and give it the integer value 100
cars = 100
#Define a variable called space_in_a_car a nd give it the floating point value 4.0
space_in_a_car = 4.0
#Define a variable called drivers, and give it the integer value 30
drivers = 30
#Define a variable called passengers, and give it the integer value 90
passengers = 90
#Define a variable called cars_not_driven, and compute it as the difference between cars and drivers
cars_not_driven = cars - drivers
@adaptives
adaptives / ex4_incorrect1.py
Created August 3, 2011 08:37
Exercise 4 (Incorrect 1)
#THIS CODE HAS AN ERROR
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
# The meaning of operators in Python
# + plus Addition
# - minus Subtraction
# / slash Division
# * asterisk Multiplication
# / percent Also modulus operator, which returns the remainder from division
# < less-than Returns true if the operans on the LHS is less than RHS
# > greater-than Returns true if the operand on the LHS is greater than RHS
# <= less-than-equal Returns true if the operand on the LHS is less than or equal to RHS
# >= greater-than-equal Returns true if the operand on the LHS is greater than or equal to the RHS
@adaptives
adaptives / LPTHW - Exercise 2
Created August 2, 2011 06:50
LPTHW - Exercise 2
# A comment, this is so you can read your program later.
# Anything after the # is ignored by Python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."