[Python #1] – Installation, print(), strings, integers and variables

[Python #1] – Installation, print(), strings, integers and variables

In this tutorial, we will:

  • – Download Python on our computer
  • – Download a Python coding editor to write our code in
  • – Write our first few lines of code
  • – Try to complete an interactive exercise

As explained in this article, Python has recently become one of the most popular programming languages (despite being developed in 1990). The main reason behind its recent popularity is its simple and short syntax; what might take 5 lines in other programming languages could often be expressed in 1-2 lines in Python. The article also covers the general introduction to programming languages and how they work, and I recommend reading it before engaging with this tutorial!

1. Let’s download Python to our computer

Python can be downloaded here. After opening the link, scroll down and download either the Windows x86 executable installer (for Windows) or macOS 64-bit installer (for macOS).

After downloading our Python package, click on it to install it. Make sure to check the ‘Add Python 3.X to PATH’ button in the installation window as shown below:

After installing Python, we still need to download an editor to write our code in. Do not forget that Python itself is not a programme, it is a package that, in simple terms, teaches the computer to work with that language and to understand it. Although we can write python code in notepad (Windows) or TextEdit (macOS), both of them offer no features that would make our coding experience simpler and more enjoyable. For this reason, we want to write much of our code in an IDE – an Integrated Development Environment. Think of it as a fancy text editor, which points out at any mistakes we make and auto-completes our code while showing us the output (result) of our code. There are editors such as VS Code, which can be used for multiple programming languages, or those like PyCharm, which have been developed strictly with just Python in mind. In this tutorial, I will be using PyCharm.

First, let’s download our PyCharm Community edition (not the professional one) here.

After clicking through the installation, let’s launch our PyCharm editor. Create a new project and make sure you have selected the correct interpreter version as in the gif below (if you used the link above, the version should be 3.8).

What are we seeing?

On the left, we will always see our folder/directory where our project is located. On the bottom, we have the terminal which we will use to see how our code works and to detect any potential errors in there. Although nowadays users rarely use the terminal (or the command line) to navigate through their computer, as most apps have some graphical and visual interface, we will mostly interact with the terminal in the beginner tutorials, as developing a graphical user interface (GUI) is a rather complex task.

It is most likely that the way my PyCharm window looks is different from a freshly downloaded PyCharm, and that is because I manually changed the theme. To see the settings, go to Preferences –> Settings (macOS) or File –> Settings (Windows). There we can change the font, appearance, keyboard and shortcut settings and so on. The theme I use is called ‘Nord’ and it was downloaded online, and it will, therefore, most likely not appear in the base version of PyCharm.

2. Let’s create our first Python file:

Now that our computer is aware of the Python language and that we have an interactive editor to work in, let’s write our few lines of code. Click on our directory folder and click ‘New’ and then ‘Python file’. We can also create a regular ‘File’ but make sure to add ‘.py’ at the end of it. All python files end with the ‘.py’ extension!

We have now created a python file in which we can begin to write our code.

3. Our first lines of code

In the next few tutorials, we will be going over the most fundamental functions and syntax rules in the Python programming language. Do not be afraid to experiment with the examples provided!

Let’s consider the ‘print()’ function first:

print("text")

The print function is used to print a certain message onto the screen (in our case, it will be displayed in the terminal). Try writing the above code and replace the value of ‘text’ with any word or a sentence.

The first term that we have to learn concerning the above example is the word ‘string’, which refers to a sequence of Unicode characters within double-quotes. In simple terms, a Unicode character is any character in any existing natural language (i.e. English or Mandarin), and therefore, a string is a piece of textual data. The computers read all text within a string as a plain text and do not differentiate between alphabetic characters and numbers.

How do we run (execute) our code?

Now that we wrote a single line of code, we have to run or launch our code. To do so, simply right click the [filename].py and click ‘run’ as shown below. Once we do it this way once, we can always run our code by clicking the run icon on the right of our PyCharm editor.

This is it! We just wrote our first line of code in Python. If we were to print our ‘print()’ function without passing an argument to it (without putting any value between its parentheses), it would print out an empty line.

We can also surround our string with single quotes (‘ ‘) instead of double quotes (” “); the result would be identical. However, in cases where our string contains a single quote (or an apostrophe), our code editor might get a little confused.

print('I love McDonald's')

In the above example, our code would not understand why there are three single quotes and would inevitably spit out an error. For this reason, we need to decide whether to use single or double quotes according to the actual text within the quotes.

Moreover, we can also use the backslash (‘\’) to tell the code to ignore (programmers use the word ‘escape’) the following special character and interpret it as a part of the text. The two examples below would lead to the same result.

Let’s experiment with the print() function a bit more using variables.

What are variables?

Variables are one of the most important aspects when it comes to coding. We can declare the name of our variables whatever we want and store any kind of data we want inside them. I.e. we define the variable “greeting” and store the value “hello” in it. Now when we refer to ‘greeting’, the code already knows what value is stored inside of it. Also, note that in our “watermelon ” example we added three blank spaces into the string. That is completely fine, and the computer will output the value including the spaces.

greeting = "hello"
x = "watermelon   "
...

Until now, we have always passed a string to the print() function, by inserting the text value directly within the parentheses. We can try doing the same using a variable. After declaring a variable, we can simply print the variable, not the text assigned in it. Later on, we will find out that this saves a lot of time, especially when multiple values are stored within one variable.

Consequently, we can also re-declare the value of a variable, and the print() function will output the latest value:

Although the print() function might not seem as useful for now, we will be using it a lot throughout our Python programming journey, especially since it is one of the simplest ways to know how the code views our variables, and to view potential errors.

When creating variables, we have to note the following rules to avoid any errors:

Legal variable names

myname = "John"
my_name = "John"
_my_name = "John"
myName = "John"
NYMANE = "John"
myNaMe2 = "John"

Illegal variable names:

2myname = "John" = VARIABLES CANNOT BEGIN WITH A NUMBER
my-name = "John" = NO HYPHENS, ONLY UNDERSCORES AND ALPHANUMERIC CHARACTERS
my name = "John" = NO SPACES IN VARIABLES

What can be stored in variables?

We have learned that variables can store strings, or pieces of text, which are declared within single or double quotes. We are free to put whatever character (including digits and various symbols and characters) into our string.

Besides strings, we can also store integers (specific numeric values in our variables). Although we can store numbers within a string as well (i.e. “ABC123”) the computer will not differentiate between ‘ABC’ and ‘123’, and both will be read by the computer as a plain text. Compare the results below, where the variable ‘forty’ stores two integers, and the variable ‘not_forty’ stores two strings. Integers are never stored inside of double or single quotes.

Whatever we insert into our string, the computer will not know whether it is a number or not. When using the ‘+’ arithmetic operators for addition, we see that the computer knows that the value outside of double or single quotes is, in our case, a number, whereas in the second case it simply adds the two pieces of text together.

Lastly, we can also convert a string to an integer through the str() function, by putting the variable we wish to convert between the parentheses. In the below example, we declared ’20’ as an integer and transformed it to a string:

number = 20
number_converted = str(number)

and vice-versa with the int() function, we transformed the string “20” into an integer.

text = "20"
text_int = int(text)

The above is very important since our computer does not know how to add string and integers together, so we have to make sure that inside a variable, all values are converted to either strings or integers. We cannot perform an addition (or other operations) on a numerical value and string together (i.e. 20 + “apples” will result in an error since the computer is trying to find the other numerical value to add to ’20’, but there is none).

In the following code, we created a new variable called ‘twenty_integer’ in which we transformed our ‘twenty‘ string to an integer. We then created a ‘fifty‘ variable in which we added ‘thirty‘ and ‘twenty_integer‘ together. When printing ‘fifty‘, we can see that the result is ’50’.

This is all regarding variables, strings and integers in today’s tutorial. We will be dealing with all three in all further tutorials and explore more possibilities with them.

4. Execrise

To test what you have learned, try to run the code below (no need to copy it into your editor, simply use the editor provided). You will note that it gives us a TypeError. Try to fix it according to what was discussed in this article, using either the str() or int() function. Additionally, try to think of a way to add spaces after every word in the ‘complete_message’:

Leave a Reply

Your email address will not be published. Required fields are marked *