Getting Started with Python: Variables and Data Types

Ibrahim Olawale
DSN AI+ FUTA
Published in
6 min readApr 10, 2020

--

Source

A variable is a container used to store and hold data. It can be seen as a Python object used in storing and holding data for re-usability later on during the program. These data can come in different types which will be discussed in subsequent sections below.

Variable assignment is the process of assigning a value or data to our variable. In other words, mapping/matching our variable name to a value in memory of our interpreter. After variable assignment, the value of our variable is stored in memory of our interpreter and can now be assessed by calling it with the variable name during our program. Just like in every programming language, there are rules to perform certain tasks, this is no different with variable declaration in Python.

Rules to Variable Declaration in Python

Unlike some other programming language, you do not need to explicitly declare a Python variable by setting its data type or using a keyword. A variable is declared in Python by just assigning the value to the variable name using an equal to sign (=). The variable name is always at the left hand side while the value to be stored in the variable is at the right hand side. Here are the rules:

  1. A variable must start with a letter or the underscore character e.g. “variable = value”, “_variable = value” Note: variables in Python are case sensitive. “variable” is not the same as “Variable”

2. A variable name cannot start with a number e.g. “123 = value”, “1variable = value” these are wrong examples of variable declaration

3. A variable can only contain alphanumeric characters and underscores and not any other special characters e.g. “new_1variable = value”

4. Keywords can’t be overwritten. Keywords are python in-built objects that have been created already for certain use e.g. def, while, for, return, etc.

Different type of values can be assigned to a variable. These values refer to the data we are trying to hold or “contain” in our variable. There are also many data types but limited to the scope of this article, we will be discussing the basics which are the integers, floats, Boolean values, strings etc.

Another thing to note is variable reassignment. They are called variables due to the fact that their values can change as we specify during the course of programming.

Example 1

A variable with value of 50 can be reassigned to a new value say 100

Variable reassignment

Data Types

Let’s talk about data types, in python we have integers, float, boolean, strings, lists, tuples, dictionaries etc. Data types are the different kind of data that our variables can hold/store. These data types have their different characteristics which make them unique and useful for different purposes. We have integers which are basic whole numbers 0, 1, 2, 3… Then we also have floats which are numbers with decimal points e.g. 1.0, 3.52, 1076.462 etc. Different arithmetic operations can be performed on these data types which will also be covered in subsequent classes. Another important data type in Python is string which have a lot of operations that can be performed on it, some examples will be shown as we proceed.

Strings

A string is an ordered sequence of characters. Two key words here, ordered and characters. Ordered means that we will be able to use indexing and slicing to grab elements from the string. Python strings are created by passing the characters/values into inverted commas (single quote or double quotes).

Example 2

Some of the operations we can perform on strings include string indexing and slicing.

Indexing

Since strings are ordered sequences of characters, it means we can select single character (indexing) or grab sub-sections of the string (slicing). Indexing is a way of grabbing the numeric position a character occurs in a string. Python indexing starts with a zero (0).

string: hello

index: h=0, e=1, l=2, l=3, o=4

Square brackets [ ] can also be used to grab characters from a string using the character index number

Example 3

Indexing of a string

Python also support reverse indexing. Reverse indexing refers to indexing using negative numbers as index, usually starting from the last index as -1 to the first one as zero e.g.

‘hello’

o = -1, l = -2, l = -3, e = -4, h = 0

String Slicing

Slicing is used to grab entire subsections of a string with slice notation. Slicing is done using square brackets too. This is the normal notation:

[start:stop:step]

Note:

1. The starting index directly corresponds to where your slice will start

2. The stop index corresponds to where you slice up to. The value in the stop index is not included. To grab the last character, no index number should be allocated as the stop, the interpreter automatically does this. Generally, [Start:Stop-1:Step] in terms of values.

3. The step size is how many characters you skip as you go grab the next one.

Example 4

Some string slicing examples

Basic String Methods

There are some other basic operations that can be performed on strings. These come as built-in Python methods. Methods are actions you can call off an object usually in the form .methodname( ) notice the closed parenthesis at the end. Strings have a lot of them, many methods which you can check with the Tab functionality in Jupyter notebook. Let’s go over some of the more useful string functions. e.g. The upper( ) method which is used to capitalize every character in the string.

The lower( ) method does a similar thing by converting every character in the string to lowercase

The split( ) method which is used to split words/single words in a string according to the character passed into the parenthesis, this method automatically converts the string into a list with each character as a value. The default character is space (‘ ’)

Some basic string methods and operations

The link below contains the official documentations of all python string methods

Booleans

Booleans are data types that indicate a logical state of True or False. Python also has a placeholder object called None. Let’s explore what these look like. We will work with them a lot more once we begin to learn about control flow with Python, but until then, let’s just get to understand what they look like. When you are on your Jupyter Notebook or Python editor, you will notice the syntax highlighting when using these keywords.

Example 5

Evaluating boolean statements

We can also use None as a placeholder for an object that we don’t want to assign yet

>>> c = None

>>> type(c)

NoneType

Thanks for reading 😃

--

--