A problem that many coding beginners face is finding the right coding language that suits them. Coding languages are often hard to pick up and require a steep learning curve to understand all the code and everything involved. This is where Python comes in.
What Is Python?
Python is a very popular and high-level programming language that offers a ton of flexibility. Python mainly emphasizes readability by using a much more simplified syntax than other coding languages.
Compared to other programming languages, Python is relatively quick and easy to learn thanks to its relatively simple syntax and uncomplicated nature. In Python, a new command is issued simply by creating a new line, in comparison to other languages which use semicolons <;> or parentheses <()>.
What Can Python Do for Me?
Python can be used to develop a multitude of software applications. To list only a few:
- Python can be used to develop video games
- It can connect to back-end database systems for easy-access
- It can be used to perform complex math equations
- Python can be used to develop web applications
As with any programming language, Python can be used to develop much more than what I have listed above. These list items only give a glimpse into what Python can be used for.
Programming languages are often only limited by the creativity of the coder, and Python is no different.
Getting Started With Python
To begin coding in Python I have listed the steps below; beginning with how to install Python and an associated IDE (Integrated Development Environment). This followed by walking through the steps of coding a relatively simple Python application. If you missed it, there is an embedded YouTube video near the top of this blog post that walks through these steps in more detail.
How to Install Python
The first step in the installation process involves downloading the official Python .exe installer and then running it on your system.
The most up to date version available can be found under the downloads section of Pythons website. If running windows, then you can simply download the latest release version as prompted on the site. If using Linux/UNIX or Mac OS, then simply select them from below the download button as shown.
Depending on whether you are working on an existing project or starting from scratch, you may want to download a specific version of Python. For example, if the project you are attempting to work on was coded in a previous Python version, let us say 2.7.2, then you will want to download that version.
For any new coders, however, I would recommend downloading the most up to date version.
Once you have downloaded the Python Installer simply open the installer and select Install Now.
This will start the installation process and will take a few moments. That is it! You have now successfully installed Python!
Now, to open it up simply type “py” in the command console or search up “Python” in your systems search bar and select it to open the application. This way you can directly code into Python using a command-line interface.
I would highly recommend, though, that you install an Integrated Development Environment such as PyCharm. PyCharm makes the coding experience way more engaging and overall much better!
How to Install PyCharm
Now to install an Integrated Development Environment (IDE) for you to use Python within. Although unnecessary, an IDE will overall make life easier when learning to code in a new language, as it will point out errors as well as provide a plethora of additional quality of life improvements.
There is a multitude of IDE’s available for Python, however the IDE I have found the most effective is PyCharm. PyCharm is a great IDE that enables more productivity and helps a lot when learning to code in Python.
To install PyCharm visit the download page here, then choose the correct operating system and select “Download”. The community version will suffice, but the professional version allows for much more features but costs money once the trial period ends.
Once the download is complete, follow the steps within the installer and wait for the program to finish installing. Once finished installing, simply type PyCharm into your search bar and hit enter to launch the application. Or locate the file folder where you have installed the application and launch it from there.
Getting Started With Python
Now that you have installed both Python and PyCharm or a similar IDE, it is time to start creating your first project in Python!
Before I begin, I would like to share that there are a ton of additional resources available online to assist you in learning Python. One of my favourite websites for learning new programming languages is W3Schools. W3 has a ton of helpful tutorials for learning all kinds of coding languages and I have found it to be very helpful in tons of situations.
Creating Your First Python Project
Go ahead and open PyCharm which you should have installed in the previous step and select ‘File’ –> ‘New Project…’. From here; under ‘Location:’ choose any file folder on your system that you would like to use as your Python projects workspace. Deselect the ‘Create a main.py welcome script’ checkbox and leave all the other settings as-is.
It will take a few moments, but when the project first opens it will be empty with a bunch of files and libraries along the left side. As shown below
At this point, right click on the ‘venv’ library root folder and select ‘New’ –> ‘File’
Name your file something relative to the project you are working on, ensuring that it ends with “.py” (ignore the double quotes). Alternatively, select ‘New’ –> ‘Python File’ and it won’t need the “.py” added to the end of the filename, as it will do that automatically.
Once the file is created, it is time to start coding!
Basic Python Commands
Feel free to follow along by typing out the code segments into your PyCharm IDE to see the outputs. To run your Python file right-click it on the left side-bar and select ‘Run’, or hit the green play button on the top right.
The output of the code will appear in the bottom section of PyCharm.
The first basic Python command is print, which acts in the same way as Java’s “System.out.print”. The code below will print out a simple “Hello World!” statement.
To create a comment, use the # symbol to distinguish that line is to be ignored by the code. Comments can be useful to help remember what specific parts of code will do.
#The following line prints out "Hello World!" print("Hello World!")
One of the most important commands when it comes to coding is the if statement.
In Python, if statements are required to end with a colon ‘:’ and anything within that if statement needs to be indented. The following code determines whether the first equation is greater than the second equation and if it is, displays a message.
if 5*2 > 5/2: print("5*2 (10) is greater than 5/2 (2.5)")
A very important aspect of coding is variables. In Python, creating a variable is extremely simple. There is no specific code you need to type to declare a variable, instead, a variable is created as soon as you assign a value to it.
You can separate text and a variable in a print statement by using a comma ‘,’ or by using a plus ‘+’.
variable = "My first ever variables!" x = 7 y = "Jordan" print(x) print("My name is",y) print("These are" + variable)
Run the application. You may notice that a comma adds a space before the variable while plus does not.
In Python, declaring a type for a variable such as “int”, or “string” is not needed. But if you require a variable be of a certain type you can do so by casting it to that type as shown:
x = int(7)
In Python, collecting user input is very easy compared to other coding languages. User input means that you can ask the user using the application for input based on what is required. For example: typing a username or password.
You are then able to assign that input into a variable to be able to use it later or just display it.
name = input("Enter your name:") print("Your name is:",name)
Creating a Python Application
With this knowledge about the basics of Python, you are now ready to design your very first Python application. This application will be a very basic math calculator that will be able to add, subtract, multiply, or divide two numbers. It will take in user input, and perform the equations based on the user inputs.
To begin, we first must assign each of the user inputs into variables. Since the two numbers will be integers, we can assign those to type “int”. This is needed in order to perform math calculations on those numbers later on. To do this, simply add int() to the variable and place the input within those brackets, as shown below.
firstNumber = int(input("Enter the first number in the equation")) secondNumber = int(input("Enter the second number in the equation")) operator = input("Would you like to add (+), subtract (-), multiply (x), or divide (/)? Respond with either +,-,x, or /")
Since we will be testing the operator input with if statements, it is important to ensure the user knows exactly what inputs are acceptable.
Next, to implement our if statements to decipher whether the equation will add, subtract, multiply, or divide. In coding, a single = sign is used to assign values, while two, or double == sign will test if the two values equal to each other.
if operator == "x": print(firstNumber * secondNumber) if operator == "/": print (firstNumber / secondNumber) if operator == "+": print (firstNumber + secondNumber) if operator == "-": print (firstNumber - secondNumber)
At this point, the application will be fully functional, but let’s add some more to the print statements so the application displays the full equation and answer
if operator == "x": print(firstNumber,"x",secondNumber,"=", firstNumber * secondNumber) if operator == "/": print(firstNumber,"/",secondNumber,"=", firstNumber / secondNumber) if operator == "+": print(firstNumber,"+",secondNumber,"=", firstNumber + secondNumber) if operator == "-": print(firstNumber,"-",secondNumber,"=", firstNumber - secondNumber)
And that’s it! Your very first application in Python! Feel free to add any other features you wish to this app, as it’s yours now! At this point the only thing to hold you back is your own creativity.
This tutorial blog post barely scratches the surface of what is possible within Python. I don’t know about you, but I am excited to further my knowledge in this subject and get better at designing applications using this coding language!
If you are looking to improve your knowledge on this subject, don’t forget about W3schools as a resource. They have so many tutorials to help you learn more about Python.
Finally, in the coming weeks, I will be publishing a second Python article discussing the programming language in more depth, and walking through how to make a more advanced application. I look forward to seeing you there!