Back to articles list Articles
11 minutes read

Getting Started with Python Part 1: Data Types

Want to learn Python but think you have to know something before you start? You don't! That's the whole point of learning, as you'll see.

So you want to learn Python, but something is stopping you. What is it? The usual misconception that stops people from doing something is that they should know everything, even before they start learning. This misconception makes you think everybody was born with their knowledge and that you're never going to reach their level.

Then there's the perception that something is so complicated you'll never be able to learn it. That's simply not true! The main point is to have fun and feel satisfaction from learning something that interests you. Compare yourself with yourself (not others!) and see the improvement you've made. Start with small steps and focus on what you've learned, not what you still have to learn.

You think you'll reach the point where you know everything and can stop learning? You'll always have something more to learn. As Voltaire famously said:

The more I read, the more I acquire, the more certain I am that I know nothing

So if you're still thinking whether you're able to learn Python, I'll try to help you stop thinking about learning and start learning.

From a Beginner to Beginners

Sometimes beginners can be scared off by an instructor's knowledge or inability to come down to a beginner's level. For a start, I don't want to be your instructor! Even if I wanted to, I couldn't! I'm a Python beginner myself. I've just recently started to learn – and from the very basics, just like you. The point of this article series is not for me to tutor you, but for us to learn together.

I've spent 13 years in the financial industry. That's only natural for someone who has a degree in accounting. However, I've never worked as an accountant. I started in auditing and every succeeding job moved me further away from economics and closer to IT and databases. As someone between both worlds, I've realized that my business knowledge can be very useful in the world of databases. And vice versa. After additional education in databases and programming, I've started freelancing as a database designer, among other things.

But as for Python, I didn't know a thing about it. When someone mentioned it, my first thought was Monty Python's Flying Circus and some of their brilliant jokes. As the creators of Python (the programming language) have said, it's not necessary to like Monty Python to program in Python, but it sure can help!

As I saw more and more people talking about Python and its use, I decided to learn it myself. I have no specific purpose in doing so, since I don't currently accept jobs that require Python knowledge. But learning new skills while increasing the variety of jobs I could apply for and having fun while doing it is a no-brainer for me.

Don't worry about your formal education, either! As Mark Twain said: "Don't let formal education get in the way of your learning." Let's see what we can learn!

Python Data Types

In Python as well as other programming languages, a data type is a classification that defines which kind of value can be assigned to a variable and which mathematical, logical, and relational operations can be performed with that variable.

What is a variable, you ask? Variables are named places within computer memory where you can store certain data. After the data is stored, you can retrieve it later using the variable name.

There is a wide range of data types supported in Python. For now, I will concentrate on the following two types:

  • Text
  • Numeric

Text data type

In Python, the text data type is called "string" and is abbreviated as str. Using a string data type means that everything is treated as text, even the numbers. Important: Mathematical operations cannot be performed on the string data type; you'll need a numeric data type for that.

Let's see how things work. If you want to print the sentence Python is so cool!, this is how you'd do it:

print ('Python is so cool!')

If you run this code, it will print exactly what you wanted!

If you want something to be treated as a string, i.e. text, put single quotation marks around it, 'like this'. You can also use double quotation marks, as shown below.

print ("Double quotation marks are not so cool!")

Double quotes work just the same, but it is an unwritten rule that single quotation marks should be used when defining a string.

Uh-oh, wait! What is that print thing that I just used without any warning? As you just saw when you ran the code, it is a function that prints out (or shows) anything that is put inside the brackets ( ). You just used a function, that scary thing, and didn't even realize it. Functions are nothing but blocks of organized and reusable code that are used to perform a certain action.

Remember when I told you that numbers can also be shown as a string? Let's see how. Running the following code ...

print ('3 ice creams are better than 2')

...will print the following sentence:

3 ice creams are better than 2

So now you probably realize how it works. You can write whatever you want between the quotation marks and it will be treated as a string (i.e. text) and printed on the screen.

What if you want to print the following sentence: I've just started with Python and it feels good! Let's do it the way we know. Run the following code:

