Python — Your first script

And a first look to PyCharm IDE

Girolamo Pinto
11 min readMar 12, 2022
Photo by Oskar Yildiz on Unsplash

Welcome back, fellow readers!

Today, for the Py with me rubric, I want to share with you a new story about our favourite programming language: Python. These stories obviously are intended for those who want to learn to code in Python, so basically for those who are beginners in this beautiful world. Gradually, when I’ll become more expert in this field, I’ll start sharing more complex projects.

Today we’ll see how to start coding in Python, we’ll see three methods and at the end of the guide you’ll be able to write you very own first script in Python.

Are you excited?

Let’s start!

First steps

Before to go in deep in today’s topic, i suggest you to read, if you haven’t done it yet, this article of mine about installing Python 3 in Microsoft Windows.

Note: take a look to the list that I’ve created about Python, Py with me, in this list I’ll share everything I know and everything I’ll learn about Python, so stay tuned.

Now, just a couple of things. You should know that you can code in Python in a lot of different way, you just have to choose one. My suggestion is always to download an IDE (Integrated Development Environment) and to start learning it. I’ll explain why in the following sections.

Here we are some examples on how to start coding in Python:

  1. Using the Terminal
  2. Using a web interpreter
  3. Using an IDE (Integrated Development Environment)

In this article we will see these three ways to code in Python and to start learning about this particular programming language.

Python in Terminal

For those who aren’t so skilled in programming or nevere seen the Terminal, well Terminal is also known as command lines or consoles, allow us to accomplish and automate tasks on a computer without the use of a graphical user interface.

You can open up the Terminal by:

  1. Typing on your keyboard Windows button + “r”, then in the box that appears you should just type “cmd”, et voilà, your console is ready and you can write on it.
Where you see the white pen, there will be your name

