Matt Learns Python: Part 2 - More Strings, Variables, and Math!

The goal of this blog is to document my journey learning new things in the world of web development and programming. At the same time, I hope to help others learn as well. Feel free to say hello on Twitter!

Previous Post: Part 1 - Getting Started

In the last post (linked above) we got Python up and running and wrote our first script. That was great and I enjoyed our time together. While printing quotes from The Matrix is awesome, it’s time to move on. Ok, one more quote.

You have to let it all go, Neo. Fear, doubt, and disbelief. Free your mind.

Words to live by, huh? Anyway, before we get started with strings and such I want to introduce you to the Python Command-Line Interpreter. You’ve seen it before, actually. Remember how we ran our first script?

python example1.py

Well, this time we’re going to just type python with no file afterwards.

python

You should see something like this:

Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Now, depending on your local environment it may look a little different. Don’t fret, as long as it resembles what’s above you should be ok.

So, what’s going on?

This gives us a handy environment to run Python commands without opening our editor, saving the file, then running the script. We’ll get back to that soon, but for now this will be very helpful for what we’re learning today.

To get our feet wet in our new environment, let’s print a string.

>>> print "Is this working?"

You should see…

>>> print "Is this working?"
Is this working?
>>>

Nice.

Let’s do some math! #

How about some addition?

>>> 1+1
2

Subtraction

>>> 10-8
2

Divison

>>> 10/5
2

Multiplication

>>> 1*2
2

Craziness

>>> ((2*(8+4)/63*8)-(100/3))+35
2

There are more math operators, check them out here.

Variables #

A variable stores data. This allows us to do some really beneficial things.

>>> name = 'Matt'

So, what’s going on?

The variable name now has the value of “Matt.”

>>> print name
Matt

Pretty cool, right? We can also change the value of name

>>> name = 'Matt'
>>> print name
Matt
>>> name = 'Bob'
>>> print name
Bob

Variables can store numbers too

>>> total = 10+10
>>> print total
20

Another great thing we can do is join strings and variables together. In fancy programming terms this is called concatenation.

>>> name = 'Jim'
>>> weight = 200
>>> print "Hello, my name is",name
Hello, my name is Jim
>>> print "I weigh",weight,"lbs."
I weigh 200 lbs.

We join the string "Hello, my name is" to the variable name with a comma (,). Same goes for the string "I weigh" and the variable weight.

That’s all I’ve got for now, friends. I Hope you learned a little something.
As always, I’d love to hear from you. You can find me on my Twitter machine.

 
11
Kudos
 
11
Kudos

Now read this

Matt Learns Python: Part 1 - Getting Started

The goal of this blog is to document my journey learning new things in the world of web development and programming. At the same time, I hope to help others learn as well. Feel free to say hello on Twitter! A friend of mine had been... Continue →