Study Drills resolutions from “Learn Python the hard way”

I have been practicing more details relating to Python and this time I want to summarize all the drills or little challenges that the Website provides:

Exercise 1: A Good First Program

  1. Make your script print another line.
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 'What about "this new line"'

2. Make your script print only one of the lines.

#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 'What about "this new line"'

3. Put a # (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.

#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 'What about "this new line"'

Exercise 2: Comments and Pound Characters

  1. Find out if you were right about what the # character does and make sure you know what it’s called (octothorpe or pound character).
  2. Take your ex2.py file and review each line going backward. Start at the last line, and check each word in reverse against what you should have typed.
  3. Did you find more mistakes? Fix them.
  4. Read what you typed above out loud, including saying each character by its name. Did you find more mistakes? Fix them.
print "This will run."
#print "This won't run."
#You can also use a comment to "disable" or comment out a piece of code:
print "I could have code like this." # and the comment after is ignored
#Anything after the # is ignored by python.
#A comment, this is so you can read your program later.
  • The output was different from ex2.py by just switching the order of the sentences.

Exercise 3: Numbers and Math

  1. Above each line, use the # to write a comment to yourself explaining what the line does.
#This will print a message to introduce my counting method for chickens 
print "I will now count my chickens

2. Remember in Exercise 0 when you started python? Start python this way again and using the math operators, use Python as a calculator.

python
>>> 3 == 3 > 3 == 3
False
>>> print "The square of 5 is",5**2
The square of 5 is 25

3. Find something you need to calculate and write a new .py file that does it.

4. Notice the math seems “wrong”? There are no fractions, only whole numbers. You need to use a “floating point” number, which is a number with a decimal point, as in 10.5, or 0.89, or even 3.0.

5. Rewrite ex3.py to use floating point numbers so it’s more accurate. 20.0 is floating point.

print "Is it greater?", 5.0 > -2
print "Is it greater or equal?", 5.0 >= -2
print "Is it less or equal?", 5.0 <= -2

Exercise 4: Variables And Names

  1. I used 4.0 for space_in_a_car, but is that necessary? What happens if it’s just 4?
  • The fourth output changed with this message “We can transport 120 people today.”

2. Remember that 4.0 is a floating point number. It’s just a number with a decimal point, and you need 4.0 instead of just 4 so that it is floating point.

3. Write comments above each of the variable assignments.

4. Make sure you know what = is called (equals) and that it’s making names for things.

5. Remember that _ is an underscore character.

6. Try running python from the Terminal as a calculator like you did before and use variable names to do your calculations. Popular variable names are also i, x, and j.

python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 3.0
>>> y = x + 3
>>> print x - y
-3.0

Exercise 5: More Variables and Printing

  1. Change all the variables so there is no my_ in front of each one. Make sure you change the name everywhere, not just where you used = to set them.
    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 
    print "He's %d pounds heavy." % weight
    print "Actually that's not too 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)

    2. Try to write some variables that convert the inches and pounds to centimeters and kilograms. Do not just type in the measurements. Work out the math in Python.

name = 'Zed A. Shaw' 
age = 35 #not a lie
height = 74 * 2.54 #inches to cm
weight = 180 * 0.4536 #lbs to kg
eyes = 'Blue'
teeth = 'White'
hair = 'Brown' 
print "Let's talk about %s." % name
print "He's %f cm tall." % height 
print "He's %f kg heavy." % weight
print "Actually that's not too 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, %f, and %f I get %f." %(age,height,weight,age+height+weight)

3. Search online for all of the Python format characters.

The format characters of Python are following by % like %s %f %d and then before variables as %  eyes.

4. Try more format characters. %r is a very useful one. It’s like saying “print this no matter what.”

name = 'Zed A. Shaw' 
age = 35 #not a lie
height = 74 * 2.54 #inches to cm
weight = 180 * 0.4536 #lbs to kg
eyes = 'Blue'
teeth = 'White'
hair = 'Brown' 
print "Let's talk about %r." % name
print "He's %r cm tall." % height 
print "He's %r kg heavy." % weight
print "Actually that's not too heavy."
print "He's got %r eyes and %r hair." % (eyes,hair) 
print "His teeth are usually %r depending on the coffee" % teeth
#this line is tricky, try to get it exactly right 
print "If I add %r, %r, and %r I get %r." %(age,height,weight,age+height+weight)

