The Professional Steve

The very important blog of a professional Computer Science undergrad and professional dork

Getting user input in Python

Last time I showed you how to use variables to vary the output of your programs. But that’s only part of it. You and I both know the magic happens when you can get user input. Awww yeah.

Open up a text editor and type the following:

user_input = raw_input()
print user_input

Now save it as input.py, navigate to the directory you saved it in, and type

python input.py

You should now see…. nothing!


The point is to get your input after all. Finally, you can make the computer listen to you. Go ahead and type whatever you want. I’m going to type “potato pancakes”.

potato pancakes

Now hit the enter key and watch what happens

potato pancakes
potato pancakes

Did you see that?! You have now programmed your own virtual parrot! Let’s try it a few more times:

Cookies are an all-the-time food
Cookies are an all-the-time food
I am so great!
I am so great!

You see! The computer has no choice! It has to repeat whatever you say!!

Variables in Python

In my previous post you learned about how to print “hello world”, but that’s a bit boring. I’m sure you want to learn to make programs whose outputs vary. That’s where vaiables come in.

Open up a new file called variables.py and type the following

greeting = "hello world"
print greeting

Now open the terminal, navigate to the directory that has the file, and type

python varables.py

Your output should be:

hello world

Well, that’s useless, I hear you saying. Ok Smarty McSmartypants, try changing your code to this!

greeting = "hello world"
print greeting
greeting = "Live long and prosper"
print greeting

Your new output should be:

hello world
Live long and prosper

Wha-wa-wuh? How can the lines 1 and 3 here do different things when they’re exactly the same?

greeting = "hello world"
print greeting
greeting = "Live long and prosper"
print greeting

It’s because the word “greeting” here is what’s called a variable. Think of it as a bucket whose contents vary.

For example, the following code:

greeting = "hello world"
print greeting
greeting = "Live long and prosper"
print greeting
greeting = "There's a hole in mah bucket"
print greeting
greeting = "Dear Liza, dear Liza"
print greeting
greeting = "There's a hole in mah bucket, dear Liza, a hole."
print greeting

Yields this output:

hello world
Live long and prosper
There's a hole in mah bucket
Dear Liza, dear Liza
There's a hole in mah bucket, dear Liza, a hole.

Think like a business owner

I can be pretty dumb about this kind of thing. I don’t think like a business person, I think like,  well not even a student or a scholar. I think, “Wouldn’t it be cool if I wrote an Android app in Scala? Or Haskell? Or Erlang?” And then I sit and dream, or go through tutorials for hours and hours.

Even if I do start a project (doomed from the start to never be finished), it’s seldom in a language or framework I already know. Or if I’m sensible about that for once, I spend too much time building something complicated when I should make the simplest app I can think of in the simplest and quickest way I can.

A business person looks at the assets they currently have and then figures out a way to turn them into money, using the least amount of time and resources.

Why am I spending time trying to build and install scaloid project when I could follow an Android tutorial and install a working “hello world” onto my phone in an hour or two?

Yes, we should absolutely learn new things, otherwise we’re all destined to be the future’s equivalent of today’s Cobol programmers. But we also need to leverage the skills we already have and make stuff now.

Hello world in python

This post assumes you’ve got a linux machine and know the slightest bit about how to get around in it.

Go to your terminal and type the following:

sudo apt-get install python.

Open your favorite text editor. (Gedit is fine for now.) Create a new file named “hello.py” and type this:

print "hello world"

Now save the file.

In the terminal, navigate to the directory that has your hello.py and type the following:

python hello.py

You should get this output:

hello world

Congratulations. You are now a python programmer. Now go forth and google inputs and if statements!

GO FORTH!!

How to make time for blogging

This post assumes you’re a loud mouth like me.

If the most writing you’ve ever done is in school you may have the wrong idea about it.

Writing is talking on a page.

I know you might be worried about rules, grammar, and all that crap. It’s not that important. Sure, if you want to be a novelist you might want to look into it. But this is blogs we’re talking about. It’s no big deal.

Writing is just talking on a page.

If you’re a loud mouth like me, you can’t stop talking. If you want to blog, just write the same way you talk. Give yourself 15-30 minutes max per post. Write about a paragraph’s worth. Write it just like you were talking to a friend.

Write each word, one after another, in the same way you talk, one word after another.

Don’t worry about grammar, or if it makes sense, just put your mind in the same state as if you were talking to someone. Someone who is patiently listening to everything you say, and will wait until you’re finished.

It’s a loud mouth’s dream.

But. But. Once you’re done, read what you’ve written and ask yourself if it makes sense. If someone comes across the post you’ve just written, and they don’t know you or how you think or how you talk, are they going to understand what you’ve said? If not, edit it until they would. Don’t stress too much over the details. Just edit enough so it makes sense.

Do that a bunch of times. Congrats, now you’re a blogger. Remember that each post doesn’t have to be long. The shorter the better, really.

Of course you can always get better at writing. The learning never stops, but this is great way to start.

Do it faster

I was a lazy kid. Something my mom used to say, when we needed to get ready and go somewhere or do something, was “every time you think about it hurry!” I tried, after a fashion, to follow this advice, when I wasn’t in the mood to be even more obstinate and lazy than usual.

Now I think about it a lot. And every time I do, I hurry.

