Python – Input and Output functions, where a program can accept input to produce dynamic output based on input!

VSC_Py

I waited until I got into this course to grab a screen snip for an image, and this has been a LinkedIn favorite, so I’ll go with this as the cover photo of the day 🙂

Input and Output and where its executed at what times

Input and Output at its highest level is allowing a user to input some sort of data into our Python program, and it uses all its mechanisms to give a dynamic output based on the input. This will mainly be seen in the “Terminal” in “Interactive Mode” where we can get immediate input / output program execution data.

Many “Normal Mode” programs are comprised of a lot of chunks of this type of Input/Output data, however we use Interactive mode to ensure we get the desired result before adding it to our Normal Mode / .py file to save as part of our program.

print (_) is a crucial function for input / ouput, and we’ll be introducing Parameters which are Strings or Values that are separated by commas within the (_).

Gathering input

There are two ways at this point in the demo to gather input:

raw_input (_)  <— This is used in Python 2.x
input (_) <—- Same function only in Python 3.x (remember its always nice to use the backward compatible coding, so we will use raw_input (_) as 3.x understand it as well, and it can run backwards compatible on say a 2.x Python script.

These two functions take the “input” gathered and assigns them to a variable to be recalled at a later time as well, so keep that in mind that input = variable.

Some new operators outside the = sign being brought into play!

+ – * / (Plus, Minus, Multiply, and Divide) they work exactly as they would in math, however they have some slight differences in how they execute different types of data.

For example if the variable is text or a #, the way these operators will work changes.

cmp(#,#) is considered a function to compare two numbers, returning or a 1 or -1 indicated whether the first # is higher or lower than the original #, for example (this only works in older versions but for some reason is not working on 3.8 Python for me to demo).

Note – I did find an explanation on this change in Python documentation:

In Python 3, the cmp parameter was removed, and only key (or no argument at all) can be used.

There is no fixer for this change. However, discovering it is straightforward: the calling sort with the cmp argument raises TypeError in Python 3. Each cmp function must be replaced by a key function. There are two ways to do this:

  • If the function did a common operation on both arguments, and then compared the results, replace it by just the common operation. In other words, cmp(f(a), f(b)) should be replaced with f(item)

  • If the above does not apply, wrap the cmp-style function with functools.cmp_to_key(). See its documentation for details.

    The cmp_to_key function is not available in Python 2.6, so if you need to support that version, you’ll need copy it from Python sources

I am not sure what this means, but Python is starting to seem like every even MINOR version that comes out it removes simple input to run Interactively, and requires more complex strings as the language involves I guess into more academic and Enterprise environments is my best guess – Either way its unnecessary to remove these functions and replace them with complex operations rather than just add those operations.

I am not liking Python to be honest, at least the new 3.8 that I am working off, 3.4 Ben Finkel teaches from has bee wiped off the face off the Earth (and I probably need to learn 3.8 anyways bleh), but I did confirm in my Ubunutu with 2.7 Py native in Terminal:

PyCmp

I mean did we really need to remove that simple functionality for beginners to learn using these functions to replace it with complex code? Just irritating honestly. Anyways.

“Concatenation” is another good term to be familiar with, as it is what puts together all the different Strings inside a print function, for example:

PyCat

As seen as long as we have our Strings in Quotes and comma separated, and enclosed with parenthesis we really could separate each letter of Hello World! in that print function as one long Concatenated print function / waste of time 🙂

The usage of “sep” and “end” operators or whatever they are called

Two things that can be used to remove the spaces between Strings in a print function is known a “sep” meaning it will remove all the “separation” in that wrong defined by the following characters.

In fact I found such an awesome way of rapidly doing this in Visual Studio Code with Python Scripting I wanted to share on the page here:

VSC_Py

My first statement was: print (‘Hello,’, ‘World!’, ‘!’) and it came out: Hello, World ! (so the ! got a little out of place but otherwise worked out well).

Then in VS Code I code simply add the additonal: , sep=”) and it came out: “Hello,World!”

It actually helped with Syntax in VS Code along the way, by hitting the Green Play button with the big red arrow pointed at the code was executed in the box below immediately, its like coding on the super fly and I am barely getting a hang for typing this stuff out!

I love Visual Studio Code, a free utility with incredibly power, go download it please! 😀

Similar to “sep=” we have “end=” and lets see what that does:

sepvsend

The output as seen in the lower portion is output: Hello, World!Hello, World,!

So the “end=” function in line 1 meant once those two single quotes close that was the end of the print function, while the 2nd line was still removing separation between those single quotes.

Kind of an ugly example, but a clear example of what function they serve, though there is a multitude of uses for those statements outside of messing with String Literals!

One last example for example sake, is printing a normal line with no sep / end:

VSCLines

I just love VSC, you can see I’ve added a third line without those sep / end operator things, and they did not impact the following line – They only affect the line or function (which can span multiple lines) that they are assigned to but will not impact other functions – And again yes if you haven’t go get Visual Studio Code for Python coding!

Now to bring this into the realm of Input / Output (and some outdated code)

I will be straight forward with the Syntax as it builds quick, one note is the “input” command in 3.x would NEED to be “raw_input” in 2.x Python versions, so if you ever consider updating your Python Version, you will want to TEST EVERYTHING in production before making the cut-over from even minor version to minor version!

That being said, I made this script in my favorite text editor VSC:

PyImg15

This is an interesting Variable, because the Value (on the left of the =) must be ‘Input’ to the program, while the right side the ‘input’ command tells the program to print the message shown here according to the input which I executed in my Windows Terminal:

PyImg1

Though I am a fan of a good space before the ! we want to tighten that up, and to do this we use the + which with text is not the “Addition operator” as it would be with print (2 + 2), because operators change based on the context they are used in.

When a + is used with text, it is called a “Concatenation Operator” because it is being used to put the ! right up against the word, which is an important thing to distinguish like two words can have separate meanings so can “Operators” in Python.

Shown here with the code being update (comma removed and + added) :

PyImg25

And the result in my terminal when running “Yourname.py” :

PyImg2

Nothing to it, everything is looking good, however there is a different way to perform this same function, by creating a new “Variable”  that = the users input, and prints that:

PyImg35

Aaaaand:

PyImage3

Well Hello to you too Python! It operates the exact same as the command before it, we just used the addition operator to add two strings together.

And that will do it for this post!

There is a bit more I wanted to play with, but I’m running into more Syntax errors, I am going to have to look at possibly rolling back to an earlier version of Python to keep my studies going and that really sucks.

Anyhow, until next time!

 

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 )

Facebook photo

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

Connecting to %s