Python for Beginners 04: Basic Types in Python

After two more theoretical posts on comments and programming principles, it is time to practice a bit more.


What are we going to learn?

The basic data types that are used in Python, when to use which one and why distinguishing them is important.


What do you need (to know)?

Nothing. You don’t even have to read the previous posts on calculations, comments, or the principles of Python. You just need a program that can execute Python code such as Thonny.


Before we get started with practicing, we need a quick introduction to the types of data used in Python. Basically, there are three groups of variables:

a) numeric variables = numbers

  • without decimals → integer implemented with the function int() [you probably remember them from the previous posts]
  • with decimals → floating-point numbers implemented with the function float()

b) alphanumeric variables: numbers, letters and other symbols

  • a single character → not really useful
  • sequences of character data strings → strings implemented with the function str()

c) variables of Boolean data type: may have one of two values, True or False
(Attention: make sure it starts with a capital letter)

The variable type matters because it decides which operations you can execute with your code. Add two numbers, for instance 26 and 9, as strings (a), one as string and the other as integer (b), and both as integers (c). What are the results?

If you add two number that are formatted as strings (standard option when you get them as an input from a user), you just get the two numbers combined as a result (a). If you want to get the sum, both numbers need to be numeric variables, so either integer or float (c). In this case, the integer type is enough, but you might as well format both as float. What is important is that the variables you want to do operations with are from the same group. Otherwise, you will get an error message (b).

Did you notice how I used comments to explain what I am doing and to prevent the code in a) and b) from executing, also to avoid the error message from b)?

Boolean variables are useful to evaluate whether an expression is true or not. You can just use the function bool(), write some expression in brackets and when executing the code, the result will be either True and False. Try it yourself in Python: If a = 4 and b = 5, are the following three expressions true or false?

d) a > 9
e) a < (3+b)
f) (90-b) <= a

Did you remember that Python will only display the result if you print it? This means, of course, that you have to save the result in a variable before. Lesson 5 will focus entirely on output, so don’t worry if it is still challenging for you.

A final piece of advice: If you don’t know what type of variable you are using, you can always use the function type() and just write the name of the variable inside the brackets. Python will give you the answer using the term class and indicating Python’s abbreviation of the data type, i.e. the one that you use to define a variable’s type.

Congratulations! You have mastered lesson 4! #mastery04

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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