In this lesson, we will provide an introduction to installing and working with Python, with a focus on the following key points:
- Installation and setup of the language on your machine.
- An overview of some basic features of Python programming with code examples.
Python is one of the most popular programming languages today. It is easy to use and understand, with simple enough syntax that even a complete beginner can write a working Python program with a bit of help. It is free, open-source, and has a large community and ecosystem with extensive support on various platforms. Python has also been used extensively for advanced computer science applications such as data science, artificial intelligence, and machine learning.
In this tutorial, we will look at some of the most useful fundamental features in Python.
Whether you're a beginner programmer or an expert looking to try out a new programming language, this tutorial will cover all the basics you need for starting with Python.
Installing Python
To use the language on your machine, you need to install Python. It supports installation on common operating systems such as Windows, Mac OS X, Linux, as well as on several other operating systems.
To get Python on your machine, you can download the Python setup files from the official website here. The download is free, and you can choose the setup files according to your operating system.
Let's test your knowledge. Is this statement true or false?
Python is a paid programming language that is only limited to corporations.
Press true if you believe the statement is correct, or false otherwise.
Working in Python
Python programming is easy to understand. The syntax is simple, and the programs appear like they are written in the English language itself.
Before moving forward, let's review some basic programming syntax in this language.
Let's test your knowledge. Click the correct answer from the options.
Which of the following is NOT a data type?
Click the option that best answers the question.
- int
- string
- float
- number
Great! Now that we have reviewed some basics, let's talk about some useful aspects of the programming language.
Creating Functions
You may already be aware of built-in
functions. Built-in functions are specified by the programming language directly via reserved keywords
-- terms taken by the language itself. Such functions are available for the user to freely use in their programs. An example of one such very common built-in function in Python is print()
.
But what if you wanted to create a custom function to perform a task of your choice? You can create a user-defined function in that case, and Python fully supports this.
Let's now see how user-defined functions in Python work. A function definition starts with a def
keyword, followed by the function name, parenthesis, and a colon. You can also provide function parameters within the parenthesis. However, they are entirely optional, and functions exist without parameters as well.
To use this custom function in a program, it needs to be called, or invoked, within your code. This is shown in the code block below, where the function is now utilized. As output, "Hello World!" is printed on the console.
Functions help modularize
code, as the program gets divided into different blocks (or "subroutines") which can be reused. Suppose you have a program that performs file writing when triggered by a certain condition. Instead of writing the same lines of code repeatedly, a function can be made which performs this specific task. This function is then called whenever this task is required.
xxxxxxxxxx
function myFunction() {
console.log("Hello World!");
}
myFunction();
Let's test your knowledge. Is this statement true or false?
Is the following function definition valid?
1function func() {
2 console.log("This is my first function!");
3}
Press true if you believe the statement is correct, or false otherwise.
Lists
So far, you've seen variables
in Python. The problem with variables is that they can only store one value of a single data type.
Lists
solve this problem for us. They can store a collection of data-- that is, multiple values of the same or different data types in a single variable.
Lists in Python are defined with square brackets. Let's look at some examples of lists.
What if we want to access any of its elements? This is done using indexing
. In indexing, each value in a list is given a number (starting from 0
, not 1
!). To access a certain element, this number is specified inside the square bracket with the list name. For example, "w"
from the list below can be accessed using lst[5]
.

Lists are very powerful, and many useful operations are defined with them. These list methods
make it easier to manipulate and perform operations on lists. Some of these common methods are in the table below.
Function | Usage |
---|---|
len() | Get the length of list |
append() | Add more elements to an existing list |
count() | Gives the number of times a specified element appears in a list |
pop() | Remove the element at last index in the list |
sort() | Sort all the elements in the list according to specified order |
copy() | Copy all elements from one list to another, and returns the copy |
xxxxxxxxxx
// Array with items of single type
let fruits = ["orange", "apple", "banana", "strawberry"];
// Array with items of multiple types
let lst = ["a", 1, "c", ["x", "y"], 5, 7, [1,2]];
Dictionaries
A collection of data can also be stored in the form of dictionaries
in Python. Unlike lists, dictionaries store data values in what's known as key: value
pairs. These key: value pairs are also called items
. This means that in a dictionary, values are assigned to keys
, and the values of these items can be obtained using key names.
In Python, dictionaries are declared using curly braces {}
, separated by a comma. A key
is specified within the curly braces, which is followed by a colon and its corresponding value
.
Let's look at an example dictionary.

