A Beginners tutorial to Python: Part 1

Peter Allen
12 min readJan 7, 2023

--

A Beginners tutorial to Python

Welcome to Python! This tutorial will introduce you to the basics of Python and help you get started with writing your own code.

What is Python?

Python is a high-level, interpreted programming language. It was first released in 1991 and has become one of the most popular programming languages in the world. Python is known for its simplicity, readability, and flexibility, which make it a great language for beginners.

  1. Setting up Python
  2. Python basics
  • Variables
  • Data types
  • Operators
  • Conditional statements
  • Loops
  • Functions
  • Modules

Setting up Python

To start using Python, you’ll need to have it installed on your computer. If you don’t already have Python installed, you can download it from the official Python website: https://www.python.org/

Once you have Python installed, you can start using it in a few different ways:

Using the Python interpreter

The most basic way to use Python is by using the Python interpreter. The interpreter is a program that runs in the command line and allows you to type in and execute Python code on the fly.

To start the Python interpreter, open a terminal window (on Mac or Linux) or a command prompt (on Windows) and type in python. This will launch the Python interpreter and you'll see a prompt where you can enter your code as follows:

$ python
Python 3.9.0 (default, Dec 22 2021, 16:22:07)
[GCC 9.3.1 20201123] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

To exit the Python interpreter, you can use the exit() function or press CTRL + D.

Creating Python scripts

Another way to use Python is by creating Python scripts. A script is a text file that contains a series of Python instructions that are executed from top to bottom when the script is run.

To create a Python script, create a new file in your favorite text editor (such as Sublime Text, Atom, or Notepad++) and save it with a .py extension. For example, you might save your script as hello.py.

To run a Python script, open a terminal or command prompt, navigate to the directory where the script is located, and type python script.py, where script.py is the name of your script.

Python basics

Now that you know how to use Python, let's take a look at some of the basic elements of the language.

Variables

A variable is a name that refers to a value. In Python, you can create a variable by assigning a value to it using the = operator. For example:

>>> x = 5
>>> y = "hello"
>>> z = [1, 2, 3]

In the above example, x is a variable that refers to the integer value 5, y is a variable that refers to the string value "hello", and z is a variable that refers to a list of integers.

Data types

In Python, there are several built-in data types that you can use to store different kinds of values. Some of the most common data types are:

  • Integers: These are whole numbers, such as 1, 2, and 3.
  • Floats: These are numbers with decimal points, such as 3.14 and 2.718
  • Strings: These are sequences of characters, such as "hello" and "world". Strings can be enclosed in single or double quotes.
  • Lists: These are ordered collections of values that can be of any data type. Lists are defined using square brackets and can contain multiple values, separated by commas. For example: [1, 2, 3] is a list of integers and ["apple", "banana", "cherry"] is a list of strings.
  • Dictionaries: These are unordered collections of key-value pairs. Dictionaries are defined using curly braces and consist of keys and values separated by a colon. For example: {"name": "John", "age": 30} is a dictionary with two key-value pairs.

Operators

Python has several built-in operators that you can use to perform operations on values. Some of the most common operators are:

  • Arithmetic operators: These include + (addition), - (subtraction), * (multiplication), and / (division).
  • Assignment operators: These include = (assignment), += (increment by), and -= (decrement by).
  • Comparison operators: These include == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

Conditional statements

Conditional statements allow you to execute different pieces of code based on whether a certain condition is true or false. In Python, the most common conditional statement is the if statement.

Here is an example of an if statement in Python:

>>> x = 5
>>> y = 10
>>> if x < y:
... print("x is less than y")
...
x is less than y

In the example above, the if statement checks to see if x is less than y. If it is, the code block that follows the if statement is executed. If it is not, the code block is skipped.

You can also use the else statement to specify a code block to be executed if the condition in the if statement is not true:

>>> x = 5
>>> y = 10
>>> if x < y:
... print("x is less than y")
... else:
... print("x is not less than y")
...
x is less than y

And you can use the elif statement to specify additional conditions to check:

>>> x = 5
>>> y = 10
>>> if x < y:
... print("x is less than y")
... elif x > y:
... print("x is greater than y")
... else:
... print("x is equal to y")
...
x is less than y

Loops

