Programming Tutorial
01 Basics
Installation
Windows:
Visit https://www.python.org/downloads/ and download the latest version.
MacOS:
Use homebrew,
brew install python3
Linux:
sudo apt-get update && sudo apt-get install python3
Editor &Input method
In any text editor(vim, Sublime) or IDE(Pycharm, Spyder), use English input method, not Chinese.
print("hello, world“)
File "<ipython-input-5-43e9612f56f8>", line 1
print("hello, world“)
^
SyntaxError: EOL while scanning string literal
**IMPORTANT: **
Always ensure that you give it the file extension of .py , for example, foo.py .
Comments
Notes for the reader of the program.
# Write your comments at this line in python
// Write your comments at this line in java
// Write your comments at this line in go
Literal Constants
Numbers or Strings, use its value literally.
Numbers
Numbers are mainly of two types - integers and floats.
print(18) #integer
print(3.14) #floats
Strings
A string is a sequence of characters. Strings are basically just a bunch of words.
print("hello,world") #string
Hello, World
#create hello.py
print("hello,world")
Compile & Run
Compile:Convert a source program(Source Code) and the things it depends on into instructions in native machine language(Binary Code for Go) or intermediate code(Bytecode/class file for Java, python bytecode/pyc file for python).
Run: Excuting the binary code.
To run your Java program:
-
Open a terminal window in MacOS (cmd in Windows OS)
-
Change directory to where you saved the file, for example, cd /tmp/java
-
Compile the program by entering the command javac hello.java .
$ javac hello.java
If you success in compile process, no text will be shown at the console. However, you will get a new file hello.class in the same directory as hello.java.
- Run the program by entering the command java hello.py . The output is as shown
below.
$ java hello
hello world
To run your Python program:
-
Open a terminal window in MacOS (cmd in Windows OS)
-
Change directory to where you saved the file, for example, cd /tmp/py
-
Compile &Run the program by entering the sole command python hello.py . The output is as shown
below.
$ python hello.py
hello world