While going through this course I won’t be writing full throated explanations of anything but the take-aways from weekly course content from reddit.com/r/PythonStudyGroup
Shown above is a method of error handling if your program may be used in either a Python 2.x or Python 3.x environment, which I thought was a huge take away from this first week.
Another thing I found worth noting is working with directory function, and working with it using variables, as you can simply tack on a directory function to the variable to give it an attribute of that directory contents… but first:
First the Zen of Python import!
Essentially this is the mission statement of Python code, that it is meant to be clear / concise, and I just thought that was a cool little bit Python added to its default import 🙂
Now to my point, showing the “directory” command to see how a variable can be used:
In this case I want to add capitalization to my variable without changing the variable itself, to do this I input the following into IDLE :
This is where the important part of using an IDE to assist in coding may come in!
Not only do IDE’s such as IDLE perform immediate execution of code for testing, but it also provides drop down menus as seen here, to allow you to view the directory options without using the verbose / CLI output of from “dir(variable_name)” to see options.
Also note the capitalization worked as expected in that it simply capitalized the leading letter in the variable, where as now I would choose “lower” from the drop down menu or type it in to turn “something” back to lower case characters:
Which of course as earlier discussed, but you can never repeat Python principles too much I’ve found, you will want to use a closed parenthesis on the end () which tells Python to simply call that function immediately (I believe I looked at this with sys.exit).
I actually caught myself making a typo in my variables, but caught it pretty quick here:
I missed the “g” on “something” variable, so I created a new variable called “somethin” which equaled the variable “something.upper()” as demonstrated in IDLE.
These variations can also be strung together, and are executed left to right!
With that its purely for a demonstration, but these different directory functions can be added onto the same variable over and over, and each function will over-write the previous one to the left of it as shown here:
So it went from All Uppercase to All Lowercase to Capitalizing that first letter.
Again I am sure there are better uses for this, but this is just a demonstration that you can string these together for other purposes to be taught later on down the course.
From that verbose listing of directory items seen in “dir(something)” you can also find help within the IDE for a quick explanation of what the directory item does! :
I won’t go into trying to use this built-in “split” function as I am still not entirely sure what it does off that explanation (or care as it may not pertain to this example), but wanted to show this is one reason to work with an IDE along with a program to write a program itself such as Visual Studio Code.
How to set variables, what the pieces are called, and how to find their code id #
Lets take this for an example to dissect:
First to review, on the right is the “variable” in the program, the = is the assignment operator that designates what the variable equals, and to the right is a “string literal” because it uses parenthesis and quotes (‘10.10.10.1’) so it is not an integer but a string literal like a word and NOT an integer!
Also to see where Python stores this variable in its storage (think of it like RAM), can be seen with the id(variable_name) command, as shown above.
Below is a demonstration of two different variables that are the same, and initially show they are referenced by different memory blocks by id # in Python, but once variable #1 is set to equal variable #2 Python then points to the same id # for both variables:
We can also use the “is” boolean to confirm that 1 = 1 with a True / False output, but note they initially point to different ID numbers, until one is explicitly assigned to equal the other which is not something you’d normally do but to visually explore how Python stores and calls on variables – It is not calling 10.10.10.1 but that id # shown for the variable(s) in the above example!
As seen the new shared space is going to be the second variable, in this example that is ip_addr2, and also seen it shows you can use either words (is / is not) or the boolean operators ( == / !=) equal / not equal to determine whether something is True or False.
ASCII (Python 2) vs Unicode (Python 3) – How to make programs compatible!
This is brand new to me, but I will try to give the best explanation possible:
One of the big differences in working between Python 2.x and Python 3.x is that when you create a string in Python 3.x it is automatically “Unicode” rather than plain ASCII characters, whereas Python 2.x is ASCII unless you specify it as Unicode before the string.
The reason for this change is ASCII can only represent or is “mapped to” a limited set of data that its possible to represent, whereas Unicode does not have these limitations, this is why it is the default in Python 3. Unicode can represent complex data models such as pictures or moving graphics that ASCII cannot, however this represents a problem:
This presents a compatibility issue with Python 2 running Python 3 Unicode!
In Python 2 to declare a string literal you had to specify it with a “u” as shown here:
As can be seen, Python 3 is backwards compatible, and takes the explicit “u” for Unicode the same way it would handle a string literal, however Python 2 cannot!
To demo making an ASCII string in Python 3, I use a ‘b’ instead of a ‘u’:
So how do we resolve this problem? Type a “u” in front of every Py 2 string?
Going back to the program I am working with, you can add an extra “future import” function that is run with the code as shown here:
This defines that going forward, any literals such as string literals should use Unicode, again this is not necessary for Python 3, but can be added to a Python 2 program to allow all literals to be treated as Unicode by default instead of ASCII.
Exactly how “from __future__ import print function” allowed the raw_input or input statements to be used, this now allows literals to be treated as Unicode in Python 2.x programs to allow for compatibility with Python 3 programs being run in Python 2.
I hope that made sense, as its a bit much for me to wrap my head around, but the big take away is that is how you can make Python 3 code work in Python 2.
Using “in” and “not in” sub-strings to search literal strings for matching input
This can be useful when working with Python for Networking purposes, as you may want to perform an action against only certain subnets or host addresses, in which case you could use the following operator in your program:
These are referred to as “sub-strings” that can be used in place of == or !=, as these operators are ‘match exactly’ or ‘doesn’t match exactly’ for a string, so it can find pieces of the string that match that can then execute a code on if True and not if False.
You can also pick out pieces of string using the square brackets using “zero indexing” meaning the first character starts in the 0 position, you can do this working forwards or backwards shown here:
Starting at 0 is the 1 from 192, and using the – operator indicates to start from the back of the string, which is the final 1 in ‘192.168.1.1’ in the string.
At the tail end demonstrated is the ‘len’ or length function, which will give the number of characters contained within the string.
Next is a look at string “Concatenation” using variables and Integers
Strings can be variables that contain other variables, and can also add strings onto a variable to make a complete string using the variable, so that you don’t need to alter the meaning of the original variable – This is called “Concatenation” and is shown here:
This covers a few points of Concatenation:
- You can create a variable, and use the + operator to add a string to the variable to make a complete string (something + ‘ World!’)
- You can make a second variable that contains a Concatenated string making it a single variable to be called later (hello_world)
- example2 shows that you cannot string together literals along with integers, as Python does not know if the + could be either for math or Concatentation
- The second example of example2 = 2 + 2 demonstrates that variables can also represent math equations (refer to my PEMDAS Python page for more on this)
So Concatenation is a way add onto variables without changing the variable.
Demo of how to represent integers in Binary and Hexadecimal format in Py
This will come in handy down the road I’m sure, so a quick demo here:
You cannot represent 192.168.1.1 as an integer, even using “int(192.168.1.1)” or it will throw an error at the special character of the period, I felt that was worthy of noting here as well.
Just a quick touch on that, marching on!
Use the “raw” function to ignore special characters in string “paths”
When creating a Path whether it be via a Windows Directory or a Linux Directory, there are special characters that can throw the path of that has special characters, which require the special character ‘r’ added as shown below in Visual Studio Code:
Here we see the original “some_path” variable, with \n and \t highlighted, which can be seen starting a new line (\t) and putting a tab space in the output (\n), however with the “r” in front of “some_path2” it tells it that the variable is to ignore all special characters.
I am calling it here for tonight, though I will finish filling this in with week 1 info!
I learned a valuable lesson about procrastination and where it gets you, as I pushed off studies until the weekend when I was sick as a dog, so I will be playing some catch up here into Week 2 material coming out this Tuesday (so I plan to really hit it tomorrow).
I will also be posting to the reddit group reddit.com/r/PythonStudyGroup as I can, please feel free to join at any time, I plan to be much more active going forward to tackle Python once and for all – More Week 1 info to come in this article!