The world’s leading publication for data science, AI, and ML professionals.

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…

Python for Data Science: A Beginner’s Guide

Learn the Python basics so that you can use it for your data science projects.

Image by Johnson Martin from Pixabay
Image by Johnson Martin from Pixabay

I. About Python 🐍

Created by Dutch programmer Guido van Rossum at Centrum Wiskunde & Informatica, Python made its debut in 1991. Over thirty years it has gained popularity earned a reputation of being the "Swiss army knife of programming languages." Here are a few reasons why:

In emerging fields like data science, artificial intelligence, and machine learning, a robust community, plenty of packages, paradigm flexibility, and syntactical simplicity, allow beginners and professionals to focus on insights and innovation.

III. 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 next tutorial: Pandas I.

1. Python 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.

# This is a comment. The line below is 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 equal-sign (=) is used to assign a value to a variable. Here are a few caveats about variables:

  • Naming conventions indicate that variables should be lowercase
  • Letters and numbers are okay, but numbers cannot be used without letters (e.g. "a1" would work, while "1", will not).
  • Special characters and spaces are not allowed. Underscores ("_") should be used in place of spaces
  • You can name a variable anything you want, but there are a few names you cannot use because they already Python keywords. 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 all outputs, including strings 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
      print(a_combo_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).

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. Python Integers

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

[in]  int_num = 1
      print(int_num)
[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 
      print(c)
[out] -1

You can also multiply and divide:

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

3. Python 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
      print(num_float)
      d = 1.33
      e = 4.67
      f = d + e
      g = f - 7
      print(g)
[out] 2.5
      1.0

4. Python Booleans

Boolean values are either True or False.

[in]  print(type(True))
      print(type(False))
[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. Python 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]
      print(our_list)
[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
      our_list.append(h)
      print(our_list)
[out] [5, 6, -1, 1.33, 4.67, 6.0, -1.0, 7]

We can remove things with remove():

[in]   our_list.remove(a)
       print(our_list)
[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
      print(combined_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 operator (+=):

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

Finally, we can compare lists using equality operator ("==") to get a Boolean output.

[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.


Related Articles