I woke up pretty early today. Not particularly sure why, but I just couldn't go back to sleep. So, I decided to get up, run the heater, put on a bad movie (in this case it's "Four Christmases"), and code that Word Jumble program. Here it is:
The book does a good job explaining all the bits and pieces of the code, but I'm not 100% sure if I would be able to write this myself. I think what I'll need to do next is do the challenge sections so that I can code without a cheat sheet!
Here's the first challenge this chapter. I needed to write a program that asked for a start number, end number, and a number to count by from the user and print it out.
Second challenge is to get a word from the user and spell it backwards:
What I found challenging regarding this was how to use range and splicing. I couldn't figure out which way to count and which to do and was getting frustrated in seeing my first attempt having the index being out of range. So what do I do? I decided, let's print out what the 'i' is and see what's going in. After that it was smooooooth sailing. Huzzah!
It's funny, through this experience so far, I got easily frustrated, but thought of a different way to figure out the issue and I would get it right. It's the whole programming and debugging process that's actually making this enjoyable. I guess that's why I liked correcting other people's code. Being able to debug and fix and figure out the issues folks were having and being able to help them out. Well there was the additional helping people aspect that made me feel accomplished? No that's not the word. It's rewarding.
Well that's it for this morning so far. I might do more and another post will come its way.
Hello world and good morning.
A girl's journey to chronicle her adventures in reigniting her inner geek. From getting a new Macbook AIR, learning Python and mobile development, and just plain finding her way back.
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Saturday, October 8, 2011
Thursday, October 6, 2011
Loop de loop
As I'm feeling worse than I was yesterday, I decided. Let's keep going on Python. I know. I'm crazy. Right now the thought of not making Silicon Valley Code Camp is bumming me out since I'm getting sicker by the minute.
Anyways, onto for loops, strings, and tuples!
What I find pretty nice is that the for loop for Python is super flexible. You can use it to traverse a word and it would stop at the end of the word. It also pretty simple and straight forward.
for <variable> in <range>:
DO SOMETHING!
So you can do something like:
for letter in word:
print letter
This will print each letter on a separate line of the word. So simple!
OMG you can do "in" statements too to do comparison. It's like heaven. Built in! So you can say
if "a" in phrase:
print "There's a letter 'a' in your phrase."
I use "in" in SQL all the time. It's like crazy nuts. OH...well you can only use single characters for "in."
Indexing is a little weird. So say:
word = "Flouncy"
print word[-2] --> c
That's right you can do negative indexing. That's just weird to me as it's not standard to what I've learned. But what's crazier is slicing with negative and positive indices.
print word[-4:5] --> un
Get it?
Tuples is just a list, but unlike other languages, it's not restricted to be one type of list. So you can create tuples with integers, strings, candy, muffins, pie...I guess I'm hungry.
I'm actually super sleepy, so I'll have to code this chapter's work another day. Good night world.
Anyways, onto for loops, strings, and tuples!
What I find pretty nice is that the for loop for Python is super flexible. You can use it to traverse a word and it would stop at the end of the word. It also pretty simple and straight forward.
for <variable> in <range>:
DO SOMETHING!
So you can do something like:
for letter in word:
print letter
This will print each letter on a separate line of the word. So simple!
OMG you can do "in" statements too to do comparison. It's like heaven. Built in! So you can say
if "a" in phrase:
print "There's a letter 'a' in your phrase."
I use "in" in SQL all the time. It's like crazy nuts. OH...well you can only use single characters for "in."
Indexing is a little weird. So say:
word = "Flouncy"
print word[-2] --> c
That's right you can do negative indexing. That's just weird to me as it's not standard to what I've learned. But what's crazier is slicing with negative and positive indices.
print word[-4:5] --> un
Get it?
Tuples is just a list, but unlike other languages, it's not restricted to be one type of list. So you can create tuples with integers, strings, candy, muffins, pie...I guess I'm hungry.
I'm actually super sleepy, so I'll have to code this chapter's work another day. Good night world.
Tuesday, October 4, 2011
Completing the while loop...
So I decided to finish off the third chapter and completed the Guessing Game program with while-loop and branch statements. Let's say I took my liberties again on what the text should say:
I'm finding that coding then debugging has been oddly therapeutic. Okay, not really therapeutic, but it's been a great feeling to get it working especially since it's something I wrote and want to make run properly. I know the frustration has yet to hit like during school, but the end result has always been rewarding when everything works.
I haven't really settled on what bigger project I want to write in Python, but hopefully it's something that will help me. I thought about something silly like automating my Radio Alice rewards points activities. It could be interesting.
We'll see...
I'm finding that coding then debugging has been oddly therapeutic. Okay, not really therapeutic, but it's been a great feeling to get it working especially since it's something I wrote and want to make run properly. I know the frustration has yet to hit like during school, but the end result has always been rewarding when everything works.
I haven't really settled on what bigger project I want to write in Python, but hopefully it's something that will help me. I thought about something silly like automating my Radio Alice rewards points activities. It could be interesting.
We'll see...
Monday, October 3, 2011
Still easing into coding...
Okay, I'm not sleepy, so I'll keep going with Python tonight. So far it's still nothing new.
The next code is about learning modules. So here's our good friend random. Random has a function called randrange(n) which takes in an integer and provides a random number starting from 0 to n-1. So since this coding assignment deals with dice then you need to add 1 to the result from the randrange function. Seems simple enough.
Next is about branching and conditional statements. Now here's something interesting. Python recognizes blocks of statements by indenting. So Python forces you to indent if you want them in the same code block. Good practice in my opinion. I hated it when students would not pretty print their assignments, especially in Scheme when you're surrounded by an endless sea of parentheses. At least with Java and C++ you can bracket off areas, but even then you can get away with not indenting. Bravo Python. (I might hate it when I start doing more complex code). Oh fun! If-elif-else! Now that's some fancy naming (or lazy naming).
I guess I am getting tired for the night. I'll have to stop at the while statement and will have to code another night.
*Okay, I did a quick if elif piece of code. Yay for random song references!*
The next code is about learning modules. So here's our good friend random. Random has a function called randrange(n) which takes in an integer and provides a random number starting from 0 to n-1. So since this coding assignment deals with dice then you need to add 1 to the result from the randrange function. Seems simple enough.
Next is about branching and conditional statements. Now here's something interesting. Python recognizes blocks of statements by indenting. So Python forces you to indent if you want them in the same code block. Good practice in my opinion. I hated it when students would not pretty print their assignments, especially in Scheme when you're surrounded by an endless sea of parentheses. At least with Java and C++ you can bracket off areas, but even then you can get away with not indenting. Bravo Python. (I might hate it when I start doing more complex code). Oh fun! If-elif-else! Now that's some fancy naming (or lazy naming).
I guess I am getting tired for the night. I'll have to stop at the while statement and will have to code another night.
*Okay, I did a quick if elif piece of code. Yay for random song references!*
Saturday, October 1, 2011
Slithering through day 2
Going through Chapter 2 right now for Python and nothing's completely out of the ordinary for me regarding Python. String concatenation's straight forward with the + sign.
Some new concepts did pop up though:
I got to create a Greeter program and learned how to use a variable. Let's just say I took some liberty to changing the version which was in the book:
The rest of the chapter covered using entered data as numbers instead of strings using the int() function. Then also knowing when to use + and , when concatenating strings vs. ints. Got through this chapter pretty quickly as the examples I didn't feel like I need to completely do, but just get the gist of. I was able to do the couple pieces of code above just to get a feel of how to do it.
Python is starting to feel pretty powerful with just these simple examples above. Next time I get to learn about branching, while loops, and program planning! Oh joy! <Insert sarcasm here> (I kid, I am having a fun time learning.)
Some new concepts did pop up though:
- '\' is not only an escape character but can be used at the end of any statement so that you can extend a statement to several lines
- Using the multiply symbol (*) and # with a statement repeats the statement # time
- Using triple quotes can save multiple print statements and special characters as it takes whatever you put between the """ verbatim.
- To have two consecutive print statements on the same line you can suppress it by putting a comma at the end of the first print statement.
I got to create a Greeter program and learned how to use a variable. Let's just say I took some liberty to changing the version which was in the book:
The rest of the chapter covered using entered data as numbers instead of strings using the int() function. Then also knowing when to use + and , when concatenating strings vs. ints. Got through this chapter pretty quickly as the examples I didn't feel like I need to completely do, but just get the gist of. I was able to do the couple pieces of code above just to get a feel of how to do it.
Python is starting to feel pretty powerful with just these simple examples above. Next time I get to learn about branching, while loops, and program planning! Oh joy! <Insert sarcasm here> (I kid, I am having a fun time learning.)
Friday, September 30, 2011
Playing with the python...
So after doing a lot of searching regarding what I should learn in terms of how to code again, I keep stumbling upon Python. I know I was pretty adamant before to not learn it, but I keep seeing it in job postings and how much folks find it so useful. So that's where I start. I actually wanted to start a while back and realized it so much more of a hassle to install it on a Windows machine. The Mac has it preinstalled. I simply get to Terminal (now that was an adventure in itself, Lion's very different than Leopard that my mom's iMac is on...I think it's Leopard) then type in python and bam! I'm at the interactive python interpreter (?). Can I say interpreter? It's so Scheme of me.
So what should I use to learn? I decided I had free online books to my disposal and started to look on books24x7. I found a book called "Python Programming for the Absolute Beginner." It talks to my level and well doesn't scare me like other books. It's been pretty nice with small jokes here and there.
My second adventure was how do I write a script? What editor should I use? My good friend GKrimer was online and said they used emacs. Well I am a sucker to go old school and reintroduce myself to my friend emacs. Oh ctrl-k how I missed thee. So what's awesome about emacs is it's also built into Mac. (My Air's been awesome so far, with a few drawbacks which will be explained in another entry of course!) Also the color coding is also already there so huzzah!
My third obstacle was how the hell do I run this script? I started googling how to run a script in emacs, then on mac, and realized, stupid, just type "python <filename>" into the terminal window. And ta-da! Here's the outcome of my first program in 7 years:
(To print screen on a mac it's Cmd + Shift + 4 all at the same time >_<)
I know, go throw up now. But I made Alex run it and he was proud. :)
Not bad for my first time in a long while even though it was following a book. All and all a good start! I'm well on my way to eventually getting into mobile development!
So what should I use to learn? I decided I had free online books to my disposal and started to look on books24x7. I found a book called "Python Programming for the Absolute Beginner." It talks to my level and well doesn't scare me like other books. It's been pretty nice with small jokes here and there.
My second adventure was how do I write a script? What editor should I use? My good friend GKrimer was online and said they used emacs. Well I am a sucker to go old school and reintroduce myself to my friend emacs. Oh ctrl-k how I missed thee. So what's awesome about emacs is it's also built into Mac. (My Air's been awesome so far, with a few drawbacks which will be explained in another entry of course!) Also the color coding is also already there so huzzah!
My third obstacle was how the hell do I run this script? I started googling how to run a script in emacs, then on mac, and realized, stupid, just type "python <filename>" into the terminal window. And ta-da! Here's the outcome of my first program in 7 years:
(To print screen on a mac it's Cmd + Shift + 4 all at the same time >_<)
I know, go throw up now. But I made Alex run it and he was proud. :)
Not bad for my first time in a long while even though it was following a book. All and all a good start! I'm well on my way to eventually getting into mobile development!
Subscribe to:
Posts (Atom)







