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

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

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:

REPL

What is REPL?

REPL is the language shell.

Its short for Read, Eval, Print and Loop.
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

Then it starts this process:

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

Related Courses:

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:

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.

Python IDE

Python IDE : a software development tool. Compare Python IDEs.

An integrated software developent (IDE) makes software development much better!
Code with tabs, syntax highlighting, code completion and many awesome features!

Related courses:

Python IDEs

pycharm
A list of the most popular Python IDEs. These are some of the most popular.
If you are a total beginer, I recommend PyCharm or Spyder.

PyCharm

  • JetBrains Product
  • Free/Commercial
  • Closed-source
  • Linux, Mac and Windows support
  • Code Completion, Debugging, Bracket Matching, Line Numbering, Code Folding, Code Templates, Unit Testing, Integrated DB support.
  • Easy for beginners

Vim

  • Free
  • Open source
  • Cross platform, but mainly used on Linux
  • Works in terminal and over SSH (remote)
  • Code Completion, Debugging, Bracket Matching, Line Numbering, Code Folding, Code Templates, Unit Testing, Integrated DB support.
  • Hard for beginners

Spyder IDE

  • Free
  • Open source
  • Made for scientific programming
  • Windows, Linux, Mac support
  • Code Completion, Debugging, Bracket Matching, Line Numbering, Code Folding, Code Templates, Unit Testing, Integrated DB support.

Wing IDE

  • Commercial
  • Closed-source
  • Cross platform
  • Code Completion, Debugging, Bracket Matching, Line Numbering, Code Folding, Code Templates, Unit Testing, Integrated DB support.

Sublime Text

  • Commercial/Free
  • Closed-source
  • Cross platform
  • Text editor for code and markup.
  • Polished interface, syntax highlighting, tabs.
  • Code Completion, Debugging, Bracket Matching, Line Numbering, Code Folding, Code Templates, Unit Testing, Integrated DB support.

Emacs

  • Open source
  • Cross platform, but mainly Linux
  • Takes time to master
  • Works in terminal and over SSH
  • Code Completion, Debugging, Bracket Matching, Line Numbering, Code Folding, Code Templates, Unit Testing, Integrated DB support.

If you want to compare on features, check this: Python IDE comparison

Text output

we can output to the terminal with print function. In this article we’ll show you how to
output text to the screen with Python.

Related Courses:

Output

To output text to the screen you will need one line of code:

If you run the program:

Hello World

Print newline

To write multiple lines, add the ‘\n’ character:

Results in:

Hello World
This is a message
Note: the characters \n create a new line

 

Print variables

To print variables:

This will show:

3

To print multiple variables on one line:

Will give you:

x = 2, y = 3

Text Input

Want to get keyboard input?

To get keyboard input, use the 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.

Related Courses:

User Input

To get a text value:

This will show you:

Enter a name:

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

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

Numeric input

To get a whole number:

To get a decimal number:

Strings

Strings in Python can be defined using quote symbols. An example of a string definition and output below:

This will output to the terminal:

Hello World

 
Related Course:

Accesing array elements

You may access character elements of a string using the brackets symbol,
which are [ and ]. We do so by specifying the string name and the index.  

Note: Computers start counting from zero, thus  s[0] is the first character.

 
The example below prints the first element of a string.

To print the second character you would write:

Related Course:

String Slicing

You can slice the string into smaller strings. To do so you need to specify either a starting, ending index or both. Let us illustrate that in the Python shell:

>>> s = “Hello World”
>>> s[:3]
‘Hel’
>>> s[3:]
‘lo World’
>>> s[1:3]
‘el’
>>>

If no number is given, such as in s[:3] it will simply take the beginning or end of teh string. We have accessed the string as if it was an array.

If you want to output them from your program, you have to wrap them in the print command. You can store the sliced string as a new string:

There you have it! String slicing is pretty easy.

 

Split string

Want to split a string?

To split a string, we use the method .split().

This method will return one or more new strings. All substrings are returned in the list datatype.

Note: Split with the method str.split(sep, maxsplit). The 2nd parameter is optional. If maxsplit is set, it returns no more than maxsplit elements.

 
Related course:

String split

We can split a string based on a character.

Result:

[‘To’, ‘convert’, ‘the’, ‘result’, ‘to’]

Any character can be used. If you want to get sentences you could use:

This will result in:

[‘Python string example’, ‘ We split it using the dot character’, ”]

 

If statements

If statements are all about choices.

A block of code is executed based on one or more conditions. The block of code will only be executed if the condition is true.

Related Course:

If statements

Computer programs do not only execute instructions. Occasionally, a choice needs to be made. Such as a choice is based on a condition.

Python has several conditional operators:

> greater than
< smaller than
== equals
!= is not

Conditions are always combined with variables. A program can make a choice using the if keyword. For example:

The condition is shown on line 3.

When you execute this program it will always print ‘End of program’, but the text ‘You guessed correctly!’ will only be printed if the variable x equals to four (see table above).

Note: Code is executed based on the variable x. Try different numbers: 2,3,4

If else

Python can also execute a block of code if x does not equal to 4. The else keyword is used for that.

If x is set to 2, the second code block is executed. If x equals (==) four, the first code block is executed.

Take a look at the execution below:

if statement

 

if elif and else

We can chain if statements.

How?

Using the keywords elif and else. That way, we can walk through all options for the condition. Imagine a weather app: if snow, elif rain, elif thunder, else sunny. We can execute different code for each condition.

Related Course:

If elif and else

We can execute blocks of code using if-statements. We can also do that if we have multiple conditions.
Lets start with an if statement, like so:

It will make the decision based on the value of x, also known as the condition.

x smaller than 5

What if you want to have multiple cases?
In that case you can use elif