Data types and operators in Python

Welcome back to the Python basics tutorial! Today you're gonna learn about more data types like strings and how to manipulate them.

Data types

There are some inbuilt "data types" in Python, which you can use to store data. One example is a string. (Recall what we learned in the previous tutorial.) Let's say you wanted to store a number like 42 in a variable. You could do it like:

num = '42'

but, this would create problems most of the time, you'll see why later in the chapter. So, instead, you can create another type of data, an integer. You create integer like this:

num = 42

Here, you can see that we did not use quotes, as that would make it a string. just a simple number. You can store negative values too:

num = -42

Now, if you wanted decimals as well? Easy, just assign it to a variable just like an integer:

num = 42.31

But, remember, these are known as "floating-point numbers" or "floats" not integers.

There's also a type called a boolean. A boolean has two possible values: True and False:

i_have_apples = True
i_have_oranges = False
i_have_a_life = False

So, the basic data types are: String, Integer, Float and Boolean. Of course, there are more, which we will see later.

To convert a type to another, use str() to convert to String, int() to convert to integer and float() to convert to float. For example:

>>> a = 4
>>> b = float(a)
>>> print(b)
4.0
>>> print(int(b))
4
>>> print(str(a))
'4'

This is necessary because '4'+'4' wil return 44 because it's adding them as strings, not integers. But 4 + 4 returns 8 as expected. You can find the type with type() like:

>>> print(type('Hello'))
<class 'str'>
>>> print(type(1))
<class 'int'>
>>> print(type(3.0))
<class 'float'>

There's also another type called None, which denotes the absence of a value.

Operators

You're probably wondering by now, what's the point of all this number storing and stuff if you can't do basic stuff like addition and subtraction?

Well, you can.

a = 42
b = 21
c = a + b
print(c)

This code assigns 42 to a and 21 to b . Then it adds a and b and assigns the result to c and prints c. Same for subtraction, division and multiplication. You just need to use the appropriate operator (sign). The 'equals to' sign = you use for assigning variables is also an operator.

+ for addition

- for subtraction

* for multiplication

/ for division

% for modulus

a = 21
b = 12
added = a + b
subtracted = a - b
multiplied = a * b
divided = a / b

You can also use = with the operators.

a = 12
a += 12

So far, we've been creating new variables. We can do a calculation and save the result to the same variable too. So, the code above translates to:

a = 12
a = a + 12

But, we use += to make it shorter and simpler. Same for -= , *=, and /= .

These are called assignment operators.

For exponents, you can do:

2**3

to get the result of 2^3. Recall that we used the + operator with strings as well. to multiply a string: 'Hello World' * 3 which would return "Hello WorldHello WorldHello World". - and / do not work though.

You can not add a string and an integer or float directly. You need to convert it to a string on an integer respectively. You can add, subtract, multiply and divide floats and integers though.

> , < , >=, <= , !=and ==are comparison operators.

> means "Greater Than" < means "Smaller Than" >= means "Greater than or equal to" <= means "Smaller than or equal to" != means "Not equal to" == means "Equal to"

When you use then to compare something, they return a boolean value.

print(2 > 1)

will print True and:

print(2 > 4)

will print False and of course:

>>> 2 == 2
True
>>> 4 == 2
False

Of course, you can save it in a variable too.

is_bigger = 3 > 5
print(is_bigger)

These are the basic data types and operators in Python. Just like before, there are more data types and operators you can work with, but that's going to be kept for later.

Homework

Assign an integer to a variable called x, multiply x with 21 and divide by 13, and save the result to x again.

Last updated