Python Introduction

What is Python?
Python is a high-level, open-source, and general-purpose programming language designed by Guido van Rossum in 1991. The language has gained significant popularity due to its simple and readable syntax, ease of learning, and a large user community. Python supports various programming paradigms such as object-oriented, procedural, and functional programming. It is used in many fields, including web development, data science, artificial intelligence, automation, and data analysis. The availability of powerful libraries such as Django, NumPy, Pandas, and TensorFlow has made Python one of the most widely used programming languages in the world.
Goal Setting
The goal of this series of articles is to teach the mindset of a computer scientist. This mindset is a combination of the best features of mathematics, engineering, and natural sciences. Like mathematicians, computer scientists use formal languages to express ideas (particularly computations). Like engineers, they design things and convert components into systems while evaluating the pros and cons of different options. Like scientists, they observe the behavior of complex systems, formulate hypotheses, and test predictions.
The most important skill for a computer scientist is problem-solving. Problem-solving is the ability to formulate problems, think creatively about solutions, and present a solution clearly and precisely. Interestingly, the process of learning programming provides an excellent opportunity to practice problem-solving skills. That’s why this chapter is titled “The Programming Way.”
On one level, you will learn programming, which is a useful skill in itself. On another level, you will use programming as a tool to achieve a goal. Over time, this goal will become clearer.
What is a Program?
A program is a sequence of instructions that specifies how a computation should be carried out. This computation could be something mathematical, like solving a system of equations or finding the roots of a polynomial. But it could also be symbolic, like searching and replacing text in a document, or something graphical like image processing or video playback.
The details of how this is done vary between languages, but there are several basic instructions that exist in nearly every language:
- Input: Getting data from the keyboard, a file, a network, or any other device.
- Output: Displaying data on the screen, saving it to a file, or sending it over a network.
- Math: Performing basic mathematical operations like addition and multiplication.
- Conditional Execution: Checking certain conditions and executing appropriate code.
- Repetition: Performing an action repeatedly, often with some variations.
Believe it or not, that’s nearly all there is to it. Every program you’ve used, no matter how complex, consists of instructions that are almost identical to these. So, you can think of programming as the process of breaking down a large, complex task into smaller and smaller parts until these small parts are simple enough to be carried out with one of these basic instructions.
Running Python
One of the challenges when starting with Python is that you may need to install Python and related software on your computer. If you are familiar with your operating system, especially if you are comfortable with the command line, installing Python should not be difficult. However, for beginners, learning how to manage the system and program at the same time might be a bit tricky.
To avoid this issue, I recommend running Python in a browser at first. Once you are more comfortable with Python, I will provide suggestions for installing Python on your computer.
There are several websites you can use to run Python. If you’ve already found one, feel free to use it. Otherwise, I recommend using PythonAnywhere.
There are two versions of Python: Python 2 and Python 3. These two versions are quite similar, so if you learn one, you can easily transition to the other. In fact, you will notice a few small differences, but they won’t be significant for beginners. This series of Python tutorials is written for Python 3, but some notes on Python 2 are also included.
The Python interpreter is a program that reads and executes Python code. Depending on your environment, you might start the interpreter by clicking on an icon or typing python
in the command line. Once you begin, you should see output like this:
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The first three lines contain information about the interpreter and the operating system it is running on, so they may vary for you. However, you should check that the version number, which in this example is 3.4.0, starts with the number 3, indicating that you are running Python 3. If it starts with the number 2, it means you are running Python 2.
The last line is a prompt message indicating that the interpreter is ready to execute the entered code. If you enter a line of code and press Enter, the interpreter will display the result.
>>> 1 + 1
2
Now you’re ready to get started. From now on, I’ll assume that you know how to start the Python interpreter and execute code.
Your First Program
Traditionally, the first program you write in a new language is called “Hello, World!” because all it does is display the message “Hello, World!” In Python, this program would look like this:
>>> print('Hello, World!')
This is an example of a print statement, although it doesn’t actually print anything on paper. Instead, it displays the result on the screen. In this case, the output is:
Hello, World!
The quotation marks in the program indicate the start and end of the text that should be displayed; these marks won’t appear in the output.
The parentheses indicate that print
is a function. We’ll talk about functions in future lessons.
In Python 2, the print statement is a bit different. It is not a function, so it doesn’t use parentheses:
>>> print 'Hello, World!'
This distinction will make more sense soon, but for now, that’s enough to get started.
Arithmetic Operators
After “Hello, World!”, the next step is performing mathematical calculations. Python provides operators, which are special symbols that represent calculations like addition and multiplication.
The operators +
, -
, and *
are used for addition, subtraction, and multiplication, respectively, as shown in the following examples:
>>> 40 + 2
42
>>> 43 - 1
42
>>> 6 * 7
42
The /
operator is used for division:
>>> 84 / 2
42.0
You might wonder why the result is 42.0
instead of 42
. This will be explained in the next section.
Finally, the **
operator is used for exponentiation, which means raising a number to a power:
>>> 6**2 + 6
42
In some other languages, the ^
symbol is used for exponentiation, but in Python, this operator is a bitwise operator called XOR. If you’re not familiar with bitwise operators, the result might surprise you:
>>> 6 ^ 2
4
I won’t cover bitwise operators in this tutorial, but you can learn more about them here.
Values and Data Types
A value is one of the basic elements that a program works with, such as a number or a character. Some of the values we’ve seen so far are: 2
, 42.0
, and 'Hello, World!'
.
These values belong to different data types: 2
is an integer (int
), 42.0
is a floating-point number (float
), and 'Hello, World!'
is a string (str
) because its characters are grouped together as a string.
If you’re unsure about the type of a value, the interpreter can tell you:
>>> type(2)
<class 'int'>
>>> type(42.0)
<class 'float'>
>>> type('Hello, World!')
<class 'str'>
In these results, the word “class” means “category”; data types are categories of values.
It’s no surprise that integers are of type int
, strings are of type str
, and floating-point numbers are of type float
.
But what about values like '2'
and '42.0'
? They look like numbers, but since they are enclosed in quotes, they are considered strings.
>>> type('2')
<class 'str'>
>>> type('42.0')
<class 'str'>
These are strings.
When writing a large number, you might be tempted to use commas (,
) to separate groups of digits, like 1,000,000
. This number is not valid in Python, but here’s what happens:
>>> 1,000,000
(1, 0, 0)
This is not what we expected! Python sees 1,000,000
as a sequence of integers separated by commas. We will learn more about this type of sequence later.
Natural and Formal Languages
Natural languages are languages that people speak, such as English, Spanish, and French. These languages were not designed by humans (although humans try to impose certain structures on them) and have evolved naturally.
Formal languages are languages that are specifically designed by humans for particular purposes. For example, the symbols mathematicians use to represent relationships between numbers and symbols form a formal language that works well for showing relationships between numbers. Chemists use a formal language to represent the chemical structure of molecules. And most importantly:
Programming languages are formal languages designed to express computations.
Formal languages usually have precise syntactic rules that define the structure of sentences. For example, in mathematics, the expression 3 + 3 = 6
has correct syntax, but 3+ = 3$6
does not. In chemistry, H2O
is a syntactically correct formula, but 2Zz
is not.
Syntactic rules come in two types: those related to tokens and those related to structure. Tokens are the basic components of the language, such as words, numbers, and chemical elements. One problem with 3+ = 3$6
is that $
is not a valid token in mathematics (at least, as far as I know). Similarly, 2Zz
is not valid because there is no element with the symbol Zz
.
The second type of syntactic rules involves how tokens are combined. The equation 3 + /3
is illegal because, although +
and /
are valid tokens, they cannot be placed together. Similarly, in a chemical formula, a subscript must follow the element name, not precede it.
This sentence @
is a well-formed sentence in English but contains invalid tokens. It has all the valid tokens, but its structure is incorrect.
When you read a sentence in English or a statement in a formal language, you must identify its structure (although in natural language, this is done unconsciously). This process is called parsing.
Although formal and natural languages share many characteristics—tokens, structure, and syntax—there are also differences:
- Ambiguity: Natural languages are full of ambiguities, which people deal with using semantic clues and other information. Formal languages are designed to be almost or completely unambiguous, meaning every statement has exactly one meaning, regardless of context.
- Redundancy: To compensate for ambiguity and reduce misunderstandings, natural languages use a lot of redundancy. This is why they are often long-winded and detailed. Formal languages have less redundancy and are more concise.
- Literalness: Natural languages are full of idioms and metaphors. If I say “the coin dropped,” there probably isn’t a coin, and nothing is falling (this idiom means that someone has figured something out after some confusion). Formal languages say exactly what they mean and nothing more.
Since we all learn natural languages from childhood, it can sometimes be difficult to adapt to formal languages. The difference between formal and natural languages is like the difference between poetry and prose, but more so:
- Poetry: Words are used for both sound and meaning, and the whole poem creates a particular feeling. Ambiguity is not only common but often intentional.
- Prose: The exact meaning of words is more important, and the structure determines the meaning of the sentence. Prose is more analyzable than poetry, but it can still have ambiguity.
- Programs: The meaning of a computer program is exact and unambiguous, and it can be fully understood by analyzing the tokens and structure.
Formal languages are more compact than natural languages, so they require more time to read. Also, structure is important, so reading them from top to bottom and left to right is not always the best method. Instead, learn to analyze the program in your mind, identify the tokens, and interpret the structure. In the end, details matter. Small mistakes in spelling and punctuation that can be overlooked in natural languages can make a big difference in formal languages.
Debugging
Programmers make mistakes. For fun reasons, programming errors are called “bugs,” and the process of finding them is called debugging.
Programming, and especially debugging, can sometimes provoke strong emotions. If you’re facing a tough bug, you might feel frustration, anger, or embarrassment.
There’s evidence that people naturally react to computers as if they were people. When computers are working well, we view them as our colleagues, and when they are stubborn or disrespectful, we respond to them in the same way we might respond to stubborn or disrespectful people.
Being prepared for these reactions may help you cope better. One approach is to think of the computer as an employee with specific strengths (such as speed and precision) and specific weaknesses (such as a lack of empathy and inability to understand the big picture).
Your job is to be a good manager: find ways to exploit its strengths and reduce its weaknesses. You also need to find ways to use your emotions to engage with the problem without letting your reactions interfere.
Learning to debug can be exhausting, but it’s a valuable skill that is useful for many activities beyond programming.
Glossary
- Problem Solving: The process of formulating a problem, finding a solution, and expressing it.
- High-level language: A language like Python that is designed to be human-readable and easy to write.
- Low-level language: A language designed to be easy for computers to execute; also called “machine language” or “assembly language.”
- Portability: The feature of a program that allows it to run on multiple types of computers.
- Interpreter: A program that reads and executes another program.
- Prompt: Characters displayed by the interpreter to indicate it is ready to receive input from the user.
- Program: A sequence of instructions that specifies how a computation is performed.
- Input: Data received from the keyboard, a file, the network, or other devices.
- Output: Data displayed on the screen, stored in a file, or sent through the network.
- Math Operations: Performing basic mathematical operations like addition, subtraction, and multiplication.
- Conditional Execution: Checking specific conditions and executing appropriate code.
- Repetition: Performing an action repeatedly.
- Token: The smallest meaningful unit in a formal language, like words or numbers in natural languages or specific instructions in programming languages.
- Syntax: A set of rules that defines how tokens are combined in a language.
- Structure: The way tokens are organized and ordered in a language.
- Formal Language: A language specifically designed by humans for specific applications. Programming languages are a type of formal language.
- Natural Language: Languages naturally used by people for communication, like English, French, or Persian.
- Parser: A program responsible for analyzing a formal language to identify its correct structure.
- Debugging: The process of identifying and correcting bugs or errors in programming.
- Bug: An error or mistake in code that causes the program to malfunction.
- Interpreter: A program that reads and executes code written in a programming language.
- Data Type: A category of values that indicates what kind of data a particular value belongs to (e.g.,
int
for integers andstr
for strings).