Exercise 6: Strings and Text

  1. Go through this program and write a comment above each line explaining it:
#It's going to print 10 types of people instead of saying 2 in dec
x= "There are %d types of people" % 10
#String variables as binary and do_not are going be stored
binary = "binary"
do_not = "don't"
#This new message is going to be stored in y 
y = "Those who know %s and those who %s." % (binary, do_not)
#Printing x and y to show the previous messages with the variables
print x
print y
#x is going to print and use %r instead of %s, y keep its format
print "I said: %r" %x
print "I also said: %s" %y
#hilarious is setting with boolean value
hilarious = False
#Define a variable which contains a sentence and waits for a value
joke_evaluation = "Isn't that joke so funny?! %r"
#Print the call of joke_evaluation with the hilarious value: False
print joke_evaluation % hilarious
#Set two sentences and stored in variables w and e
w = "This is the left side of ... "
e = "a string with a right side"
#Join the two previous sentences stored in w and e, shows together
print w + e

2. Find all the places where a string is put inside a string. There are four places.

y = "Those who know %s and those who %s." % (binary, do_not)
print "I said: %r" %x
print "I also said: %s" %y
joke_evaluation = "Isn't that joke so funny?! %r" 
print joke_evaluation % hilarious

3. Are you sure there are only four places? How do you know? Maybe I like lying.

I think, there are five sentences inside other sentence.

4. Explain why adding the two strings w and e with + makes a longer string.

Because + join in this case, two strings.

Exercise 7: More Printing

  1. Go back through and write a comment on what each line does.
#printing a sentence related to Mary and her little lamb
print "Mary had a little lamb"
print "Its fleece was white as %s " % 'snow'
print "And everywhere that Mary went"
print "."* 10 # this print 10 point, because . is multiplied by 10
#In this case a character is going to be stored in a variable end
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
#A comma at the end allows the next characters be in the same line
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12

2. Read each one backward or out loud to find your errors.

3. From now on, when you make mistakes, write down on a piece of paper what kind of mistake you made.

I missed the ‘ ‘ when I wrote the snow variable in the third line.

4. When you go to the next exercise, look at the mistakes you have made and try not to make them in this new one.

5. Remember that everyone makes mistakes. Programmers are like magicians who fool everyone into thinking they are perfect and never wrong, but it’s all an act. They make mistakes all the time.

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 %(
     "I had this thing",
     "That you could type up right",
     "But it didn't sing.",
     "So I said goodnight."
)
  1. Do your checks, write down your mistakes, and try not to make the same mistakes on the next exercise.  – I missed the last parenthesis at the end of the example

2. Notice that the last line of output uses both single-quotes and double-quotes for individual pieces. Why do you think that is?   – I do not know –        This is my output  –>

1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'I had this thing' 'That you could type up right' "But it didn't sing." 'So I said goodnight.'
  • Finally, I was able to accomplish my first little Python challenge in minutes:

Anagrams

def anagrama(L):
    l = []
    for i in range(len(L)):
       l.append(set(L[i]))
    if all( x == l[0] for x in l):
      print "It is an anagram"
    else:
      print "It is not anagram"

anagrama(['naaa','ana','annn','naaan'])

 

About Julita Inca

Ingeniero de Sistemas UNAC, Magíster en Ciencias de la Computación PUCP, OPW GNOME 2011, Miembro de la GNOME Foundation desde el 2012, Embajadora Fedora Perú desde el 2012, ganadora del scholarship of the Linux Foundation 2012, experiencia como Admin Linux en GMD y Especialista IT en IBM, con certificaciones RHCE, RHCSA, AIX 6.1, AIX 7 Administrator e ITILv3. Experiencia académica en universidades como PUCP, USIL y UNI. HPC researcher, a simple mortal, like you!
This entry was posted in τεχνολογια :: Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s