Thursday, April 17, 2014

Learn Python the Hard Way by Zed Shaw - Exercises, Questions and Problems - Mon 14 - Sun 20 April 2014

FIRST SESSION 13/04/2014 


###LEARN PYTHON THE HARD WAY

###EXERCISE ONE V1. RAN SUCCESSFULLY 13/4/2014 SM//
print("Hello World!")
print ("Hello Again")
print ("I like typing this")
print ("This is fun.")
print ("Yay! Printing.")
print ("I'd much rather you 'not'.")
print ('I "said" do not touch this.')


###EXERCISE ONE V2. RAN SUCCESSFULLY 13/4/2014 SM

print ("Hello World!")
print ("Hello Again")
print ("I like typing this")
print ("This is fun.")
print ("Yay! Printing.")
print ("I'd much rather you 'not'.")
print ('I "said" do not touch this.')
print ("That's it, it's GO time!") ### <--- ADDED THIS LINE

##### COMPLETE #####

###EXERCISE TWO V1. RAN SUCCESSFULLY 13/4/2014 SM//

# 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."

# READ THIS BACKWARDS AND OUT LOUD FOR ERROR CORRECTION


###EXERCISE 3 NUMBERS AND MATH V1. RUN SUCCESSFULLY 13/4/2014###

print("I will now count my chickens:")

print("Hens"), 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4

print("Now I will count the eggs:")

print("3 + 2 + 1 - 5 + 4 %")

print("Now I will count the eggs.")

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print("Is it true that 3 + 2 < 5 - 7?")

print 3 + 2 < 5 - 7

print("What is 3 + 2?"), 3 + 2
print("What is 5 - 7?"), 5 - 7

print("Oh, that's why it's False.")

print("How about some more.")

print("Is it greater?"), 5 > -2
print("Is it greater or equal?"), 5>= -2
print("Is it less or equal?"), 5 <= -2



###EXERCISE 4 V1. VARIABLES AND NAMES - successfully run on 13/04/2014###



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


print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."

###EXERCISE 4 V2. VARIABLES AND NAMES - successfully run on 13/04/2014###

#defines the number of cars
cars = 100

#defines the capacity (in terms of the number of passengers, to one decimal place), available per car.
space_in_a_car = 4.0

#defines the total number of drivers
drivers = 30

#defines the total number of passengers
passengers = 90

#defines the number of cars that are not being driven (as the difference between cars and drivers).
cars_not_driven = cars - drivers

#defines the number of drivers (as the number of cars_driven
cars_driven = drivers

#defines the total capacity for carpooling as a number of passengers (being the product of the number of cars driven by the average space in each car)
carpool_capacity = cars_driven * space_in_a_car

#defines the average number of passengers per car (as the number of passengers divided by the number of cars driven)
average_passengers_per_car = passengers / cars_driven

print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."

###EXERCISE 5 - MORE VARIABLES AND PRINTING ####

## coding: utf-8 ##
###DEFINE VARIABLES###
name = "Sean McMahon"
age = 31
height = 194
weight = 92
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'

###WRITE CODE###
print "Let's talk about %s." % name
print "He's %d cms tall." % height
print "He's %d kgs heavy." % weight
print "That's pretty heavy..."
print "He's got %s eyes and %s hair." % (eyes, hair)
print "His teeth are usually %s depending on the coffee." % teeth

# this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." %(age, height, weight, age + height + weight)


##END EXERCISE 5 ##

END SESSION ONE: 13 APRIL 2014.

###EXERCISE 6 ###

#EXERCISE 5 MORE VARIABLES AND PRINTING

#coding: utf-8
###DEFINE VARIABLES###
name = "Sean McMahon"
age = 31
height = 194
weight = 92

teeth = 'White'
hair = 'Brown'
income = 'USD $1,000,000,000'
resting_HR = 50
body_temp = 37