I’ve made mention of the fact that I’m a big fan of the Self publishing Podcast and one of the things they often talk about is writing speed. They write thousands of words a day. They time themselves, track their stats, hit their word count goals, and then raise the bar.

Far from reducing writing quality they believe this improves it. Going fast forces them to turn off their internal editor (which they later turn on during the editing phase, much more useful ;) ) and to get into a flow state.

Thing is, this doesn’t just apply to writing. We should be trying to achieve speed and a flow state in all our activities. In Stephen King’s On Writing he says that he writes as fast as he can while still remaining comfortable. I think speed can be like that. Of course you’re comfortable doing something at a slow speed, but if you go fast enough and focus more, you’ll find another valley of comfort at a higher level of speed and focus. With practice this level of speed and attention can get ever higher.

I think The Pomodoro Technique dovetails quite nicely with this approach. Spend 25 minutes working as hard and fast as you can, then rest for 5. After you do that 4 times, the 4th break is 20.

Doooo eeeet

Auto-generate html tags in vim

This post assumes you’ve been using vim for a bit but doesn’t assume you’re some kind of expert.

So, I’ve been writing a lot a posts. I usually write them in vim and I really don’t want to have to write out html markup every time I want to put a link in my post. Turns out, there’s a simple script for that.

Go to your terminal and type the following:

vim ~/.vimrc

Make a new line in the .vimrc file and paste this in there

" Convert two lines (URL then TITLE) to one line: <a href=URL>TITLE</a>
map <F7> <Esc>I<a href="<Esc>A"><Esc>gJA</a><Esc>

Do a wq.

Now, any time you want to make a new line, paste or type in the link, then make a new line again and type what you want the link text to be, like so:


http://www.w3schools.com/

W3 schools

(Except delete line 2. I don’t know why WordPress is putting a blank line there.)

Now put your cursor on the first line there, the one with the url, and then hit the f7 key. The result should look like this:

<a href="http://www.w3schools.com/">W3 schools</a>

Cool, right?

Write pdf docs using html

So, as I’ve mentioned, my computer is old, weak, and slow. (Insert jokes at my expense here. :P )

Even opening up Libre Office is an excercise in waiting. Vim, however, remains as fast as a cyber-cheatah rolling at maximum hyper-speed on greased techno-rails. I’m inclined to use it in any event because I think vim is old-school-cool. So I recently looked for a way to create a pdf (which I needed to make for an assignment) in my favorite editor.

Enter wkhtmltopdf. It’s a simple little program that takes an html file and makes a pdf out of it. The site gives you installation instructions, but I found I was able to install it using

sudo apt-get install wkhtmltopdf

Then I just went to the directory of my html file (made in vim :) ) and ran

wkhtmltopdf file_name.html file_name.pdf

It was stupid easy. And now I have another excuse to use the terminal.

How to remember to remember

One thing I’m really bad at is remembering things when it’s important to remember them. It can really get me into trouble, sometimes with the law.

I have a lot of points on my record for speeding. I’m pretty close to losing my license. My problem is that I keep forgetting to check for speed limit signs.

So when I got another ticket last summer I knew I had to do something drastic. I started pointing to each speed limit sign and saying the limit out loud. I did that for about a month. Then I just pointed to them as they went past. Now, for the most part I just notice them and adjust my speed.

I still may get a ticket in the future, but I’ve adopted a habit that’s made it much less likely.

More recently I did something almost as bad. I missed an appointment for a job interview. I thought I was better at that kind of thing. I felt helpless to the whims of my own memory. I know from experience that, despite how important a thing is, sometimes I just won’t remember it. So, what to do?

Something I’ve already been doing, albeit haphazardly. Every time a new appointment comes up I will imeadiately, no matter what, put it in my calendar and set at least three reminders (a week before, 2 days before, and a day before). I’m also going to set an alarm every day to remind myself to check for upcoming appointments and set alarms for them during the week. (My calendar can do reminders but doesn’t make a noise, my alarm app makes a noise but only goes 7 days in advance).

In both of these cases I’m creating a habit that reminds me to remember. Because that shit doesn’t automatically happen for me. :-)

Don’t just try harder, try something different

I am a slacker by nature. It wasn’t until early middle age that I really got wise to the fact that unless you work really really hard for a long time, you won’t accomplish anything of note. Maybe it shouldn’t be this way, but the world can be rough for the lazy.

And those of us catching up. When I first started going back to school and the semester poured more and more work on my head, my response was to work more and more, to fill as many hours as I could with schoolwork and studying, but that strategy only goes so far.

This semester I’ve really bitten off more than I may be able to chew. I’m working as hard and as long as I can and I’m still behind. I can’t try much harder. What I can do is try something different.

What does a business do to improve it’s methods, find new markets, or become efficient? If they’re smart they don’t simply tell their employees to work harder. They try new things. They experiment. They measure their results. They adapt.

It should be no different for us as individuals. Don’t do more of the same thing if you’re struggling, ask your peers how they do it. Read. Google. Reflect on your own processes. Start collecting stats. Estimate and see how accurate your estimate was when you’ve finished. Try something one way now and the next time you have to do it, try doing it a different way and compare.

Adapt. Experiment. Learn.

Follow

Get every new post delivered to your Inbox.

Join 165 other followers