Loops allow you to execute a block of code multiple times. In Python, there are two types of loops: for loops and while loops.

for loops

for loops are used to iterate over a sequence of values, such as a list or a string. Here is an example of a for loop in Python:

>>> for i in range(5):
... print(i)
...
0
1
2
3
4

In the example above, the for loop iterates over the values in the range function (which generates a sequence of numbers from 0 to 4). The loop variable i takes on the value of each element in the sequence, and the code block inside the loop is executed for each iteration.

while loops

while loops are used to execute a block of code repeatedly as long as a certain condition is true. Here is an example of a while loop in Python:

>>> i = 0
>>> while i < 5:
... print(i)
... i += 1
...
0
1
2
3
4

In the example above, the while loop continues to execute as long as i is less than 5. The loop variable i is incremented by 1 at the end of each iteration using the += operator.

Functions

Functions are blocks of reusable code that can be called from anywhere in your program. In Python, you can define a function using the def keyword.

Here is an example of a simple function in Python that takes a single argument and returns a value:

>>> def add(x):
... return x + 1
...
>>> add(5)
6
>>> add(10)
11

In the example above, the add function takes a single argument x and returns the value of x plus 1. The function is called twice with different arguments and the returned values are printed to the console.

You can also define functions with default arguments, which have a predetermined value if no argument is passed to the function:

>>> def add(x, y=1):
... return x + y
...
>>> add(5)
6
>>> add(5, 3)
8

In the example above, the add function has two arguments: x and y. y has a default value of 1, so if it is not provided as an argument when the function is called, it will default to 1.

Modules

Modules are Python files that contain a collection of related functions and variables. You can import a module into your program using the import keyword.

Here is an example of how to import and use a module in Python:

# Import the math module
>>> import math
# Use the sqrt function from the math module
>>> math.sqrt(4)
2.0
# Use the pi constant from the math module
>>> math.pi
3.141592653589793

In the example above, the math module is imported and the sqrt function and the pi constant are used from the module.

That’s it for this Python tutorial for beginners! I hope you now have a good understanding of the basics of Python and are ready to start writing your own code.

Welcome to Python! This tutorial will introduce you to the basics of Python and help you get started with writing your own code.

What is Python?

Python is a high-level, interpreted programming language. It was first released in 1991 and has become one of the most popular programming languages in the world. Python is known for its simplicity, readability, and flexibility, which make it a great language for beginners.

  1. Setting up Python
  2. Python basics
  • Variables
  1. Variables
  • Data types
  • Operators
  • Conditional statements
  • Loops
  • Functions
  • Modules

Setting up Python

To start using Python, you’ll need to have it installed on your computer. If you don’t already have Python installed, you can download it from the official Python website: https://www.python.org/

Once you have Python installed, you can start using it in a few different ways:

Using the Python interpreter

The most basic way to use Python is by using the Python interpreter. The interpreter is a program that runs in the command line and allows you to type in and execute Python code on the fly.

To start the Python interpreter, open a terminal window (on Mac or Linux) or a command prompt (on Windows) and type in python. This will launch the Python interpreter and you'll see a prompt where you can enter your code as follows:

$ python
Python 3.9.0 (default, Dec 22 2021, 16:22:07)
[GCC 9.3.1 20201123] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

To exit the Python interpreter, you can use the exit() function or press CTRL + D.

Creating Python scripts

Another way to use Python is by creating Python scripts. A script is a text file that contains a series of Python instructions that are executed from top to bottom when the script is run.

To create a Python script, create a new file in your favorite text editor (such as Sublime Text, Atom, or Notepad++) and save it with a .py extension. For example, you might save your script as hello.py.

To run a Python script, open a terminal or command prompt, navigate to the directory where the script is located, and type python script.py, where script.py is the name of your script.

Python basics

Now that you know how to use Python, let's take a look at some of the basic elements of the language.

Variables

A variable is a name that refers to a value. In Python, you can create a variable by assigning a value to it using the = operator. For example:

>>> x = 5
>>> y = "hello"
>>> z = [1, 2, 3]

In the above example, x is a variable that refers to the integer value 5, y is a variable that refers to the string value "hello", and z is a variable that refers to a list of integers.

Data types

