Python – Variables overview, concepts, terminology, and making some Python programs to demonstrate Variables!

PyVarDemo

This is as straight forward of a variable demo as you can get with Python, X + Y = Z!

Variables in Interactive Mode (on the Terminal executing code) is cached in a sort of memory until saved to a .py file, however if you do not save this in ‘Interactive Mode’ in a Terminal before saving, the variable values.

However as discussed, Interactive Mode / Terminal is mode for testing lines of code, not creating entire complex programs – For that you would use a text editor / Normal Mode.

Lets get right into Functions and Variables with Python Programming!

The first function most people learn and will be discussed here is the “print” function, by printing “Hello, World!” in some form, which is defined by: print (something).

The purpose of the print function is to output the value of variables contained within the ( ) as seen with with the 2 + 2 = 4 using variables, and one important note (why I underlined it initially) is that print is considered a “function” of Python.

On Python 2.x the print function DOES NOT need parenthesis, whereas on 3.x DOES need them, though its recommended to use functions of newer versions of Python in written code as it is backwards compatible (in all cases I am aware of).

Also to note, syntax is absolutely critical in Python, so knowing the differences between things like functions and variables is absolutely critical.

Another component in Python programming is the = Called an Assignment Operator,  which may seem pretty obvious but it assigns a value to a variable, so even if you don’t see an = within the print ( ) function, if it contains variables it is using assignment operators within that function!

Variables

At a high level view “Variables” are a value assigned to a variable, however in a more detailed review of what a variable it is, it is a value that is stored in memory borrowed from the PC’s memory which is assigned a variable we know like x or y to use to represent a certain value.

When these Variables are made into a .py file, it will load those variable back into the cached memory storage to be called when the program is executed, however one last time variable are lost upon closing the terminal you are working in if not saved!

To recap all of this in a few straight forward statements which will be demonstrated:

  • The value on the left side of the Assignment Operator is the “name” called from memory for the value assigned to it
  • The Assignment Operator or Equals symbol assigns values to names
  • The value on the right side of the Assignment Operator is the “value” which gets assigned to the name

To demonstrate this concept including that names don’t have to be just letters:

PyVarRes

So the name it recalled was “result” and that called the value of (x * y), so in this way we can have variables referencing variables to really building out a robust script which I am sure is the end goal for Python – Though I have a ways to go yet.

This could get word salady on an exam or in an instructor course, so a variable is a name and value separated by an Assignment Operator, and once a variable is entered. Another point is that variables are “implicitly declared” upon creation, meaning they can hold entire operations within the variable or have the value changed, but that variable is now designated in memory for later recall.

Not sure why that matters yet, or if its just a good extra piece of info, but Variables are “Implicitly Declarative” in Python, so know it and love it! 🙂

Another thing I am not quite wrapping my head around yet is “Strong-Typed” variables, from googling around means that Python is not good at interpreting it, this is because there are several types of variables that can not be swapped out different pieces between:

  1. Numeric
  2. String
  3. List
  4. Tuple
  5. Dictionary

So time to try to make some code, which in my text editor Visual Studio Code I wrote the Hello World print function again, and saved as .py to be executed in my terminal window:

HelloDerp

Missing a single quote, those will get, you, so with a quick tweak in VS Code:

HelloDerp1

Aaaaand fixed:

HelloDerp2

You can keep Variables going here with multiple Assignment Operators =, however I won’t keep posting up pictures here, but I did want to make one point.

While variables can contain multiple variables separated by an =, as “String” indicated by single or double quotes cannot be part of an Integer Value Assignment!

When you start to build out your variables to used in the same print function, “Strings” cannot be used within them, unless they are enclosed in special use cases shown below.

Also to round this article off quick ( on work a lunch break so excuse the quick closure of this to move onto the next course tonight after work), here is examples of the different type of variable we can.

To quickly wrap up some different types of Varibles:

  • varString = “A string is contained in double or single quotes. ‘So this would be considered a string inside a string when closed with a single quote’ And now to time close the entire string with a final double quote!”
  • varInteger = 32 (Lower Numeric single #’s)
  • varLong =  Longer Integers like 50292874728L <– L indicated a Long Interger
  • varFloat = 123.40 Integers with Decimal  values
  • varList = [1, 2, 3.4, “Hello”, “World!”] Uses square brackets for the above variables
  • varTuple = (5, 6, 7.8, “Why”, “Hello”, “World!?”) Same thing enclosed with ( )
  • varDictionary = {‘First’: ‘The first item listed’, ‘Second’: ‘The Second item’} which uses the curly brackets

First thing to point out is not to put integers in quotes as that would make them a String Literal instead of an Integer, so save quotes for Literal Strings like phrases.

Also the last 3 I am not entirely sure what they are technically called, but wanted to get them on the list here to demonstrate what they are here before we drill into the next level deeper of Python.

That will do it for this article!

Wanted to finish that out quick, was dragging on waaaay longer than I wanted, need to speed up these python courses so notes might get whittled down a bit to what I need as core pieces of code to remember functions and such within Python.

With this out of the way, onto input / output in Python!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s