Friday, May 15, 2020

Just Getting Started!



I'm starting my Python education at w3schools.com.


Why?


I have two main reasons for learning python. The first reason is I want to be able to develop software to support automated methods for managing listings and also analytical methods for improving merchandising and marketing on my eBay store. The second reason is to gain marketable skills for re-entry into the traditional Workforce.


I have not decided on an IDE yet but the ones I'm looking at are Thonny, Pycharm, Netbeans and Eclipse.


I think it's cool that python can do system scripting and it can also handle complex numbers and a lot of advanced math operations.


As I go along I will be expand the drop-down pages to provide useful information category by category. Moreover, sprinkled within posts and also in special pages, will be insights, tips, tricks and possibly even ways that I purposely broke the code so I can try and gain insight into how python is working. One example would be how python handles complex numbers. I experimented around in the w3schools Try Me editor and it looks like it doesn't matter whether you put the real and the imaginary arts left and right or right and left: python will still treat that as a complex number. For example y = 3 + 2j will be handled by python the same as y = 2j + 3 (j is python's special symbol square root of -1). So it's the option of the programmer on how one wants to arrange the real and imaginary parts of a complex number. However, you can force python to generate an error if you just put j by itself; it has to have number in front of it, even if it is just a 1.



Comment lines


The # allows you to leave a comment on just one line or the portion of the line that is to the right of a code line. In order to get multiline comments in Python, you have to do a hack: use a sequence of three double quotes to start the multi-line comment block and then another threesome of double quotes to end it.



# This is a one line comment


y = 2 + 3j # also a valid comment here to the right of code


Notice that a new line character is the command delimiter in python, not a ; like in PHP.


"""

this

is also a comment

"""


Code block limiters


In Python indentation, not brackets or other symbols, are used to denote the beginning and end of a code block. In this way it's more readable as in regular English writing of an essay or correspondence letter. The more you indent, the further nested is your code block. Remember to be consistent with your indentation; the beginning and ending of code blocks need to have indentations that line up perfectly.


start code block 1

    start code block 2

        start code block 3

        end code block 3

    end code block 2

end code block 1



Variable declarations


In Python there aren't any formal declarations; instead a variable is declared at the point of first use and state of its data type can change on the fly. More about that later. Again variables are not declared but rather they are defined or undefined.


As a time and space saver python allows you to do multiple variable assignments


x = y = z= "yes"

x, y, z = "1", "2", "3"


You can also use the plus sign on the fly to concatenate strings or perform numerical addition. You can NOT use the + sign to merge a string with a number though.


The python keyword def is used if you want to create a function. The def keyword should be all the way over in left margin


def yourFunction():

    code

    code


yourFunction()

# Indent all the way back to the left to close the function set up by def.


In Python a global variable is a variable that sits outside of all functions and is available to all parts of the code. However that global variable will not be used if a local variable inside a function is of the same name. Instead that local variable value will be manipulated until the function exits and then the global variable takes scope back. In an effort to try and break this, I ran a test where I put the local variable of the same name as a global variable AFTER the call to that variable in a local function. What happens is that python kicks out a "reference before assignment" error. Therefore make sure that if you are going to make a call to a local variable of the same exact name as a global variable, go ahead and assign it first in the local code space. Either that, or name your local variable differently from the global one. In other words, because you tried to assign it in the local code space, but did so erroneously after the call, the local function call does NOT get a value from the global variable of the same name (which is what you might think would happen as default fallback). The python interpreter is setup to break at that point.


If you really want to get fancy there is a global keyword in Python that you use only inside functions. It will permanently overrule any assignment to the global variable of that same name on the outside, and it will persist even after the function exits (and even if you enter other different functions!). So it only takes one localized manipulation while using the global keyword to affect it for ALL the other functions!


Python lets you use variables symbolically and algebraically, meaning, you can use them just as you would a scientific equation or answering a question on an algebra test.


There is a bytes variable type in python. You can either spell out the word bytes or just use lower case b as abbreviation; either will work the same way.


# these all work the same way

d = b"goodbye"

d = bytes"goodbye"

d = b'goodbye'

d = bytes'goodbye'


As a workaround for the fact that python does not have a formal way of declaring variables, the user can specify their type using a python Constructor of the same name as the type. In the case of the range, frozenset, byte array, and the memoryview data types in Python, they are their own Constructors - meaning - use them to define a variable with its constructor. See this page for all the python data types that can be used to define variables, plus their constructors.


Circular references are valid.


g = int(g) # is allowed


Complex numbers in Python


You can set up a complex number with the coefficient of zero (0) in front of the imaginary part; it will be valid in Python. This allows you to set up a "variable infrastructure placeholder" of sorts so that calculations further into the code could replace a nonzero coefficient in front of j, making what was simply a real number into a complex number by adding a non-zero imaginary part on the fly.


You can convert integer or float data types into complex, but you cannot convert complex into int or float types.


To bring in specialized functionality that's pre-built for you, python has the concept of importing modules which would be similar in the C programming language to including a header to a library that would be linked in at compile time.


import module_name


Although the programmer has the freedom to write a complex number as real + imaginary or imaginary + real, some functions (in particular print) when invoked on a complex number will always put the imaginary part second when echoed to a screen.


y = 4j + 5

z = 6 +0j

print(y)

print(z)


results in your display:


(5 + 4j)

(6 + 0j)



Casting


Really just another term for Constructors.


Fooling around with casting:


x = int('6.9') generates an "invalid literal for int with base 1o" error, even though w3schools claims that float literals are valid inputs for the int casting constructor?!


Other non-obvious results of casting:


x =float(.1) ----> 0.1 # python adds a leading zero for you

y =str (2e5) ------> 200000.0 # expands the scientific notation and adds a .0

y =str ('2e5') ------>2e5 # preserves the scientific notation as is

z = str(3.0e-9) -----> 3e-09 # adds a zero in front of single digit exponents

z = str(3.0e-09) -----> 3e-09 # preserves the scientific notation as is


An int cast of a float always rounds down.


































No comments:

Post a Comment