F3 CL Python Assignment 1: Introduction to Python
Table of Contents
What is Python? Why Python? #
Powerful #
Python is a powerful scripting langugae. You can use it to create games, web scrapers and automate works. You can use it to build softwares and web applications. You can use it for scientific computation and data analysis. You can use it for machine learning and deep learning. Being the third most popular language, it is so useful that over 40% of the developer respondents use it in their routine development in 2021, according to Stack Overflow’s annual developer survey [1] .
Easy to Learn; Elegant to Write #
Python has a much easier syntax compared to other programming languages. Below we show a program that prints ‘Hello, World!’ on the screen written in three different languages: Python , Java , and C++ .
# Python hello_world.py
print('Hello, World!')
// C++ hello_world.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
You can always write fewer lines of code with Python. Here we quote the The Zen of Python -try to run this easter egg (of course later)!
# Copy the following line of code and run it in the iteractive mode:
import this
Installing Python #
- Go to the Python official website . Your browser should show the latest version for your OS by default. The latest release for both Windows and macOS is Python 3.10.7.
- Open the python-3.10.7
.exe
or.pkg
installer. The installer comes with IDLE (the Python integrated development environment), the code editor you will be using, and pip, a Python third-party package manager. Click continue and agree to the license. Install it in the default disk.
- After installing Python, go to the terminal:
In the terminal, enter the following commands line by line:
python3 --version
pip3 --version
These two commands show the versions of Python and pip. Verify no error is shown. Please use python3
and pip3
explicitly in the future even if the command python
or pip
works as well (use python
and pip
if and only if you have added Python 3.10 to PATH). Suppose there is any error, please approach us or the Cyber Ambassadors or your CL teachers asap!
Using IDLE #
IDLE (the Python Development and Learning Environment) is an editor came bundled with the installation of Python. Although there are other IDEs (integrated development environments) on the market, but the functions IDLE provides are sufficient for beginners.
Firstly, you should find the IDLE.app/exe. It should locate in the folder Python 3.10.
Interactive Mode #
By default, IDLE is opened in shell mode (interactive mode). This is the best playground for testing any Python code snippets.
In the shell mode, you start a Python command after a prompt (>>>
or ...
). We should immediately go into some examples such that you can grasp the basic expressions in Python on your own. Try out the following commands in interactive mode.
Expressions #
The following commands are some mathematical expressions in Python. The syntax is pretty straight-forward: if you know arithmetic, you have already mastered this part! You may want to pay attention to difference between the operators /
and //
. Try to evalute the expressions 4 / 3
and 4 // 3
in the shell. What is the result?
# Only enter the commands AFTER the prompt >>>
>>> 1 + 1 # Addition
>>> 2 - 1 # Subtraction
>>> 3 * 3 # Multiplication
>>> 10 / 3 # Division
>>> (50 - 5*6) // 4 # // Integer division
>>> 8 ** 2 # 8 square
>>> 17 ** 34 # 17 to the power 34
>>> 7 % 3 # Modulus operator: returns the remainder after 7 is divided by 3
Variables and Values #
You can use the equal operator (=
) to assign a value to a variable. No result is displayed afterwards before the next prompt.
# Find the volume of a pyramid
>>> height = 10
>>> base_length = 5
>>> base_width = 8
# Variables can be ASSIGNED at the left hand side of =,
# they are EVALUATED to their VALUES at the right hand side of =, and anywhere else
>>> volume = (height * base_length * base_width) / 3
>>> volume # This evaluates the variable volume and displays the result
You can assgin values to a variable any times. Once you assign a new value to a variable, the original value is ’thrown away’ and you cannot access to it anymore. In the following example, we assign the numbers 1-10 to the same variable and print it out on the screen.
>>> x = 1
>>> # While x is smaller than or equal to 10, we first evaluate x, then
>>> # assign the value x + 1 to the variable x, and continue the iteration
>>> while x <= 10:
... x # The indent is essential
... x += 1
Exercises #
- In Python expressions, approximate $\pi$ with:
$$ \pi = \frac{9801}{2206\sqrt{2}} $$
To compute the square root of a number, try:
>>> from math import sqrt
>>> sqrt(2)
For the very mathematical-inclined, see Ramanujan–Sato series .
- Assign a value to the variable
radius
, and assign values to the variablesperimeter
andarea
given that:
$$ perimeter = 2 \times pi \times radius $$
$$ area = \pi \times radius^2 $$
You can get the constant $\pi$ by:
>>> from math import pi
>>> pi
- For a set of simultaneous equations
$$ ax + by = e $$
$$ cx + dy = f $$
where $x$ and $y$ are variables and the remainings are constants. The roots are given by:
$$ x = \frac{ed - bf}{ad - bc} $$
$$ y = \frac{af - ec}{ad - bc} $$
Assign values to $a-f$, and compute the variables $x$ and $y$.
Strings #
Strings are sequences of characters, in other words, text. Strings are enclosed in either single quotes '...'
or double quotes "..."
. Do not mix single and double quotes together. Try out the following examples:
>>> "Computer Club is the best club in spcc"
>>> 'Computer Club is the best club in spcc'
In interactive mode, Python print strings with the quotes. To produce a more readable version, try:
>>> print('Computer Club is the best club in spcc')
Strings can be concatenated (joined together) with the +
operator:
>>> 'Computer Club'
>>> ' is the best club in spcc'
>>> 'Computer Club' + 'is the best club in spcc'
>>> print('Computer Club' + ' is the best club in spcc')