Python I: Data Types and Operators, variable assignment, and print()

In the previous section I showed you how to set up your computer for a data science project in Python. In this tutorial, I’ll tell you about the basic data types and operators in Python.

Image by Johnson Martin from Pixabay

This blog is part of a series of tutorials called Data in Day. Follow these tutorials to create your first end-to-end data science project in just one day. This is a fun easy project that will teach you the basics of setting up your computer for a data science project and introduce you to some of the most popular tools available. It is a great way to get acquainted with the data science workflow.

I. Data Types

Python has about twelve different data types and in this tutorial I’ll introduce you to five of them: string, integer, float, boolean, and list. I’ll also show you some basic functions and methods that you can use in Python for your project. This should be enough to get you started and prepare you for the Pandas I tutorial.

1. Strings

Strings are a sequence of characters. They can be numeric and non-numeric characters. In Python, they are surrounded by quotes. You’ll notice above the string there is a description that comes after a hashtag. This is called a comment. Anything after a hashtag will be a comment.

# a string in Python
“This is a string.” # you can also use them inline

In Python, we assign values of any type to variables in order to work with them. That looks something like this:

the_string = “This is a string.”

A single “=” is used to assign a value to a variable. You can name a variable anything you want, but there are a few names you cannot use because they already represent something important. See the list below:

False      class      finally    is         return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
pass else import assert
break except in raise

To print outputs in Python we use print():

[in]  print(the_string)
[out] "This is a string."

You can concatenate or put strings together like this:

[in]  another_string = “This is another string.”
a_combo_string = the_string + another string
[out] "This is a string. This is another string."

Strings are immutable, which means they cannot be changed in place. They need to be reassigned to be changed (you can recycle the variable if you like). In Python II, I go into more detail about how strings can be manipulated .

If you want to know the data type of any value or variable, you can use type().

[in]  print(type(the_string))
[out] <class 'str'>

2. Integers

Integers in Python are who numbers without a decimal point. They can be positive or negative.

[in]  int_num = 1
[out] 1

You can use arithmetic operators to perform operations on numeric data types integers.

[in]  print(1 + 2)
[out] 3

You can assign them to variables and perform operations as well.

[in]  a = 5
b = 6
c = a — b
[out] -1

You can also multiply and divide:

[in]  print(a * b / c)
[out] -30.0

3. Floats

Floats are a numeric data type, like integers. They can also be positive and negative. However they are floating point decimals, which means they have a decimal point and are not whole numbers. However, most of the operations we can perform on integers we can perform on floats.

[in]  int_float = 2.5
d = 1.33
e = 4.67
f = d + e
g = f — 7
[out] 2.5

4. Boolean

Boolean values are either True or False.

[in]  print(type(True))
[out] <class 'bool'>
<class 'bool'>

You can compare values using boolean operators.

[in]  print(d > e)   # d greater than g 
[out] False
[in] print(c >= g) # c greater than or equal to g
[out] True
[in] print(a < e) # a less than e
[out] False
[in] print(d <= g) # d less than or equal to g
[out] False
[in] print(g == c) # g equals c
[out] True

5. Lists

Lists in Python are containers to store values. They are surrounded by brackets, and we generally assign them to variables.

[in]  our_list = [a, b, c, d, e, f, g]
[out] [5, 6, -1, 1.33, 4.67, 6.0, -1.0]

They can be numeric values, non-numeric values.

[in]  i = “Ice”
j = “Jewel”
k = “Karate”
l = “Lemon”
another_list = [i, j, k, l]

They can be sorted as well.

[in]  sorted(our_list)
[out] [-1, -1.0, 1.33, 4.67, 5, 6, 6.0]

However, if we want the list to remain sorted as we manipulate it going forward, we need to reassign it:

[in] our_sorted_list = sorted(our_list)

We can add to lists with append():

[in]  h = 7
[out] [5, 6, -1, 1.33, 4.67, 6.0, -1.0, 7]

We can remove things with remove():

[in]   our_list.remove(a)
[out] [6, -1, 1.33, 4.67, 6.0, -1.0, 7]

We can concatenate (put together) two lists:

[in]  combined_list = our_list + another_list
[out] [6, -1, 1.33, 4.67, 6.0, -1.0, 7, 'Ice', 'Jewel', 'Karate', 'Lemon']

We can also put lists together using add-assign or “+=”:

[in]  another_combo_list = []
another_combo_list += combined_list
[out] [6, -1, 1.33, 4.67, 6.0, -1.0, 7, 'Ice', 'Jewel', 'Karate', 'Lemon']

Finally, we can compare lists using boolean operators:

[in]  print(combined_list == another_combo_list)
[out] True

II. What Did We Do?

  1. Discovered five different data types in Python: integer, float, boolean, string, and list.
  2. Discussed variable assignment, print(), type(), and comments.
  3. Learned about arithmetic operators and boolean operators.
  4. Experimented with lists.

IV. What is Next?

In Pandas I, you will learn how to use Python and Pandas to analyze the Metal Bands by Nation data set.

If you enjoyed this blog, check out Data in Day.

🐍 Python Enthusiast 🐼 Pandas Aficionado ⭐ Experience NLP using SpaCy and NLTK 🔎 Relentlessly curious 👩🏻‍💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store