Discuss@GL4L

Python Basics for Data Science


#1

Python Data Types
In Python, we have many data types. The most common ones are float (floating point), int (integer), str (string), bool (Boolean), list.

Float - Used for real numbers.
Int - Used for integers.
Str - Used for texts. We can define strings using single quotes’value’, double quotes"value", or triple quotes""“value”"". The triple quoted strings can be on multiple lines, the new lines will be included in the value of the variable. They’re also used for writing function documentation.
Bool - Used for truthy values. Useful to perform a filtering operation on a data.
List - Used to store a collection of values.
We can use the type(variable_name) function to check the type of a specific variable. Operators in Python behave differently depending on the variable’s type and there are different built-in methods for each one.

Python Lists
Python list is a basic sequence type. We can use this type to store a collection of values. One list can contain values of any type. It’s not commonly used, but you can have a list with a mix of Python types. You can create a new one using square brackets like this:

fruits = [“pineapple”, “apple”, “lemon”, “strawberry”, “orange”, “kiwi”]

Subsetting Lists
You can use indexes to get element or elements from the list. In Python, the indexes start from 0. Therefore, the first element in the list will have an index 0. We can also use negative indexes to access elements. The last element in the list will have an index -1, the one before the last one will have an index -2 and so on. We have also something called “list slicing” in Python which can be used to get multiple elements from a list. We can use it like this: sliceable[start_index:end_index:step].

The start_index is the beginning index of the slice, the element at this index will be included to the result, the default value is 0.
The end_index is the end index of the slice, the element at this index will not be included to the result, the default value will be the length of the list. Also, the default value can be - length of the list -1 if the step is negative. If you skip this, you will get all the elements from the start index to the end.
The step is the amount by which the index increases,
the default value is 1.

fruits = [“pineapple”, “apple”, “lemon”, “strawberry”, “orange”, “kiwi”]
fruits[1] # apple
fruits[0] # “pineapple”
fruits[-1] # “kiwi”
fruits[5] # “kiwi”
fruits[-3] # “strawberry”

List slicing

fruits[::] # [“pineapple”, “apple”, “lemon”, “strawberry”, “orange”, “kiwi”]
fruits[0:2] # [“pineapple”, “apple”]
fruits[-2:-1] # [“orange”]
fruits[3:] # [“strawberry”, “orange”, “kiwi”]
fruits[:4] # [“pineapple”, “apple”, “lemon”, “strawberry”]
fruits[:] # [“pineapple”, “apple”, “lemon”, “strawberry”, “orange”, “kiwi”]
fruits[::-1] # [“kiwi”, “orange”, “strawberry”, “lemon”, “apple”, “pineapple”]
fruits[::-2] # [“kiwi”, “strawberry”, “apple”]
fruits[::2] # [“pineapple”, “lemon”, “orange”]

Understanding some default values

fruits[0:6:1] # the same result as the result from fruits[::]
fruits[-1:-7:-1] # the same result as the result from fruits[::-1]

List Manipulation
We can add element or elements to a list using append method or by using the plus operator. If you’re using the plus operator on two lists, Python will give a new list of the contents of the two lists.
We can change element or elements to list using the same square brackets that we already used for indexing and list slicing.
We can delete an element from a list with the remove(value) method. This method will delete the first element of the list with the passed value.

Add values to a list

fruits.append(“peach”)
fruits # [“pineapple”, “apple”, “lemon”, “strawberry”, “orange”, “kiwi”, “peach”]
fruits = fruits + [“fig”, “melon”]
fruits # [“pineapple”, “apple”, “lemon”, “strawberry”, “orange”, “kiwi”, “peach”, “fig”, “melon”]

Change values from a list

fruits[0:2] = [“grape”, “mango”]
fruits # [“grape”, “mango”, “lemon”, “strawberry”, “orange”, “kiwi”, “peach”, “fig”, “melon”]

Delete values from a list

fruits.remove(“mango”)
fruits # [“grape”, “lemon”, “strawberry”, “orange”, “kiwi”, “peach”, “fig”, “melon”]

It’s important to understand how lists work behind the scenes in Python. When you create a new list my_list, you’re storing the list in your computer memory, and the address of that list is stored in the my_list variable. The variable my_list doesn’t contain the elements of the list. It contains a reference to the list. If we copy a list with the equal sign only like this my_list_copy = my_list, you’ll have the reference copied in the my_list_copy variable instead of the list values. So, if you want to copy the actual values, you can use the list(my_list) function or slicing [:].

numbers = [10, 42, 28, 420]
numbers_copy = numbers
numbers_copy[2] = 100
numbers # [10, 42, 100, 420]
numbers_copy # [10, 42, 100, 420]

ratings = [4.5, 5.0, 3.5, 4.75, 4.00]
ratings_copy = ratings[:]
ratings_copy[0] = 2.0
ratings # [4.5, 5.0, 3.5, 4.75, 4.0]
ratings_copy # [2.0, 5.0, 3.5, 4.75, 4.0]

characters = [“A”, “B”, “C”]
characters_copy = list(characters)
characters_copy[-1] = “D”
characters # [“A”, “B”, “C”]
characters_copy # [“A”, “B”, “D”]

source link: https://towardsdatascience.com/python-basics-for-data-science-6a6c987f2755