Learning Python Programming - Part 2 - Learning variables

in #programming8 years ago (edited)

Welcome to the second part of an ongoing series: Learning Python!

In this lesson we will be learning the importance of variables, then upgrading our previous Hello World program!
Here is a link to the previous post.


(source)

Before we continue, here is a very important note:
The hash symbol or pound symbol (#) is used to denote comments in Python code and is ignored when the program is run. It is good practice to cover your code in comments that explain what your code is doing.

First, Variables.

Variables are very important in programming, and come to think of it, are probably the most used concept in a program. Variables store information for later use. This information can be anything from numbers and text, to pointers that reference different parts of your code like functions and, later down the line, classes.

How do we make a variable?

To assign a value to a variable in Python, you need a line of code before you use the variable that looks something like this:

myFirstVariable = 1

In this example the variable is named "myFirstVariable" and we are assigning it the integer 1. The code must have the variable name first followed by a single equal sign and then the value you wish to assign to it.

What can I name my variable?

You can name your variable anything you want!
With a few exceptions:

  1. Your variable must start with a letter or an underscore.
  2. Your variable can only consist of letters, numbers, or underscores; with the exception of the previous rule.
  3. Your variable is case sensitive. You can have Var, var, and vAr all set to separate values with the only confusion being in readability.


(source)

Why do variable names have these rules?

When a Python program runs it is both interpreted and compiled. I can't go into the specifics of interpretation and compilation, as I am not qualified in that detail, but for now you can imagine it as Python reading over the code you wrote. For the first rule, you must start a variable with a letter or an underscore because Python detects variables by looking for a letter or underscore in a place where a variable would be located; like for instance: the start of a new line, or in between parenthesis. More concisely the first rule is important because that is actually how Python interprets what is and is not a variable. For the second rule, your variable can only consist of letters, numbers, and/or underscores because anything else is a different instruction for Python to interpret. If you insert a plus sign (+) into the middle of your variable, Python will read it as a first variable + a second variable. For the third rule, your variable is case sensitive is simply because Python sees capitol letters as a separate letter to their lowercase counter-part. These three rules of naming variables are very simple and allow for countless variable names to be made.

What can be assigned to a variable?

You can assign anything you can think of to a variable, but there are some rules. These rules are based on what type of variable you are making. In some languages, like Java, you must declare the variable type before you can assign anything to it; in Python you do not have to declare your variable, but what you are assigning must fall into a certain type. There are 6 standard variable types: Booleans, Numbers, Strings, Lists, Tuples, and Dictionaries.

Booleans are simply a true or false value. In Python we write true as True and false as False; the lowercase is actually a valid variable name.

Numbers are pretty obviously any number, but there are 4 sub-types: int, long, float, and complex.
Ints or integers are simply any positive or negative whole number with no decimal point in the range of -2^(your_bit_rate-1) to 2^(your_bit_rate-1)-1, which is 9223372036854775807 for 64 bit machines.
Longs are integers of unlimited size and differ from ints in that they end with the capitol L.
Floats or floating point numbers are any number with a decimal point.
Complex numbers are any number that contains the mathematical i. Complex numbers are written in the form of a+bJ where a is a and be are floats and J is the square root of -1 or, in the world of mathematics, i.

#Ints
integer = 1
#Longs
long_int = 10000000000000000000000000000000L
#Floats
floating_point = 1.0
#Complex
complex_num = 10J

NOTE:
The next four types are very similar to one another with some specific differences that make them unique. They overlap in that each of them has an index to denote elements within them. This index starts at 0 and goes up to the length of whatever it is indexing-1. You can use this index feature like so:

var[index_number]

This is not the only similarity between these last four variable types, but is a very important feature that each of them utilize.

Strings are the variable type that groups all textual values and they have one very general rule. They must be surrounded by single or double quotes, but single quotes are more widely used. (In my examples I will be using double quotes as a personal preference) Anything in between these quotes are part of the string. Whether what is in between is a number, symbol, or letter, they are all made into a string.

string_var = "String"


(source)
Lists are pretty obviously used to make a list of other things. A list starts and ends with a square bracket and elements in the list are separated by commas. A list can contain a very large number of elements with that number being related to your bit rate in the expression 2^(your_bit_rate)-1, but to have a list with that many elements on a 64 bit machine you would need 9.2 EB (exabites) of RAM!

lst = [one, two, three]
# index: 0   1    2

Tuples are immutable lists, meaning that once you initialize them, you cannot change them without re-initializing them, and are surrounded by parenthesis similar to the list type.

tup = (one, two)

Dictionaries are like variables themselves with each element pointing to a different value. Dictionaries are surrounded by squiggle brackets {} with each element having two parts, a key and a value separated by a colon. The value of a dictionary is to put in one value and get out another.

dictionary = {key_one:one, key_two:two}
print(dictionary[key_one]) # would print the variable one

Any of those variable types can be assigned to a variable.

Next, let's upgrade our program!

Our program was pretty basic last time, but now we're adding variables.
First, we are going to define our variable that we are going to print out. Let's name it "text." This variable definition must be run before you use it in your program or you will get a variable undefined error!

text = "Hello World!"

Second, we need to replace our text in our print statement.

#Before
print("Hello World!")
#After
print(text)

Lastly, we can run our program to confirm our new proficiency in using variables!

Previous Post