print ('I've just started with Python and it feels good!')

Oh, no! A mistake! Remember what I said about single quotation marks? The first quote marks the start of the text and the second marks the end of the text. How many do we have? Three! That's why there's an error.

So does this mean you can't print such a sentence in Python? Of course you can! You can use something called the escape character, or backslash (\). It is put before special characters, such as apostrophes. Let's try that!

print ('I\'ve just started with Python and it feels good!')

Yes! It works!

Now let's try Python's numeric data types.

Numeric data types

The two main numeric types supported by Python are:

  • Integers
  • Floating-point numbers

Integers got their name from the Latin word for "whole", meaning integers are numbers that can be written without fractions (i.e. no decimal points allowed!). Integers can be positive, negative, or zero. Examples of integers are 0, 15, 100, -100, and -328. In Python, the integer data type is marked as int.

Floating-point numbers (or floats) are numbers that contain a decimal point. Some examples of floating-point numbers are 1.52, 8.5245825478, and -32.7.

Will Python print numbers the same way as text? Let's see! Run the following simple code:

print (12)

It works – it printed the number 12. You just printed an integer. Let's do the same for a float. Run the code:

print (3.14)

The principle is the same and it still works – it printed the number 3.14.

Mathematical operations

I've already mentioned that the main difference between text and numeric data types is that mathematical operations can be performed on the latter but not on the former. So let's see how to perform simple mathematical operations, such as:

  • Adding by using the symbol +
  • Subtracting by using the symbol -
  • Multiplying by using the symbol *
  • Dividing by using the symbol /

Adding

Let's see how it works on a simple example. Running the code below ...

print (13+28)

... will return 41. If you want to add two floating-point numbers, it works the same way. Run this code:

print (72.35847+28.135541254)

The result is 100.494011254. If you want to add integers and floats, you can combine them any way you want. For example, running the code ...

print (13+82.13+100+13+52.875954+12.11+10.0)

... will show the result 283.115954 which is, of course, not an integer.

Subtracting

Subtracting works exactly the same way as adding. Let's use the same examples as above, but let's replace + with -. If you run the following code ...

print (13-28)

.... you will get -15 as a result. Yes, Python works with negative numbers too! To subtract two floats, simply run the code ...

print (72.35847-28.135541254)

... and you will get 44.222928745999994 as a result. If you want to subtract several integers and floats, it's easy. For example, running the code ...

print (13-82.13-100-13-52.875954-12.11-10.0)

... will give you -257.115954.

Multiplying

The principle is the same as above, so let's practice a little bit. Run this code:

print (13*28)

You're multiplying two integers and getting 364. If you want to multiply two floats, there's no problem with that! Multiply them just as above. Try this:

print (72.35847*28.135541254)

And enjoy the beautiful number 2035.8447177613214 that it returns. Do you want to multiply a combination of integers and floats? No problem! Run this code:

print (13*82.13*100*13*52.875954*12.11*10.0)

Python quickly calculates the correct result: 8887730694.973112.

Dividing

I think you've already understood how it goes! You simply write any number you want and divide it by any number you want just by using /. Let's go!

Run this code:

print (13/28)

You will get 0.4642857142857143. But wait! Even though you divided two integers, the result is a float. Python will automatically recognize that and return the result formatted as a float.

You can divide floats using exactly the same process. For instance, try this:

print (72.35847/28.135541254)

You'll get 2.571781695854629 as a result. Dividing multiple integers and floats also works:

print (80.07/13/2.2/2)

The result is 1.3998251748251747.

Combining mathematical operations

What if you want to perform several basic mathematical operations in one step? Is this possible? Not only is it possible, it's also very easy. You can combine every mathematical thing you've learned so far. Let's play around a little bit and combine adding and subtracting.

Running this code ...

print (18+32-14.7)

... will give you 35.3 as a result. You can combine integers and floats any way you like. You can combine multiplying and dividing in the same simple way as above. Let's run the code and see what you'll get:

print (20*18*7/3/2.7)

The result is 311.1111111111111.

We've combined adding with subtracting and multiplying with dividing. But can we use all four operations in a single step? Surely, if you run code like this ...

print (20+15-5*10/2)

... the result will be 150, right? But running the code gives you 10.0! So Python can't combine all those mathematical operations after all! Or maybe it can but it returns the wrong result? Surely the result should be 150 because 20+15-5 is 30 multiplied by 10 is 300 and divided by 2 is 150. Right?

Wrong! When combining mathematical operations, you have to be careful: Python will perform them according to the mathematical order – not according to the order you wrote them! So in the above case, first it will multiply and then divide, which will give you 25 as a result. Add 20 and 15 then subtract 25 and yes, the result is 10.0. Seems Python didn't make a mistake.

If you really want to change the order of mathematical operations, you should use brackets (). If you want the above code to return 150, it should be written like this:

print ((20+15-5)*10/2)

If you don't trust me, run the code and see for yourself!

Let's practice Python math a little more. Here's a short exercise:

You're selling tickets for the AC/DC concert and you have 720 tickets for 50 dollars and 180 tickets for 100 dollars. After the concert, you have only 12 tickets left in the first category and 27 in the second category. How much did you sell?

How would you write that as Python code? Try it yourself and see if you get 50,700 dollars as a result. The code should look like this:

print ((720-12)*50+(180-27)*100)

Selling more than 50,000 dollars brings you to the end of the first part of the journey. But you've actually only started, as I bet you'll continue to learn more about Python!

You Can Learn Python!

In this part, I've tried to introduce myself. The purpose of this was not to scare you away with my vast Python knowledge. Au contraire, my friends! I was trying to show you that anybody, no matter their education and previous knowledge, can start learning Python very easily. If I can do it, anybody can!

Having a good quality Python course is also important in keeping people interested in learning. And I think LearnPython.com's courses are great in doing exactly that. They're tailored for people who have no previous coding experience and lead you very smoothly through information and exercises.

I've seen plenty of coding courses and they often jump from topic to topic, leaving you sometimes confused and unable to implement what you've just learned. This Python course is exactly the opposite and will keep you wanting to learn more.

What have you accomplished today? You've learned what data types are and you've learned about the three basic data types supported by Python that you'll always need. You've also learned what variables are, but only in theory. You will soon find out how you can use them in practice. One of the huge things you've learned is how to perform basic mathematical operations. Oh, and you've learned what a function is, almost by accident.

Did you find this introduction helpful? Did you find it easy to follow? Maybe too easy? Or maybe too much information for one article? Feel free to let me know in the comments section. I would like to hear from you so I can adapt the next article(s) to your needs. The point is to help you in the most efficient way possible. Don't be ashamed to have your say!