How to Train Your Python: Part 2, Basic Output, Variables, and Types

Oct 7, 2015 09:59 PM
Oct 7, 2015 10:01 PM
635798269274989284.jpg

In python, or any scripting language for that matter, there are certain things that you will carry with you throughout your entire scripting career. Some of the most basic things that you will keep with you are output, variables, and their value types. We'll start by going over output, then cover variables and their types. So, let's get started!

A Side Note

When practicing scripting with python, you don't have to keep making files to test your scripts. You can test them in a live python environment, which can be accessed right from the terminal! We can get our live python shell by simply entering "python" into the terminal...

635798232348114447.jpg

We can see here that after we entered python, we were given some information about the version of our interpreter, and then provided with a prompt. This is where we'll be doing our scripting for this series, as it is easy to test things.

Basic Output

Now, when I use the word "output", what I mean is communicating something to the user. Whether it be telling them that the script is shutting down, or that something has changed, when we give output, we're communicating something.

In python, when we want to communicate something, we use print. This simply takes whatever we tell it to and displays it in the terminal. Let's start with a classic print statement...

635798234123583019.jpg

Now in the above snippet, you'll see that the words "Hello, World!" are incased in quotations, this is because this signifies them as words. This is called a string. A string is a fancier way of saying words. For example (and we'll be using this later) the string "10" is not the same as the number 10. This is because the string of ten is seen as a word, not a number.

Now this is one of the things that we'll carry with us throughout all of python. But there are ways to string multiple things into one print statement.

Variables and Their Value Types

Now throughout every scripting language, there are variables. Simply put, variables hold the value of whatever you decide to put in them. Now python is what is called an "object oriented language". This will become more important later in this series, but what this means for us now is that we don't have to declare variables, we just make them. For instance, we can give a variable the value of "Hello, World!" and then we could print that variable instead of the words themselves...

635798239124676940.jpg

Here we gave the variable "greetings" the value of "Hello, World!". Now if we try to print this variable, it will print it's value.

635798239514051478.jpg

We entered "print greeting" into our python shell and instead of printing the word greetings, it saw that it was a variable and printed it's value. We can also assign numbers to variables and do some basic math...

635798240393114367.jpg

Here we give the variable "number1" the value of 10, as an integer. Then we say we want the value of number1 plus one, and the result (obviously) is eleven.

Now, you may have noticed that I said "as an integer" when talking about the number1 variable. This is because that there are two types number values in python, integers and floats. Integers are whole numbers. 1, 2, 3, and 4 are all integers, but 1.5 however, is a float. We can also assign floats to variables, just like integers, but when we do math with them, we can use decimals as well...

635798243548895605.jpg

Here we've made a new variable, number2, and we assigned it a value of 10.0, a float. Then we added 13.37, and it returned a float. I used addition as an example, but as a side note, be very careful with division, python will not return a float unless you divide floats, it will round down and return an integer, this can cause issues if left neglected.

Now, one question you might have is "How do we convert from one type to another?", well, get ready for your answer.

There are ways you can convert one value type to another. First, we'll convert strings to integers...

635798251663428723.jpg

A Side Note

Be careful when printing multiple values. If you attempt to print a string and an integer in the same print statement, you will get an error in return. This is when we can convert everything to strings in order to print it.

Here, we've reassigned the number1 variable a new value, but it's different than what we've seen before. That is we've converted the string "10" to the actual number 10. We can also convert integers to floats using a similar method...

635798253902334818.jpg

We can also convert numbers to strings using the str({NUMBER}) method.

The Exercise

That does it for this article! For a small exercise and for some practice, you can make your own print statement, using what you've learned here today. Don't be afraid to experiment and try things out. Once you've gotten a print statement together using what we've learned here, put it in the comments!

Feedback!

Let me know what you think in the comments section! Sorry this article seemed to scattered, it was a bit difficult to group things together.

Thank you for reading!

-Defalt

Just updated your iPhone? You'll find new Apple Intelligence capabilities, sudoku puzzles, Camera Control enhancements, volume control limits, layered Voice Memo recordings, and other useful features. Find out what's new and changed on your iPhone with the iOS 18.2 update.

Comments

No Comments Exist

Be the first, drop a comment!