Python — Simple programs
vvrubelIntroduction to Python
§1. What is Python?
Python is a modern general-purpose programming language initially developed by a Dutch programmer named Guido van Rossum in the late 1980s. The name comes from the popular Monty Python show, not the snake as you might think. This language has a clean, uniform and well-readable syntax and is designed to be easy to learn and use in practice.
Nowadays, Python is one of the most popular programming languages worldwide according to the TIOBE index and the number of programmers who use it is growing every day. The language has a huge community of developers around the world. If you have a problem, you can always ask other programmers for help or find a suitable answer on a site like Stack Overflow.
Developing software with Python is easy and fun :)
Python has a wide range of possible applications, especially in:
- web development
- data science (including machine learning)
- scripting (task automation, e.g. text processing or a simulation of typical user actions)
Less commonly, it is also used in desktop development.
§2. Python in data science
Python's huge popularity in recent years is mostly due to its use in data science. What makes it better than other languages for this purpose? Well, there're a number of reasons:
- its simple syntax allows people from non-programming backgrounds to use it for data processing and model training without spending much time learning a new language;
- Python supports a very large number of third-party libraries for machine learning, neural networks, statistics, and numeric calculations, which makes your job much easier;
- with Python, it is possible to collect, clean, and explore data, as well as train models and visualize the results — all in one setting;
- the Python ML developer's community is very large, so you can always find support for your tasks.
As you can see, Python does have a lot to offer for data science enthusiasts.
§3. Short history of Python
Like other programming languages, Python has gone through a number of versions. Python 1.0 was released in 1994 and laid the basic principles of the language with emphasis on simplicity.
Python 2.0 was released in 2000. This version has become very popular among programmers. Different 2.x subversions (2.6, 2.7) are still used in various projects and libraries. The symbol x in 2.x means any subversion of Python 2.
Python 3.0 was the next major version released in 2008. It broke backward compatibility with its predecessors in order to rid the language of historic clutter and make Python more readable and consistent.
So, today two similar but incompatible versions of Python are commonly in use. Throughout this course, we will learn Python 3.x.
§4. First program example
Here is a single line of Python code that prints Learn Python to be great!.
print("Learn Python to be great!")
Now, you do not need to understand how this code works, just start to appreciate the syntax looking like English :)
Overview of the basic program
In this topic, you will learn how to develop your first Python programs. Despite the fact that these programs are quite simple, they are still syntactically correct and show that programming in Python is a treat.
§1. The Hello World program
Our first example will be Hello, World! It is traditionally used to introduce beginners to a new programming language.
print("Hello, World!")
As you can see, it consists of a single line and just prints a string passed in the parentheses, but without quotes. You may run this code online (just copy it and click on the triangle) or follow these installation tips. You should get this result:
Hello, World!
Although this code is very simple, we will go through it in some more detail.
§2. Short explanation
Here, print is the name of a function. A function is a block of code that does some useful work for you, e.g. prints a text. In some sense, a function is a subprogram that can be reused within your programs. When the name of a function is followed by parentheses, it means that it was called to get the result.
Let's go further, "Hello, World!" is a Python string. All strings are surrounded by either single or double quotes, so 'Hello, World!' is also a valid string. You may replace this string with another one, and the program will print the new string. For example:
print('Python 3.x')
As you might guess, this program will print:
Python 3.x
§3. Printing quotes
Now imagine that the string you want to print already contains some type of quotation mark. If you would like to include quotes into a string, then enclose this string in quotes of another type, e.g.:
print("Yes, I'm ready to learn Python.")
Part of the string with I'm is printed correctly, because you used double quotes "..." to enclose the whole string:
Yes, I'm ready to learn Python.
If you write in the following wrong way:
print('Yes, I'm ready to learn Python.')
Your program won't know where the string starts and ends.
You can try to run all the examples using the link provided earlier. This will help you familiarize yourself with Python.
§4. Possible errors
Even this simple line of code may contain errors, most common of them are:
- putting extra indentation
print("Hello, World!")
This does not work because of extra spaces before print.
- calling the function by the wrong name
pint("Hello, World!")
This line contains pint instead of print. Make sure to refer to every function by its proper name.
- writing names in the wrong case
PRINT("All caps")
Again, Print, print and PRINT are not the same. Names are case-sensitive in Python.
- missing one or both quotes for a string
print("Python)
This does not work because of missing closing quotes.
- missing one or more parentheses
print("I have no end"
Be careful with parentheses, especially when calling a function.
Now you shouldn't have any serious trouble with such programs.
Multi-line programs
We hope you have already learned how to write simple Python programs consisting of a single line that just prints a text. However, real programs contain a significant number of lines: from tens and hundreds for small scripts to thousands and even more for large projects. So, in this lesson, you will write programs that print multiple lines.
Let's consider an example, the following code prints exactly three strings, each on a new line:
print("I")
print("know")
print("Python")
The output is:
I know Python
You can run this example here or locally if you have already installed Python on your computer.
There are other ways to print the same text using just one function call. We will consider them in the next topics.
The print function also allows you to print an empty line with no string specified:
print("I")
print()
print("know")
print()
print("Python")
Here's the output:
I know Python
However, skipping the line will have no effect:
print("And")
print("you?")
The output is:
And you?
Now it's time to solve some problems.