Python is a high-level programming language developed back in 1991. Since then, it has become one of the favorite languages for thousands of developers worldwide. It is open-source, and you can do anything with this language, such as data visualization, machine learning, building desktop apps, building web apps, and much more, to name a few.
1. Installing Python
Python language comes pre-installed in macOS and Linux, but you need to install the python interpreter on windows. Go to their official website of python and make sure to download the latest version of python. Let’s now start with this tutorial.
2. Install The IDE
The IDE stands for Integrated Development Environment, the software you need to write your code. Not just python, but every programming language has its IDE. Some of them can be used to write many languages such as Sublime Text IDE.
For python, dozens of IDEs are developed to work only with this language, such as Pycharm and Jupyter Notebook. Feel free to install which IDE you would like to use and after the successful installation, come back and start the tutorial by creating your very first command line:
print("Hello, World") #Print the text
If everything is working correctly, you will see a message in your IDE, whether you’ve used Pycharm or Jupyter Notebook, telling you “Hello, World,” which the print function does.
3. Variables
The most essential and first thing you need to learn in any programming language is its variables.
These variables will hold the data that you need to use in your code, such as numbers, floats, integers, words, and more, to name a few. Let’s see how you can use variables in python:
x = 5 #Variable holds number
y = "Python" #Variable holds word
z = 3.9 #Variable holds float
print(x, y, z)
Output:
5 Python 3.9
You will see the above output when you run the code, which means everything is working fine. The print function lets you print as many items as you want but separate them with a comma, as you see from the previous code.
You can also see that you can assign values to a variable without telling python the type of that variable like in other languages such as C++. It will raise an error if you don’t specify the value type. You can see the type of every variable by using this simple command that python provide:
z = 3.9 #Variable holds float
print(type(z)) #Print the variable type
After running the code above, python will tell you that this is a float variable. We’ve used the type() function and printed the result using the print function. You can try using the type() function on the other variables and see your results.
4. Comments
You can see that we’ve already used the comments inside our previous codes, which are the sentences after the “#” sign, which tells python to ignore these sentences, and it is used to explain what that line or piece of code does.
Documenting your code is an excellent way to write python codes because professional developers always use this documentation to explain what their code does. If they made it open-source, then everyone can understand it, or if you are working inside an organization with a team of developers.
5. Python Mathematics Operations
Since python is a programming language and can handle numbers, we also need a way to deal with them mathematically, meaning adding, subtracting, multiplying, and more. Let’s see an example in python:
x = 22
z = 17
print(x * z)
The previous code shows multiplying these two numbers, and the results will be 374, and the sign “*” is the multiplying in python. There are a lot of mathematics operations you can perform, and this below picture shows you their signs:
You can also apply some mathematics operations with words but not all. See this example below:
print("Python" + "Developer")
print("Python" * 2)
Output:
PythonDeveloper
PythonPython
You can see that we could add two words together and multiply the word python two times using the asterisk sign.
6. Python List
Python lists are also variables, but the only difference between the previous variables and lists is that they can hold multiple values inside one value. Let’s see an example:
x = ["Python", "Developer"] #List holds words
z = ["Python", 5, 44.6] #List holds many values type
print(x)
print(z)
Output:
['Python', 'Developer']
['Python', 5, 44.6]
You can see that python lists can hold many values type such as integers, floats, string, and more inside one variable. It is better to use one value type for one list to have a more organized code.
List items are ordered starting from 0, and it allows duplicate values. Let’s say you want to access the second element of the list. You will type this command in python:
x = ["Python", "Developer"]
print(x[1])
It will print the word “Developer” since we’ve accessed the second item with the number 1 since in python, the counting starts from 0, so to access the first element of any list, you will use the index number 0.
The list items are ordered, meaning it has a defined order, and it will not change unless you change it yourself.
Also, when you add an item to the list after you create it, it will be added to the end of the list. You can also change the list after you’ve made it means adding, removing, replacing any item of that list.
Conclusion
Thanks for reading! This simple python tutorial will help you understand the basics of this language, and we will discuss more on this language and the other variables that can hold more than one value, as you see in the lists.