Python is a high level language that was created in 1991 by Guido van Rossum .
Let us study how a computer program is executed on a computer. The diagram is somewhat simplistic and offers an overview of how a computer will work in general.
The instructions for the program are stored in the RAM and the CPU grabs an instruction from the RAM and executes it. It will then grab the next instruction and execute that and so on. What type of instructions are these ? Well these are the instructions that the CPU understands. In case of an Intel CPU the machine language is based on x86 . That is the only thing the CPU understands. It does not know about C++ , Java or any other language. It only knows about the x86 machine language. What do the instructions of this machine language look like ?
000000 00001 00010 00110 00000 100000
100011 00011 01000 00000 00001 000100
As you can imagine writing machine language instructions is a tedious and impractical task. Programmers
used Assembly language instead to do their programming. So how did the CPU run Assembly language .
Well it didn't. The Assembly language program was converted to machine language by a compiler.
What did the Assembly language instructions look like ?
MOV AL, 1h ; Load AL with immediate value 1
MOV CL, 2h ; Load CL with immediate value 2
MOV DL, 3h ; Load DL with immediate value 3
The Assembly language instruction had one to one correlation with machine language and was
slightly better in terms of using opcodes but still a tedious task. So now the high level languages
came such as Fortran, Pascal, C . For a long time programming was done in these high level
languages . These language had features like functions, procedures , structures. A programmer could
use variables and did not have to worry about mapping variables to specific addresses in memory.
However these languages involved the programmer converting the application problem in
terms of the language which still bore a close relationship to the way the CPU worked. The
concept of object oriented languages was developed and thus came languages such as Java,
C# and C++ . Now a programmer could translate the application problem in terms of objects and
classes. The way we think about problems and concepts can be correlated and organized better with
object oriented programming. As an example think of an application for the bank . We can think of
a customer with properties such as name, age, gender, address, phone. This customer could have an
account and the account could be checking or saving. An account can have properties like amount and
account number. It could also have methods like add a deposit and so on. This sort of technique can
be used in many problems where we can model the solution in terms of classes to more closely resemble
the problem domain.
Using the Hills server
Hills.ccsf.edu
We can also use our "hills.ccsf.edu" remote unix system . This is what we will be using in class and you must do your assignments on this system. The software to access and use this can be any SSH based client ftp and "Putty" type command line system.
Winscp
Putty
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
The program "putty.exe" is a single file that be saved to a folder such as "C:\putty" . We can integrate Putty with WinScp by
changing the "Option/Preferences" screen
The checkbox "Remember session password will allow you to open up Putty from WinScp without a prompt for password.
Browser
The web site:
https://codeanywhere.com
can also be used to connect to the hills server and you can do your assignments on this server.
Use the connections menu on the left hand side and choose the "SFTP" option and enter your settings as below with your username and password.
Download Python
You can install Python from the site:
https://www.python.org/downloads/
They have the installs for different operating system versions.
Windows/Cygwin
Cygwin is a unix simulator for Windows. When you install Cygiwin you can select an option to install Python . You can do this after installation also by running the "Setup.exe" again and selecting the option.
Python Versions
There are 2 versions of Python ( 2 and 3 ) that are most commonly used these days. We will be using Python 3 and discuss the differences between the 2 versions.
Compiling and Running your first Python program on Hills
Running a file with extension ".py"
Create a folder on Hills called :
cs131b
Inside that folder create a file called:
"first.py"
that will contain the contents:
print("Hello World")
From the console type:
python3 first.py
Running a shell script with python commands in it.
Create a file named "first.sh" with the contents:
#!/usr/local/bin/python3
print("Hello World")
Make the file executable with the command:
chmod 777 first.sh
Run the shell script with the command:
./first.sh
Running python commands in interactive mode
From the terminal start the interpreter using the command:
python3
At the prompt( ">>>" ) type:
print("Hello World")
IDE for Python
There are some free IDE's ( Integrated Development Environment ) for Python such as Eclipse , Pycharm. An IDE can provide debugging information and context sensitive help . An IDE is not required for this course .
ByteCodes
We have seen how we can create a file with extension ".py" and then run it with the command:
python3 first.py
In the background the Python interpreter will take the code in the file "first.py" and convert that to something called "byteCodes" . These are instructions that the Python people have made up and are similar to Assembly language instructions. Then the PVM ( Python Virtual Machine ) part of the interpreter will run the bytecodes file. Normally the user will not see the bytecodes file unless a module is imported. Let us create a file:
File: "import1.py"
var1="A variable inside a import module."
print( "import1.py" )
This has a single variable and prints "import1.py" to the console. A module in Python is a unit of compilation. Other files can use the modules. In this case the module is a single text file. Now we will create another file in the same folder that uses this module:
File: ex1.py
import import1
print( import1.var1 )
In this file we are importing the module "import1" and then printing the variable "var1" inside it. We use "import1.var1" to indicate that the "var1" variable belongs to the module "import1" .
[amittal@hills bytecodes]$ python3 ex1.py
import1.py
A variable inside a import module.
[amittal@hills bytecodes]$
As expected this ran the module first and we got "import1.py" printed to the console and then the value of the variable "var1" got printed out. If we look at the folder we can see that a new folder got created.
__pycache__
that contains the file:
import1.cpython-36.pyc
This file is the one that contains bytecodes for the module "import1.py" . If we look at the contents of this file we can see that it is gibberish ( binary code instead of text ) . We also note that the python version is placed in the name of the file.
We work with numbers using base 10 or the decimal system. When we have a number such as
245
This is interpreted as :
2 * 10 to power 2 + 4 * 10 to power 1 + 5
If we have a base such as 10 then the digits range from 0 to 9 ( 0 to base -1 ) . When we go from right to left we
multiply the digit by the base to the power increasing the power by 1 each time.
Computer work with base 2 also called the binary system. The digits are 0 and 1 . The number
101
is 5 in decimal system( 1*2 power2 + 0 + 1 ) .
If we have 3 digits then the unsigned number will look like below:
000 -- 0
001 -- 1
010 -- 2
011 -- 3
100 -- 4
101 -- 5
110 -- 6
111 -- 7
Two's complement.
A negative binary number is represented in two's complement. In this scheme the leftmost bit is "1" and represents the negative number. The right most bits are inverted and then a "1" is added to get the negative number amount.
Ex:
101
The bit "01" are inverted to form "10" and then a "1" is added to form "11" so the number "101" is actually "-3" . If we have 3 digits then the signed numbers look as below:
000 0
001 1
010 2
011 3
100 -4
101 -3
110 -2
111 -1
Base 16 also called Hexadecimal system . The digits are 0-9, A-F with A representing 10 and F representing 15.
Examples:
F0 - 240
AF - 175
The "hills.ccsf.edu" is a unix server. When a utility such as "Putty" is used then we have a command line interface that we can use to compile and run our programs.
ls
The command "ls" will list the files and folders in our current directory.
Ex: ls
Ex: ls -l
ls with the "-l" option produces a detailed list of files.
pwd
The command "pwd" lists the current directory we are in ( Present Working Directory ) .
mkdir
The command "mkdir" can be used to create a folder. Ex:
mkdir demo
cd
Changes your current folder. Ex:
cd demo
cd .. //Goes one folder up
cd //Goes to the default home folder
cd ~ //Same thing changes to the default home folder
touch
The command "touch" is used to update the access date/time of a file. It can also be used to create an empty file. Ex:
touch hello.cpp
rm
Removes a file. Use rm with options to directory and all it's contents inside it.
Ex: rm "test1"
Ex: rm -rf someFolder
cp
Copies a file to some new file.
Ex:
cp hello1.cpp hello2.cpp
mv
Moves a file to another location; deleting the original file. Can also be used to rename the original file in the same folder.
mv hello1.cpp hello2.cpp
cat
The "cat" command can be used to print the contents of a file. It can also print contents of multiple files.
tar
The "tar" command can be used to compress and extract files. To compress the folder "mymath" to a file called "mymath.gz"
tar -czvf mymath.gz mymath
To extract the compressed file use:
tar -xvf mymath.gz
This will create a folder "mymath" in the current directory and extract all the files there.
chmod
A Unix file has permissions associated with it .There are permissions for reading, writing and executing the file. This 3 permissions can be set for 3 different sets of people: the owner ( creator fo the file ) , the users in the group that the owner belongs to and the third set everyone else.
The permissions for a file can be viewed with the command "ls -l" .
[amittal@hills unix_stuff]$ ls -l
total 0
-rw------- 1 amittal csdept 0 Jan 26 12:10 first.txt
The first character will tell us if the file is a director ( with the "d" character ) or "-" if it is a normal file. Then we have "r", "w" and ""x" . For the above case the owner has permissions to read and write to the file. There are 2 ways we can manipulate the permissions. The first is with characters and the second using binary numbers.
[amittal@hills unix_stuff]$ chmod o+rwx first.txt
[amittal@hills unix_stuff]$ ls -l
total 0
-rw----rwx 1 amittal csdept 0 Jan 26 12:10 first.txt
The format is:
chmod people (+/-) rwx
Where people is "o" for others , "g" for group and "u" for the owner.
r - Read w - Write x - Execute u - The owner of the file g - The group that the file belongs to o - Anybody who is not one of the above a - All users + - Add permissions - - Remove permissions
[amittal@hills unix_stuff]$ chmod a+rwx first.txt[amittal@hills unix_stuff]$ ls -l total 0-rwxrwxrwx 1 amittal csdept 0 Jan 26 12:10 first.txt
The above changes permissions for all the 3 sets of people by giving everyone read, write and
execute permissions.
We can also use binary numbers.
: The first octet represents permissions for the owner. r w x T : The second octet represents permissions for the group. Owner: 4 2 1 7 : The third octet represents permissions for everyone else. Group: 0 0 0 0 : For each octet, start at 0 and: Other: 0 0 0 0 : +4 for read permission. : +2 for write permission. Command: chmod 700 : +1 for execute permission.
[amittal@hills unix_stuff]$ chmod 000 first.txt
[amittal@hills unix_stuff]$
[amittal@hills unix_stuff]$
[amittal@hills unix_stuff]$ ls -l
total 0
---------- 1 amittal csdept 0 Jan 26 12:10 first.txt
[amittal@hills unix_stuff]$ chmod 605 first.txt
[amittal@hills unix_stuff]$ ls -l
total 0
-rw----r-x 1 amittal csdept 0 Jan 26 12:10 first.txtThe string "605" states that 6 which in binary is "110" and that means assigns "rw" to the
owner and 5 translates to "101" so assign "rx" to everyone else.