Python Tutorial 2: Data Types – Integers, Strings, Floats, Boolean

To start off our tutorial series, we will first learn about basic data types in Python.

Strings – Commonly used to represent text, but numbers can be strings as well.

Integers – If you remembered learning E Maths in secondary school, integers are just whole numbers.

Floats – Other real numbers which have demical places are floats. Do note that 3.0 is considered a float, while 3 is an integer.

Assigning strings to variables

In Python, assigning means storing or setting a value to a variable. For example, when I say “assign ‘apple’ to x”, I would store the string ‘apple’ to x, and x will return the string ‘apple’. In IDLE, you have to type print(x) to show an output in the complier.

Whenever you assign a string to a variable, do remember to include double apostrophes around your text!

In [2]:
x = "hello world!"
'hello world!'

Assigning integers/floats to variables

Similarly, we can assign integers and floats to variables in Python. To assign an integer or a float, we do not include double apostrophes. This differentiates it from assigning strings.

In [1]:
# assigning an integer
a = 1
In [2]:
# assigning a string
b = "1"
In [3]:
# assigning a float
c = 1.0

Checking the type of a variable

To check the type of a variable, we will use the function type(). You will learn more about functions in Chapter 6.

In [17]:
# Using type() function
class 'int'
class 'str'
	class 'float'


Booleans are used to represent truth values in Python. They are either be True or False. You can assign a Boolean to a variable, but they are more commonly used as the output of logical (boolean) operations. Booleans are important as they can be used to perform filtering operations on data structures.

Comparison Operations

Comparison operations are a type of logical operations. The most commonly used ones are >, <=, =<, < and ==. In Python, the operator ‘==’ means equal to in Mathematics, as ‘=’ is reserved for assigning variables.

In [8]:
a = 10
b = 15
print(a > b)
In [9]:
c = 20
d = 20
print(c == d)

Logical Operations

We can also check multiple comparison statements using ‘and’ and ‘or’.

In [11]:
print(a > b and c == d)
In [12]:
print(a > b or c == d)

The Boolean True has a value of 1 while False has a value of 0. You can also compare between booleans.

In [13]:
x = True
y = False
In [14]:
# Checking the type of variables x and y
class 'bool'
class 'bool'
In [15]:
print(x == 1)
In [16]:
print(y == 0)
In [17]:

Note that when assigning booleans to variables, True or False need to be capitalised.

In [18]:
x = true
y = false
NameError: Traceback (most recent call last)
in ()----> 1 x = true
2 y = false
NameError: name 'true' is not defined

Next: Python Tutorial 3 Basic Operations

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s