To get started, you will need the Python interpreter or a Python IDE. An IDE is a tool that will make the experience of software development much better.
Python programs are simply a collection of text files. If you want something more sophisticated than notepad for editing, you will need a Python IDEs (recommend). A Python IDE will make programming Python easier.
You can download Python interpreter here: https://www.python.org/downloads/
The Python interpreter is a command line program, we’ll discuss it in the next lecture.
An IDE generally supports listing all program files, syntax highlighting and other features. There are lots of Python IDEs you could choose from.
Using one of these Python IDEs makes programming easier than in say, notepad. It will automatically color the text like the example below:
What is REPL? REPL is the language shell. Its short for Read, Eval, Print and Loop. The process is:
- Read: take user input.
- Eval: evaluate the input.
- Print: shows the output to the user.
- Loop: repeat.
To start the language shell, type ‘python’ and press enter.
Python 3.6.1 (default, Mar 27 2017, 01:39:26)
[GCC 6.3.1 20170306] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> 700713 + 700713 # read, eval
1401426 # print
>>> # loop
We can type all kinds of input in the interactive shell:
>>> 8 (8 8)
>>> 256 * 4
If you get an error when typing Python, install Python or set your environment variables. The method for setting this varies per operating system.
Learn how to run Python code?
You can execute Python code from the terminal or from a Python IDE. An IDE is a graphical environment that assitsts in software development.
If you are new to Python, I recommend this course:
All Python programs are written in code, text files with lots of instructions. These are saved with the extension .py.
To run a Python program, you need to have Python installed.
Open a terminal and type:
where file.py is the name of your program. If you have more than one file, the main program is often the name of the program itself;
Sometimes an easier way to execute Python programs is using a Python IDE. In a Python IDE, you simply press the ‘play’ button.
The image above shows the header of the program PyCharm, a Python IDE. The green play button can be used to start a program.
Want to get keyboard input?
To get keyboard input, use the input function. Inside the terminal you’ll be able to type text.
In Python 3 you can use the input() function, older versions of Python have the raw_input() function.
The input function has a return variable. In this case the keyboard input. If we do not store the return variable into a programs variable, we lose it. That’s why we write a variable to store the result in.
To get a text value:
name = input("Enter a name: ")
This will show you:
You can now give keyboard input, it will be stored in the variable name.
To get a whole number:
x = int(input("What is x? "))
To get a decimal number:
x = float(input("Write a number"))
Text output is one of the basics in Python programming. Not all Programs have graphical user interfaces, text screens often suffice.
You can output to the terminal with print function. This function displays text on your screen, it won’t print.
The terminal is a very simple interface for Python programs. While not as shiny as a GUI or web app, it’s good enough to cover the basics in.
Create a new program (text file) in your IDE or code editor.
Name the file hello.py. It only needs one line of code.
To output text to the screen you will need this line::
Run the program (from terminal: python hello.py)
If you run the program:
The program above prints everything on a single line. At some point you’ll want to write multiple lines.
To write multiple lines, add the ‘\n’ character:
print("Hello World\nThis is a message")
To print variables:
x = 3
This will show:
To print multiple variables on one line:
x = 2
Will give you: