DEV Community

Zachary Stone
Zachary Stone

Posted on

Python Teach & Learn Part 1: Variables

We are going to start where all programmers start. Staring forward into the vast expanse of the new shiny language we are embarking to learn and learn how to say "Hello, World!". Why?

hmmm... I don't know why. It's just that's the first thing we do.

To say "Hello, World!" we need to tell the computer the right syntax so it can understand what we want. And by "say" I don't mean the computer will verbally say it, although there are ways to do that... I mean that the text "Hello, World" will be printed out in the console window. What is the console window? WELL LET ME JUST BLOW YOUR MIND!

If you are on a windows PC hit the F12 button while you are in a web browser.. yes, go ahead and do it! If you are on Mac google how to open up the developer tools in your browser. I don't feel like googling it myself, and you need to get used to googling because you will google more than you ever googled before with programming. There is a TON to learn, and you won't remember everything. You won't even know what tools are all available for you at once.

Well, is your mind blown? No.....? Oh.. you don't know what you are looking at... Let me explain. What you are seeing is something you will use a lot as a programmer. Right now it may be confusing, but you will get familiar with the developer tools the more programming you get under your belt, but for the sake of staying on topic, we are going to focus on the console. The console is used by developers to debug, see errors, and write haikus to each other (maybe not the last part). You can also print out the text to this console. And this will be the first thing we learn.

Now if you are unsure how to download, set up Python, or run the code, you can look at the first post I wrote introducing this new series for a link to a non-programmers guide. If you don't want to download a text editor, python, and go through that mess...you can use Pyfiddle

Fo Shizzle

Now we are cooking with fire!

The first thing we will learn is not a variable, but a function. That function is called print. Think of a function as a verb, it's a doing element. Functions can take in inputs called parameters, and inside the function something happens. In this case print takes in a value and writes it to the console.

There are different kinds of values. But right now we are going to start simple and pass a string to the print function. One of my favorite analogies of a string, other than the simple it's just text... is thinking of the Happy Birthday banners you see sometimes at parties.

Happy Birthday Banner

These banners have a literal string that is attached to a bunch of letters that are arranged in a certain way that whoever sees it can read out the message. This is what a string is. A collection of letters (in programming they are char or characters) arranged in an order so it can be used to be readable.

So let's write our very first line of code with python using the function print and a string!

print('Hello, world!')

YOU DID IT!

Horray!

You should see the output in the console say Hello, world!

Now, let's move on to talk about variables since this is the first topic of our series.

Wikipedia says that a variable is ..." a storage address (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context. This separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during program execution."

Gary Coleman

It takes different strokes for different folks I guess?

To put it simply, a variable is a way to store a value for use later. Sorta like a bucket. Yeah... a bucket. Except you can't just throw anything in the bucket mixed with other stuff. No junk buckets with random doodads. You have to stick with a type.

The standard variable types are:

  • Number
  • String
  • List
  • Tuple
  • Dictionary

We will just go over the first one for now, and then we can focus on the others in a later post.

You already used the String type, we just didn't store it in a string variable. So if we wanted to print out "Hello, World" over and over, we would have to hand type it out every time we used the print function. That would be tiresome, and it also makes it easy for us to make a spelling error. So instead we can declare a variable and assign it a value.

Look at that, we are already using fancy programming lingo... we will be impressive to the dev community in no time!

Fancy Pants

Let me show you how that is done.

str = "Hello, Mr. Fancy Pants
print(str)

Let's break it down.

str Is the variable being declared. In Python, you don't have to explain if you want to use a number, string, etc. This is inferred when you assign the value to the variable. I used str, it's short for 'string' just as an example. I could name this whatever I wanted. poopy pants hiya pal, whatever you wanted it would work. BUT, in programming we want to name things so that we can understand the code better.

The str is the declare we talked about earlier. Just like God declares in the OT In Genesis "Let there be light"! We declare things to exist in our program. In this case we are declaring a variable named 'str'.

The next part = is the assign we talked about. This is where we attribute value to the variable "bucket" we made. This is also where Python infers the variable type. It looks after the = to see the string `"Hello, Mr. Fancy Pants!" makes the variable a string type and assigns the value of the variable the string we give it after the assignment operator. Now that we have a bucket for our string, we can use it elsewhere without having to type out the string. So we can pass this as a parameter in the print function.

print(str) str is referencing the variable we declared, then passing it to the function, which then prints this to the console!

And that is the concept of variables in a nutshell. Wow... we learned a good amount in such a short period of time. Until next blog!

Top comments (0)