Then you just have to type python` on the right of the sign > and you can code in Python.

From here we can start to code in Python whatever we want. For example we want to ask to Python to perform some calculations. To do that we can type the folowing code: print(10+10) and we can see the result above.

The part in red box is our code

Yuuhuuu!! Congratulations, you’ve just typed your first code in Python! Are you excited about that?

To understand this, if you’re not familiar with other programming languages, we’ve just asked to Python to perform a simple calculation: 10 + 10. As we expected the result was 20 and through the function print() we’ve just asked to Python to litterally “print” the result on the screen.

The print() function is very powerfull in Python language and in many other programming languages, maybe in another article I’ll explain better the potential of this particular function. For now just remember that print() it means : I want to print this particular thing on the screen.

Project Jupyter

To start in an easy way, I suggest something that helped me so much while I was starting to learn Python: online IDE.

Exactly, you read that right! To code in Python you don’t need to install an IDE, you can code using an online IDE, like Jupyter Notebook.

Note: You can code by using your web browser, but this is suggested just for small scripts, not for a whole project.

As you can read on the official page of Project Jupyter:

Project Jupyter builds tools, standards, and services for many different use cases.

Indeed, Jupyter offers the possibility to create scripts in Python, C++, R, just using your web browser. This is possible thanks to Jupyter Notebook. To open it up just go on Project Jupyter website, then scroll down to the button Try it in your bowser.

Click on it and it should open another window where you just have to scroll down and click on Jupyter Notebook, that’s the one we use for coding in Python.

Once you’ve clicked on it, you will see the Intro for Jupyter Notebook, I suggest you to read this informations, they could be very useful.

But if you’re a lazy person like me, just skip all of these bunch of useful informations about how to use correctly Jupyter Notebook and follow these steps to start coding in Python:

  1. Click on File in the upper left corner
  2. Move the cursor to New tab in the dropdown menu
  3. Click on Notebook

It will open another tab in your browser, here you can see a small window that asks you to choose a kernel, just select the default one.

Note: For those who doesn’t know what is a kernel, well the kernel is a computer program at the core of a computer’s operating system that has complete control over everything in the system. It is the portion of the operating system code that is always resident in memory, and facilitates interactions between hardware and software components. In our case we are using a notebook kernel. A notebook kernel is a “computational engine” that executes the code contained in a Notebook document. The Pyolite kernel, referenced here, executes python code.

Now we can type our code and start coding in Python, let’s try the example that we made before.

Remember: to execute a portion of code in Jupyter Notebook we can’t just type Enter on our keyboard, we should press Shift + Enter.

It works! Great!

Note: Jupyter offers also the chance to download it and to use it directly on your machine without using the web browser.

Python in IDE (PyCharm edition)

The examples above can help us to write small chunks of code in Python, they aren’t useful for big project in Python. First because it’s harder to detect errors while writing the code and second, because we can’t create a lot of project files or using files in our local machine.

So, to accomplish our desire of coding big projects in Python, let’s try to install an IDE that will help us to be more efficient while writing Python code.

The IDE (Integrated Development Environment) that I chosen for this guide is: PyCharm. Since PyCharm is computer program we need to install it on our machine, so follow these easy steps to install it fast:

Windows installation

  1. Go to JetBrains website
  2. Click on Download button under PyCharm Community Edition tab
  3. Wait for download…
  4. Click on downloaded .exe file in your Download folder and install PyCharm Community on your machine

Click on PyCharm Community Edition icon and start the IDE.

Once you opened the program, click on File, in the upper left corner, then in the drop down menu click on New Project… . You’ll see this window:

As Location choose the location you prefer, in location part if you change the last part of the path you change also the name of the project, in my case I named the project pythonProject1. The Virtualenv is the virtual environment of PyCharm, it’s perfect for now. As Base interpreter choose the most updated version of Python, you can check it by looking at the path, in my case is Python310, that stands out for Python 3.10 (at the time of writing Python 3.10 is the most recent version). Last, check the Create a main.py welcome script option. When you’ve done, the PyCharm window shoul look like this:

Before to write our code, let’s take a look to PyCharm window

Now let’s write some code in PyCharm to start with our first script. We can use the example above to make sure that everything works fine.

It works! Great!

In the upcoming section you’ll write your very first script. Are you ready?

First script in Python

Before to start we should talk about Python programming language. Indeed, learning a new programming language it’s like learning a new language when you land in another country: in our case the country is our machine and the language which is used by this machine it’s called machine language. Our purpose is to communicate with this machine and Python programming language helps us by translating what we write in Python into machine language.

Basically a programming language is any set of rules that converts what we write in that specifically programming language, into machine language, to talk with the machine explaining it what we want.

But what are the rules of Python? Well, Python is a high-level, general-purpose programming language, this means that you can use it to do whatever you want. High-level, instead, it means that you can write in Python almost like you write in english.

Syntax

In linguistics, syntax is the study of how words and morphemes combine to form larger units such as phrases and sentences.

In programming languages syntax refers to the rules that define the structure of a language. Syntax in computer programming means the rules that control the structure of the symbols, punctuation, and words of a programming language.

In Python there’s an important rule that you shouldn’t understimate: indentation. Whenever you write your code remember to indent it, because if you don’t do that this will lead to an IndentationError.

This means that when you should write something like “if number1 is greater than number2, then print number1 greater than number2”, you should write:

As you can see the print() function is more to the right than the if() statement. This is the indentation. You can do the indentation by pressing the space bar (4 spaces are enough) or by pressing the Tab (by default are 4 spaces).

If I write it without using the indentation, the result is:

As you can see my compiler raised an error: an IndentationError, it means that I forgot to indent my code. This happens because Python doesn’t anything to close the line, like ;(semicolon) in C++, neither parentheses to open the body of a function or statement, like {…}(curly brackets) in Swift. So the onlye way to make understand the compiler that a part of code is a body of a function or statement it’s the indentation.

Variables

As you can see on the example above, I wrote two numbers, 10 and 20, and I wrote them in this particular way: number1 = 20 and number2 = 10.

Well number1 and number2 are called variables. Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.

In Python, variables are created when you assign a value to it: number1 = 20.

In other programming languages you should specify the type of a variable (integer, string, boolean), in Python you should just assign them the value and the compiler understand the type.

Don’t you believe me? Let’s try. In Python it exists a function called type()` that tells us the type of a variable:

As you can see the compiler said that the variable1 it’s an integer value and it’s true because we assigned to variable1 the value 10.

Differently from math the = sign doesn’t mean it’s equal to, but through this sign we assign, to what we wrote on the left of the =, the value on the right of the =. So basically it should be read from right to left.

The if statement

Last but not least it’s the if(): statement. In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out.

Specifically the if(): statement perform an action if a particular condition it’s true. Condition that should be written in parentheses. To start the if(): statement we use a colon : to say to our compiler that a particular statement is starting.

The else statement

As you can imagine the else: statement follows the if(): statement. It performs an action if the condition in the if(): statement result to be false. It takes no conditions and you start it with a colon :.

The print function

In Python the print() function has a lot of potential and I should write an entire article about the print() function. For now I would say that: if you want to print something on the screen you should use the print() function and you have to put your text between single '’ or double "” quotation marks. In this way: print("The text between quotation marks will be printed on the screen") or print('Also this text will be printed on the screen').

Let’s code

Now we have all the necessary tools to perform our first script in Python.

Let’s see how to compose an if and else statement.

What we want to see in this particular script is: we have two numbers and we want to know if the first number is greater than the second or viceversa.

Congratulations! You’ve written your first script in Python! Stay tuned for other tutorials and try also by yourself to search for beginners tutorials on the internet: it’s the best way to learn a new programming language.

See ya, to the next article about Python! Cheers!!🍻

References

https://jupyter.org/, https://www.jetbrains.com/pycharm/download/#section=windows

--

--

Girolamo Pinto

Computer Engineer student. Python developer. C# and Unity addicted. Music, food and Formula 1 lover.