1. Introduction to Python Programming
Just a quick introduction
- Extended readings:
- What is a program?
- Why Python?
- Python Installation
- Your first Python program
- Arithmetic operators
Problem solving is one of the most important skills that everyone should learn and practice. The process of learning to program (especially using Python) is an excellent opportunity to practice problem-solving skills.
Extended readings:
-
https://avc.com/2011/10/program-or-be-programmed/
When human beings acquired language, we learned not just how to listen but how to speak. When we gained literacy, we learned not just how to read but how to write. And as we move into an increasingly digital reality, we must learn not just how to use programs but how to make them. In the emerging, highly programmed landscape ahead, you will either create the software or you will be the software. It's really that simple:Program, or be programmed.
What is a program?
A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial; it can also be a symbolic computation, such as searching and replacing text in a document or something graphical, like processing an image or playing a video.
Question: What are the basic elements of a program?
Why Python?
- Python is a general-purpose, high-level programming language designed by Guido van Rossum.
- Python is really (yes, really) easy to learn.
- Lot of libraries: network, files, GUI, database...
- Python is simple and elegant.
Extended readings:
- https://docs.python.org/3/tutorial/appetite.html
- The Zen of Python. https://www.python.org/dev/peps/pep-0020/
Why Python 3?
Short version: Python 2.x is legacy, Python 3.x is the present and future of the language.
- https://www.python.org/doc/sunset-python-2/
Python Installation
Go to https://www.python.org/downloads/. Select the latest version of Python 3.9 (currently Python 3.9.7), click 'download'. Before clicking 'Install Now', check 'Add Python 3.9 to PATH'.
Python interpretor
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 by typing 'python' on a command line. When it starts, you should see output like this:
Check the version number.
The last line is a prompt that indicates that the interpreter is ready for you to enter code. If you type a line of code and hit the Enter key, the interpreter displays the result:
Now you’re ready to get started.
Using IDLE
IDLE is the standard Python development environment. Its name is an acronym of "Integrated DeveLopment Environment". It works well on both Unix and Windows platforms. It has a Python shell window, which gives you access to the Python interactive mode. It also has a file editor that lets you create and edit existing Python source files.
Your first Python program
Before starting this exciting journey, I want you to read the following sentences three times:
DO NOT copy/paste other people's code. Type it out!
Now, start Python interpreter or IDLE.
Traditionally, the first program you write in a new language is called "Hello, World!" because all it does is display the words "Hello, World!". In Python, it looks like this:
print('Hello, World')
Congratulations! You just finished your first Python program!
This is an example of a print
statement, although it doesn’t actually print anything on paper. It displays a result on the screen.
Q: What are the parentheses for? What about quotation marks?
Let's try something else.
1 + 1
exit()
Oops, where is the code? How can I display "Hello, World!" again?
Use text editor
You can use Notepad to write Python program. Remember to save the text file as .py file. I recommend Visual Studio Code as your default Python program editor.
https://code.visualstudio.com/
Why VS Code?
- Highlighting syntax.
- Built-in Git. You need a GitHub account. https://github.com/
- Running and debugging using Python extension. https://marketplace.visualstudio.com/items?itemName=ms-python.python
Now, create a file hello.py
. (Naming convention: modules (filenames) should have short, all-lowercase names, and they can contain underscores.)
Type in the following code (don't forget to change to your first name):
print('Hello, Zhi')
Then execute this Python program in Command Prompt.
Cool! Greeting from your machine.
However, if we create a file calc.py using the below code:
39 + 41 + 33
What do we get after executing this program?
python calc.py
Nothing.
We need to modify the code as below:
print(39 + 41 + 33)
2021 - 2000
15 / 3
Q. Why is the result 5.0
instead of 5
?
Finally, the operator **
performs exponentiation; that is, it raises a number to a power:
2 ** 6
Exercise 01
Whenever you are experimenting with a new feature, you should try to make mistakes. For example, in the "Hello, world!" program, what happens if you leave out one of the quotation marks? What if you leave out both? What if you spell print wrong?
- In a print statement, what happens if you leave out one of the parentheses, or both?
- If you are trying to print a string, what happens if you leave out one of the quotation marks, or both?
- You can use a minus sign to make a negative number like
-2
. What happens if you put a plus sign before a number? What about2++2
? - In math notation, leading zeros are ok, as in
02
. What happens if you try this in Python? - What happens if you have two values with no operator between them?
Exercise 02
Rewrite calc.py to solve the following questions.
- How many seconds are there in 42 minutes 42 seconds?
- How many miles are there in 10 kilometers? Hint: there are 1.61 kilometers in a mile.
- If you run a 10 kilometer race in 42 minutes 42 seconds, what is your average pace (time per mile in minutes and seconds)? What is your average speed in miles per hour?