Contact Form

Name

Email *

Message *

Mattia’s Dummie’s Corner- Variable initialization

I'm trying to learn numerous programming languages, after studying Visual Basic last semester, and this concept suddenly hit my mind yesterday, while I was watching a Python tutorial.

What's a variable?

It's something that may vary, i.e. can get different values.

It's nothing but a placeholder for something you want to store, a sorta box where you want to store something for you to use later.

If you grab a programming textbook, you'll find several data types, i.e. numerous types of variables you can use (strings, floats, integers, booleans, date/time, etc.).
    We don't need them now, so let's forget about them for a second. I love Python programming language for many reasons.

    One of the most important is its ease of use, as there's no need to state what data type your variable is, when coding in Python.

    We create a variable by simply giving it a value (assignment).

    For example, if we want to define a grade variable in Python, we'd have something like:

    grade="A"

    The use of quotes informs Python that's a string variable (text).

    In case of numbers (e.g. In Italy we use numbers 18 through 30 for grades, where 30 is A), you'd have, for example:

    grade=26

    Loops and variable initialization

    Now let's suppose we need to know how many students, included in a list, got A as their final grade in a class.

    We're not definitely going to count all A's manually (it can be easy for 3 students, but how about 300,000?), but simply create a program that checks seamlessly all values one by one and sums all the A's it finds, or, in other words, a loop.

    The term loop comes from loop tape cassettes. Check the link and you'll immediately get what I mean by going through a loop.

    Before checking all the grades in the list, it's necessary to create and initialize a counter variable.

    Counter gets usually a null value, as computers start counting from zero. If we need to check for a string inside our list (i.e., we need to check for "A") , we define counter as an empty string (for example, counter=" "). An empty string has a length equal to zero (there's nothing inside).


    Why's that?

    Let's compare a variable to a story.

    If you're writing a story about a character named -say- Jeff, you need to create it before the story starts developing.

    You can't skip to page 20 and come out with: "Jeff walked through the door and never went back".

    You've never created a character with that name, so your readers would think: "Wait a minute, who the heck is Jeff now?".

    Similarly, if we want to loop through a variable, we need to initialize it first (i.e. create it and assign it a starting value, which is usually zero).

    Otherwise, our code will spit out an error like "I don't know what this stuff is, as you haven't created it yet, so you can't use it here".

    This is a very common mistake, but it's easy to fix if you think of what you're doing.

    In the end, human life is a little bit like a program (I know, it’s much more than this, but I’m trying to make a point here).
     

    We were born (initialization), we live our lives (runtime) and we die (end).

    It’s still unclear to me if we end up in God’s big Recycle Bin (or Trash, for other operating systems), or not.


    If you think of a Trash, that gives you some hopes, even though this is only a old geeky fart’s digression
    .

    Hope my post will help absolute beginners understand this critical concept.

    Cheers!

    Comments

    Related Posts Plugin for WordPress, Blogger...