In Python, there are several built-in data types that you can use to store different kinds of values. Some of the most common data types are:

  • Integers: These are whole numbers, such as 1, 2, and 3.
  • Floats: These are numbers with decimal points, such as 3.14 and 2.718
  • Strings: These are sequences of characters, such as "hello" and "world". Strings can be enclosed in single or double quotes.
  • Lists: These are ordered collections of values that can be of any data type. Lists are defined using square brackets and can contain multiple values, separated by commas. For example: [1, 2, 3] is a list of integers and ["apple", "banana", "cherry"] is a list of strings.
  • Dictionaries: These are unordered collections of key-value pairs. Dictionaries are defined using curly braces and consist of keys and values separated by a colon. For example: {"name": "John", "age": 30} is a dictionary with two key-value pairs.

Operators

Python has several built-in operators that you can use to perform operations on values. Some of the most common operators are:

  • Arithmetic operators: These include + (addition), - (subtraction), * (multiplication), and / (division).
  • Assignment operators: These include = (assignment), += (increment by), and -= (decrement by).
  • Comparison operators: These include == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

Conditional statements

Conditional statements allow you to execute different pieces of code based on whether a certain condition is true or false. In Python, the most common conditional statement is the if statement.

Here is an example of an if statement in Python:

>>> x = 5
>>> y = 10
>>> if x < y:
... print("x is less than y")
...
x is less than y

In the example above, the if statement checks to see if x is less than y. If it is, the code block that follows the if statement is executed. If it is not, the code block is skipped.

You can also use the else statement to specify a code block to be executed if the condition in the if statement is not true:

>>> x = 5
>>> y = 10
>>> if x < y:
... print("x is less than y")
... else:
... print("x is not less than y")
...
x is less than y

And you can use the elif statement to specify additional conditions to check:

>>> x = 5
>>> y = 10
>>> if x < y:
... print("x is less than y")
... elif x > y:
... print("x is greater than y")
... else:
... print("x is equal to y")
...
x is less than y

Loops

Loops allow you to execute a block of code multiple times. In Python, there are two types of loops: for loops and while loops.

for loops

for loops are used to iterate over a sequence of values, such as a list or a string. Here is an example of a for loop in Python:

>>> for i in range(5):
... print(i)
...
0
1
2
3
4

In the example above, the for loop iterates over the values in the range function (which generates a sequence of numbers from 0 to 4). The loop variable i takes on the value of each element in the sequence, and the code block inside the loop is executed for each iteration.

while loops

while loops are used to execute a block of code repeatedly as long as a certain condition is true. Here is an example of a while loop in Python:

>>> i = 0
>>> while i < 5:
... print(i)
... i += 1
...
0
1
2
3
4

In the example above, the while loop continues to execute as long as i is less than 5. The loop variable i is incremented by 1 at the end of each iteration using the += operator.

Functions

Functions are blocks of reusable code that can be called from anywhere in your program. In Python, you can define a function using the def keyword.

Here is an example of a simple function in Python that takes a single argument and returns a value:

>>> def add(x):
... return x + 1
...
>>> add(5)
6
>>> add(10)
11

In the example above, the add function takes a single argument x and returns the value of x plus 1. The function is called twice with different arguments and the returned values are printed to the console.

You can also define functions with default arguments, which have a predetermined value if no argument is passed to the function:

>>> def add(x, y=1):
... return x + y
...
>>> add(5)
6
>>> add(5, 3)
8

In the example above, the add function has two arguments: x and y. y has a default value of 1, so if it is not provided as an argument when the function is called, it will default to 1.

Modules

Modules are Python files that contain a collection of related functions and variables. You can import a module into your program using the import keyword.

Here is an example of how to import and use a module in Python:

# Import the math module
>>> import math
# Use the sqrt function from the math module
>>> math.sqrt(4)
2.0
# Use the pi constant from the math module
>>> math.pi
3.141592653589793

In the example above, the math module is imported and the sqrt function and the pi constant are used from the module.

That’s it for this Python tutorial for beginners! I hope you now have a good understanding of the basics of Python and are ready to start writing your own code.

--

--

Peter Allen

Data Analyst in Melbourne Australia. Ex-mechanical engineer who transitioned across due to the love of all things data. Beekeeper. DIY. Tinkerer.