Getting started

Python is a programming language with a clean syntax that is easy to learn. Python programs can be run under all desktop computers. 

It used in many application domains including:  Web and Internet Development,Scientific apps,Desktop apps, education and general software applications.

Recommended course:
Complete Python Bootcamp: Go from zero to hero in Python

Python interpreter

To run Python programs, you will need the Python interpreter and possibly a graphical editor.
A Python interpreter executes Python code (sometimes called programs).

run python

A program can be one or more Python files. Code files can include other files or modules. To run a program, you need to specify a parameter when executing Python.

Run Python code

You can download the Python interpreter here: https://www.python.org/downloads/  Once you have a Python interpreter installed, continue with this tutorial.

To run the code, open a terminal and launch:

This site contains exercises for the Python programming language. If you are new to programming, start with running Python code.

Execute Python scripts

Type the text below into a text editor and save as hello.py. Python files usually have the .py extension.

You can use any text editor to create a Python program. I recommend using a text editor that supports syntax highlighting (text colouring) and line numbers.

To start the program, we have to open the command line and type:

Related course: Complete Python Bootcamp: Go from zero to hero in Python

What you see if successful

You should see a line of text showing “hello world”.
python windows

Study drill

Try the exercises below:

After completing these continue with the next exercise.

Variables

Python supports different types of variables (datatypes) such as whole numbers, floating point numbers and text.  

You do not need to specify the datatype of a variable, you can simply assign any value to a variable. Type the program below and start it.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Datatypes and use

The example below shows you several variables. These can be assigned as you wish. Once defined you can print them or use arithmetics.

You should see several lines containing numbers and text:

python on dos

Study drill

Try the exercises below

After completing these continue with the next exercise.

Answers

Strings

Any time you want to use text in Python, you are using strings. Python understands you want to use a string if you use the double-quotes symbol.

Once a string is created, you can simply print the string variable directly. You can access characters using block quotes.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

String indexing

It creates string variable x. You can show it on the screen using the print() function.

Individual characters can be accessed using blockquotes, counting starts from zero. By using a colon you can create a substring.

Try the program below:

You should see this output:

python strings

Study drill

Try the exercises below

Answers

Random numbers

The module named random can be used to generate random numbers in Python. To use a module you need to type import module. This loads all of the functions inside the module.

Keep in mind that random numbers with the random module are pseudo-random numbers. For most programs this is fine.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Random module

To create random numbers with Python code you can use the random module. To use it, simply type:

This module has several functions, the most important one is just named random().
Type the program shown below and run it:

Study drill

Try the exercises below

After completing these continue with the next exercise.

Answers

Exercise 3: Keyboard input

In Python and many other programming languages you can get user input. Do not worry, you do not need to write a keyboard driver.

The input() function will ask keyboard input from the user. If you are still using Python 2, you have the function raw_input().

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Example

The input function prompts text if a parameter is given. The functions reads input from the keyboard, converts it to a string and removes the newline (Enter).

Type and experiment with the script below (save as key.py)

Study drills

Try these exercises:

After completing these continue with the next exercise.

Exercise 4: If statements

A program sometimes may have to make choices.  These choices can execute different code depending on certain condition. In Python the if statement is used for conditional execution.

The if statement may be combined with certain operator such as equality (==), greater than (>=), smaller than (<=) and not equal (!=). Conditions may be combined using the keywords or and and.

Related: Complete Python Bootcamp: Go from zero to hero in Python

Example

In the example below we show the use of if-statements.
Copy the program below and run it.

Visual example of if statement:
if statement

Study drills

Do these exercises:

Once completed continue with the next exercise.

Answers

Exercise 5: For Loops

Programs sometimes need to repeat actions. To repeat actions we can use a for loop.
A for loop is written inside the code. A for loop can have 1 or more instructions.

for loop

These instructions (loop) is repeated until a condition is met.

Related: Complete Python Bootcamp: Go from zero to hero in Python

Exercise

In the exercise below we will repeat actions on every item of a list. The first loop will repeat the print functionfor every item of the list. The second loop will do a calculation on every element of the list num and print the result.

Type the code below and run the program.

Study drill

Try the exercise below:

1. Make a program that lists the countries in the set

Answers

Exercise 6: While loop

A while loop repeats code until the condition is met. Unlike for loops, the number of iterations in it may be unknown.  A while loop always consists of a condition and a block of code.

A while loop ends if and only if the condition is true, in contrast to a for loop that always has a finite countable number of steps.

while loop

 
Related course: Complete Python Bootcamp: Go from zero to hero in Python

Definition

The while loop below defines the condition (x < 10) and repeats the instructions until that condition is true. Type this code:

Executes the code below until the condition x < 10 is met.   Unlike a for loop, the iterator i is increased in the loop.

Study drill

Try the exercise below:

1. Make a program that lists the countries in the set below using a while loop.

Exercise 7: Functions

To group sets of code you can use functions. Functions are small parts of repeatable code.
A function accepts parameters.

Without functions we only have a long list of instructions. Functions can help you organize code. Functions can also be reused, often they are included in modules.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Parameters

In the example below we have parameter x and y. Type this program and save it as summation.py

In this example we have two functions: f(x,y) and print(). The function f(x,y) passed its output to the print function using the return keyword.

function example

Output can also be stored into a variable:

Study drill

Try the exercises below

After completing these continue with the next exercise.

Answers