In Python, data types are fundamental building blocks that define the kind of information a variable can hold and the operations that can be performed on it. These types act like labels, dictating how the data is stored in memory and what functions or operations are applicable. Understanding data types is essential for writing effective and efficient Python programs, as they directly influence how your code manipulates and processes information.

Types of Data

  • str - String is represents text data, essentially sequences of characters. Ideally when building block for storing and working with textual information.
  • int - Integers, whole numbers like 1, -5, or 0. They can’t have decimal places. What makes Python’s integers special is their size. Unlike some other programming languages, Python’s int type isn’t limited in how big or small a whole number it can be, as long as your computer’s memory allows it.
  • float - Floating point number - These are numbers with decimal points, allowing you to store and work with values like 3.14159 (pi), 10.5, or -2.71.
  • bool - Booleans - In Python we often need to answer yes-or-no questions. This is where booleans come in. They act like tiny decision-makers, storing either True or False based on conditions.
  • NoneType - None refers to a special data type that represents the absence of a value. It’s essentially a way to say a variable doesn’t hold any data or has no assigned value yet. Think of it as an empty container.

Lists

As data scientist we often want to work with many data point. Let’s say that we want to measure the net income of a banch of people and store this information in Python, it would be inconvenient to create muliple veriables for each person. So what we can do instead is store all this information into a list. This is where lists come in handy. They act like a container where you can store multiple data points of the same type, all in one place. They act like a container where you can store multiple data points of the same type, all in one place. In the following example we can see a list that build from floating data points, this list stores information about the net income of 3 people:

1
2
3
4
x = [9586.64 , 10448.0, 15671.99]
print(type(x))
print(type(x[0]))
print(x)

Output:

<class 'list'>
<class 'float'>
[9586.64, 10448.0, 15671.99]

Features

  1. With lists you can name a collection of values.
  2. List can contain any type of data
  3. List can contain sub-lists: [1, [1,2,3], ["a","b","c"], "Z"]
  4. A value can contain calculations or groups of numbers, for example: list = ["a" * 5, 2] # This will print the following list ['aaaaa', 2]
  5. Lists in Python are iterable, meaning you can use loops like while and for to efficiently access and process each element within the list.
    For example: lets build a list that contains all the odd numbers between 0 to 100: odd = [n for n%2!=0 in range (0, 101)] # Will print [1,3,5,7,9, and go on] 

Subsetting Lists

Imagine a list as a shelf full of items. To grab a specific item, you need to know its location. In Python lists, this location is identified by a number called an index. It’s like a tag on each item, starting from 0. The first item on the shelf (like the first book on the left) has an index of 0, the second item (next book) has an index of 1, and so on. This way, you can tell Python exactly which item you want to work with.

Have you know what is negative index? Think of it like starting from the back!
While regular indexing begins at the front (0), negative indexing starts at the end of the list. So, -1 refers to the last element, -2 to the second-last, and so on. This can be handy for quickly accessing elements from the end of the list without needing to know its exact length.

1
2
3
new_list = ['Dolev', 33, 'Michal', 27, 'Orr', 7, "Be'eri - Nathan", 0.6] # This is our raw list
print(new_list[0]) # Telling python to print the first item
print(new_list[-1]) #Telling python to print the last item

Output:

1
2
Dolev
0.6

Slicing

Slicing in Python lists is a powerful technique for extracting a specific subset of elements from a list. It’s like cutting a slice out of a cake! Here’s a breakdown of how it works:

Basic Slicing:

Imagine you have a list of fruits:

1
fruits = ["apple", "banana", "cherry", "mango", "orange"]

You can use slicing to extract a portion of this list using square brackets [] along with start and stop indexes separated by a colon :.

  • Without arguments: fruits[:] - This retrieves the entire list, copying all elements from the beginning (index 0) to the end (implicit index referring to the element just before the end).
  • Specifying start index: fruits[1:] - This extracts elements starting from the specified index (index 1 in this case, which is “banana”) and goes all the way to the end of the list.
  • Specifying stop index: fruits[:3] - This extracts elements from the beginning of the list (index 0) up to, but not including, the specified stop index (index 3 in this case, which is “mango”).

Key Points:

  • Slicing always creates a new list, it doesn’t modify the original list.
  • The stop index is exclusive, meaning the element at that index is not included in the sliced list.
  • You can omit either the start or stop index to use defaults (0 for start and the list’s length for stop).

Advanced Slicing:
Slicing offers more flexibility by allowing you to specify a step value:

1
fruits[1::2]  # This extracts "banana" and "orange" (every other element starting from index 1)

Here, the step value is 2, meaning it takes every second element from the starting index (1) onwards.

Negative Indexes:
You can also use negative indexes for slicing to start from the end of the list:

1
fruits[-2:]  # This extracts "mango" and "orange" (last two elements)

Final words

Today we learned about different data types and working with lists. While this is just a basic lesson, it is the foundation for more advanced topics that will come in future chapters. I hope you enjoyed it.

Photo by RDNE Stock project: https://www.pexels.com/photo/white-text-on-black-paper-8580801/