category: Beginner | Python Tutorial

Category: Beginner

Getting started

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.

Related course:

Python interpreter

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.

Python IDE

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:

pycharm python IDE

REPL

What is REPL? REPL is the language shell. Its short for Read, Eval, Print and Loop. The process is:

  1. Read: take user input.
  2. Eval: evaluate the input.
  3. Print: shows the output to the user.
  4. Loop: repeat.

Related Courses:

To start the language shell, type ‘python’ and press enter.


$ python
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

python interactive shell repl

REPL

We can type all kinds of input in the interactive shell:

>>> 128 / 8
16.0
>>> 8 (8 8)
512
>>> 256 * 4
1024
>>>
Note: We’ll teach you how to start python programs in the next article.

If you get an error when typing Python, install Python or set your environment variables. The method for setting this varies per operating system.

How to run

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:

How to run Python

All Python programs are written in code, text files with lots of instructions. These are saved with the extension .py.

Note: Often a Python program is not just one file, but many files.
python program

To run a Python program, you need to have Python installed.

Python installed?

Open a terminal and type:

python file.py

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;

Use an IDE

Sometimes an easier way to execute Python programs is using a Python IDE. In a Python IDE, you simply press the ‘play’ button.

pycharm toolbar

The image above shows the header of the program PyCharm, a Python IDE. The green play button can be used to start a program.

Text Input

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.

Related Courses:

User Input

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: ")
print(name)

This will show you:

Enter a name:

You can now give keyboard input, it will be stored in the variable name.

keyboard input

Note: don’t forget to store the return variable. variable = input(“..”)

Numeric input

To get a whole number:

x = int(input("What is x? "))

To get a decimal number:
x = float(input("Write a number"))

Text output

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.

Related Courses:

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::

print("Hello World")

Run the program (from terminal: python hello.py)
If you run the program:

Hello World

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")

Results in:

python print function newline
Note: the characters \n create a new line

To print variables:

x = 3
print(x)

This will show:

3

To print multiple variables on one line:

x = 2
y = 3
print("x = {}, y = {}".format(x,y))

Will give you:

x = 2, y = 3

1237