###WRITE CODE###
print "Let's talk about %s." % name
print "He's %d cms tall." % height
print "He's %d kgs heavy." % weight
print "That's pretty heavy..."
print "He's got %s eyes and %s hair." % (eyes, hair)
print "His teeth are usually %s depending on the coffee." % teeth
print "His annual income varies, but it is usually approximately %s." % income, "That's right.  One birrion dorraaaaaz."
print "He's a cool cucumber.  His resting heart rate is only %s." % resting_HR
print "He might be cool, but he's a bit hot blooded.  His body temperature is %s." % body_temp

# this line is tricky, try to get it exactly right

print "If I add %d, %d, and %d I get %d." %(age, height, weight, age + height + weight)

### EXERCISE 6 - STRINGS ###

###Defines the variable X as the number of different types of people there are
x = "There are %d types of people." % 10

###Defines the variable binary
binary = "binary"

###Defines the variable "do_not"
do_not = "don't"

###Defines the variable y as two types of people (as two variables: %s, %s)
y = "Those who know %s and those who %s." % (binary, do_not)

#Prints the variable x
print x

#Prints the variable y
print y

#Prints I said (a variable)
print "I said: %r." % x

#Prints I also said (a variable)
print "I also said: '%s' ." % y

#Defines the variable "hilarious" as "false"
hilarious = False

#Defines the variable "joke_evaluation" as "Isn't that joke so funny!?"
joke_evaluation = "Isn't that joke so funny?! %r"

#Prints the variable "joke_evaluation" and returns a variable (hilarious) as the answer
print joke_evaluation % hilarious

#
w = "This is the left side of..."
e = "a string with a right side."

print w + e

###############

###EXERCISE 7


# PRINTS "Mary had a little lamb."
print "Mary had a little lamb."

# PRINTS Its fleece was white as (VARIABLE)." and defines the variable as "snow"
print "Its fleece was white as %s." % 'snow'

# PRINTS "And everywhere that Mary went."
print "And everywhere that Mary went."

#PRINTS a line of dots
print "." * 10 # what'd that do?

#The next 12 lines define 12 variables (end1 - end12) as each of the letters from "Cheese Burger"
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

# watch that comma at the end. try removing it to see what happens

#PRINTS the 12 variables consecutively on the same line, but separates them,
in the input window for the sake of clarity.

print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 +end10 +end11 +end12

###EXERCISE 8 PRINTING, PRINTING###

formatter = "%r %r %r %r"

print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)




### Exercise 9: Printing, Printing, Printing###

days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

print  "Here are the days: ", days
print "Here are the months: ", months

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

### Exercise 10:

tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

#Testing escape sequences BELOW:

Print "Show me the backslash \\t"




######### Coding Journal #########

** Reading "Learn Python the Hard Way" by Zed Shaw

** Attempted to install Python but insufficient disk space

** Switched to cloud based solution (https://www.pythonanywhere.com)

** 13 APR 2014 (12hr on Learn Python the Hard Way)

** 14 APR 2014 (10hr on Learn Python the Hard Way)

** 15 APR 2014 (9 am - 11 am reading including Full Stack Development:
http://www.laurencegellert.com/2012/08/what-is-a-full-stack-developer/

)








MISTAKES & QUESTIONS


Q. Query questions from Ex 6 Learn Python the Hard Way 

Q. How to include Japanese characters in Python?

Q. Exercise 10 -> how to use the ESCAPE SEQUENCES? They do not work in my compiler.

Q. Exercise 10 -> On line 3 why does it print an additional quotation mark?

Q. Exercise 10 - confirm the following escape functions: bell (/a); backspace (\b); \f; and all subsequent

Q. Exercise 11 - 

I had been using IDEONE (an online compiler) which may be responsible for problems in running the code from ex. 11.  Will try again after installing a compiler on my laptop.


M. Transcription error - wrote "1" on the first line, causing a SyntaxError

M. Transcription error - wrote ' instead of \ causing Syntax Error