But in what cases can dictionaries be helpful?
Suppose you want to store distances of all cities from a chosen starting point. In such a case, it would be helpful if you could store distance values for each city from the starting city. Using a list would be a tedious task as you would have difficulty representing which city has what distance. Dictionaries make it easier to represent such problems. In this example, using a dictionary, cities can be represented as keys and their distances as values.
xxxxxxxxxx
let distances = {"A": 421, "B": 234, "C": 127, "D": 184};
Like lists, dictionaries also have a set of methods that make it easier for them to be able to be manipulated in a program.
Methods | Usage |
---|---|
keys | Get all the keys from dictionary |
values() | Get all values from dictionary |
items() | Get all key value pairs from dictionary |
get() | Gets the value of specified key |
Working with Files
Analyzing or working with data from a local file in a program is called file handling
. Suppose you have student academic records in a file and want to calculate percentages of each student. Calculating it one by one in a file is a tiring task. This work would be easier if the file could be loaded to a program, and you could directly apply the operation on all the data in the file.
To work with files in Python, you need to open the file (within the program), perform an operation on it (read or write), and then close the file. Simple, right? Let's see how it looks in code.
xxxxxxxxxx
var fs = require('fs');
fs.readFile('filename.txt', 'utf8', function(err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
Python provides an in-built function open()
which opens the file inside the program. You need to specify the name of the file which needs to be opened in a string within the parenthesis. By default, the file opens in reading
mode. You can specify different modes according to how you want to use the file in your program.
If you want to write to a file, you'd do something like this.
xxxxxxxxxx
var fs = require('fs');
fs.writeFile("filename.txt", "Writing in file", function(err) {
if (err) {
return console.log(err);
}
});
However, this would overwrite the file if there was any previous information in it. To avoid that, we can append
to a file using a different mode like this.
xxxxxxxxxx
const fs = require('fs');
fs.appendFile('filename.txt', 'Appending to existing file', function (err) {
if (err) throw err;
});
Important! Make sure you specify the file path correctly while opening files! And that the files are in the proper directory!
Let's test your knowledge regarding the different Python features discussed in this lesson!
Build your intuition. Click the correct answer from the options.
What is the correct way to check if a value 42
exists in the dictionary below?
1var employees = {"a": 10, "b": 53, "c": 42, "d": 88};
Click the option that best answers the question.
- 42 in employees
- 42 in employees.keys()
- 42 in employees.values()
- 42 in employees.items()
Try this exercise. Is this statement true or false?
Is the given function correct, and is this a valid method to read first n
characters of a file?
1const fs = require('fs');
2
3fs.readFile('filename.txt', (err, data) => {
4 if (err) throw err;
5 console.log(data.toString('utf8').substring(0,10));
6});
Press true if you believe the statement is correct, or false otherwise.
One Pager Cheat Sheet
- This tutorial provides an introduction to
installing
andworking
with Python, with a focus on the key points of installation and setup, and an overview of some of its basic features. - To download and install Python
free of charge
on youroperating system
, you can visit the official website here. - Python is a
free and open-source
programming language that is accessible and popular for a variety of purposes. Python
programming is easy to understand due to its simple syntax resembling the English language.- Number objects of the
int
,float
,complex
, andbool
data types can all be considerednumbers
in Python. - By
creating functions
and reusing them, we canmodularize
our code to make program execution more efficient. - The definition is invalid because it does not include the
keyword def
, the function name within parenthesis with the parameters, a colon and an indented code blockfor the function body
. - Lists in Python are powerful data structures, allowing us to store multiple values of different data types in a single variable and perform operations on them using
indexing
andlist methods
. - Dictionaries in Python are an efficient way to store
key: value
pairs of data, particularly for representing data wherekeys
need to be associated with particularvalues
. - Dictionaries have methods, such as
keys()
,values()
,items()
, andget()
, to help them be manipulated in a program. - By opening, reading and closing a file in Python, you can easily manipulate data from the file within your program.
- The
open()
function in Python can be used to open a file, with the name specified in astring
within the parenthesis, in eitherreading
orwriting
mode. - We can
append
to a file to avoid overwriting any previous information. - Be sure to
specify
the file path correctly while opening files and ensure they are in the correct directory. - The
in operator
and thevalues()
method can be used to check if the value42
exists in theemployees
dictionary. - Yes, the given
open()
andread(n)
function is correct and should successfully read the firstn
characters of the filefilename.txt
.