Python Fundamentals for Data Science

Daizy Obura
Analytics Vidhya
Published in
6 min readNov 3, 2020

--

In my previous post, I mentioned that I am going to launch into the field of Data Science while using python to achieve this. But before launching into the deep, it is necessary to understand the underlying concepts. I took a week off to get into this, and I am ready to share what I was able to study.

In this post, we shall go over certain concepts and study a few examples. I will also be attaching a useful link at the end of each topic, to help you better understand. Open up your code editor and let's get busy!

content = ["Variables", "Data Structures", "Conditional Statements", "Iterations", "Functions", "Lambda Expressions", "OOP"]
covered = []
print("All Topics: {}".format(content))def check_list():
if len(covered) == 0:
return "No topic covered yet"
else:
return covered
def update_list(topic):
covered.append(topic)
return covered
def alter_content(topic):
if topic in content:
content.remove(topic)
print("\nCovered: {}\nCurrent Topic: {}\n".format
(check_list(), topic))
update_list(topic)
else:
print("Topic is not part of the content")

1. Variables

alter_content("Variables")>>All Topics: ['Variables', 'Data Structures', 'Conditional Statements', 'Iterations', 'Functions', 'Lambda Expressions', 'OOP']

>>Covered: No topic covered yet
>>Current Topic: Variables

In python, a variable is created when you assign a value to it. For example name = "Daizy" . “name” is now a variable referencing the value “Daizy”. A variable can refer to a new value for example name = "Daphne" . Values can be of different types for example strings, integers, floats.

Examples of valid variable names include: name, my_name. However, names like 2car, car-name, car name are invalid.

2. Data Structures

alter_content("Data Structures")>>Covered: ['Variables']
>>Current Topic: Data Structures

The most common data structures used in python are mutable (lists, dictionaries) — the values they refer to can be manipulated, and immutable(tuples, strings) — the values can not be manipulated.

These data structures are useful for holding groups of related or unrelated data.

  • Lists are useful for holding ordered data. passwords = ["rga34jd", 11111, "DaizyO", "HighlyFavoured", 00009, 34]
  • Dictionaries are useful for holding key/pair values. student_results = {"John" : 90, "Mark" : 98, "Mary" : 94, "Michael" : 99}
  • Tuples are useful for protecting data. tuple1 = ("yes", "no", "maybe")
  • Strings are useful as sequences of characters. name = "Daizy"

Visit this link to access tutorials about these data structures and how to manipulate them.

3. Decision Making

alter_content("Conditional Statements")

>>Covered: ['Variables', 'Data Structures']
>>Current Topic: Conditional Statements

Suppose you want your program to take different routes of execution when different conditions are met, if statements are the best option to use. Python offers a neat and simple syntax for these statements:

if/then                                    elif
if expression: if expression:
Statement Statement
else: elif expression:
Statement Statement
else:
Statement

If you have more than 2 conditions to cross-check, the elif statement is available for this. Take an example:

print("Enter your marks:")
marks = int(input())
print("Your mark is {}".format(marks))if (60 <= marks <= 69):
grade = "D"
elif (70 <= marks <= 79):
grade = "C"
elif (80 <= marks <= 89):
grade = "B"
elif (marks >= 90):
grade = "A"
else:
grade = "F"

print("Grade: {}".format(grade))

If I enter 81 as the mark, it will be checked across every condition up to the one that fulfills it; the third in this case, as 80 < 81 <89 is true. And that's how if statements work.

A useful resource for more on decision making.

4. Iterations

alter_content("Iterations")

>>Covered: ['Variables', 'Data Structures', 'Conditional Statements']
>>Current Topic: Iterations

Iterations are useful for going over sequences, until a certain condition is met (while loop) or when you know how many times it should be executed (for-loop). If you have a sequence, say a list, it provides starting and ending points for iteration.

family = [{"name" :"Dee", "age" : 19, "gender" : "F"}, {"name" :"Tim", "age" : 49, "gender" : "M"}]for member in family:
print("\n{} \n{} \n{}".format(member["name"], member["age"], member["gender"]))

The loop will go over every element in the family list and perform some action, printing the name, age, and gender in this case.

5. Functions

alter_content("Functions")

>>Covered: ['Variables', 'Data Structures', 'Conditional Statements', 'Iterations']
>>Current Topic: Functions

Functions are blocks of reusable code that perform a certain task(s). Function syntax:

def function_name(parameters):
suite
expression

Let’s refer to the first block of code in this post to get an idea of how to create and use functions. There are three functions in that block but we’ll focus on one of them: alter_content(topic) as it is the one we have been calling every time we are going to start a new topic.

def alter_content(topic):
if topic in content:
content.remove(topic)
print("\nCovered: {}\nCurrent Topic: {}\n".format
(check_list(), topic))
update_list(topic)
else:
print("Topic is not part of the content")

alter_content takes exactly one parameter, topic and uses this parameter to perform certain actions:

 Check if the topic is in the content list
- If so:
1. Remove it from that list
2. Print some output
3. Call another function to update another list of topics that have been covered
- If not:
1. Print some output

To make use of this function, we do a function call alter_content()outside the function and in this case, pass the expected parameter topic for example alter_content("Daizy") . This call will yield "Topic is not part of the content” as the output, because "Daizy” is not in the content list.

The usefulness of functions is clear in this context and if I had not written a function, I would have had to write that huge block of code every time I wanted to manipulate the lists and produce some output. But because I wrote a function, all I had to neatly do is call it as I described above.

Refer to this tutorial for more on functions.

6. Lambda Expressions

alter_content("Lambda Expressions")

>>Covered: ['Variables', 'Data Structures', 'Conditional Statements', 'Iterations', 'Functions']
>>Current Topic: Lambda Expressions

Lambda expressions are anonymous functions, that is, they don’t have names as regular functions do. They are useful for one-time use and in functions that take in other functions as parameters.

They are one-line functions that take the form lambda parameters : expression . They allow for many parameters but only one expression.

Take this as an example: I have a list of integers whose squares I want to retrieve. I can use the built-in map() function to apply some function, that will return the square of each integer in the list.

numbers = [1, 2, 3, 4, 5]
squares = map(lambda x : x*x, numbers)
squares_list = list(squares)

You can see that this was pretty simple and neat with the one-line lambda function. This tutorial is a good starting point for understanding lambda functions further.

7. Object-Oriented Programming

alter_content("OOP")

>>Covered: ['Variables', 'Data Structures', 'Conditional Statements', 'Iterations', 'Functions', 'Lambda Expressions']
>>Current Topic: OOP

Object-oriented programming is a concept that groups information into classes of objects. A class is a blueprint for an object and an object is an instance of a class. Python supports this concept by allowing us to define reusable classes that can be used to created instances.

For example, the Animal class , from which many instances of animals can be created, with certain attributes like name and number of legs and certain behavior like eating:

class Animal:
def __init__(self, name, no_of_legs):
self.name = name
self.no_of_legs = no_of_legs

def eat(self, food):
print("{} eats {}".format(self.name, food))
dog = Animal("Dog", 4)
print(dog.eat("Bones"))
>>"Dog eats Bones"

For more on python OOP, refer to this link.

Photo by Lance Grandahl on Unsplash

Hooray! You made it to the end! 🙌 It has been great going over these concepts and sharing my understanding of them with you. I hope some value has been added to you, and that we can now use python for Data Science going forward. Cheers!

--

--

Daizy Obura
Analytics Vidhya

Believer in our Lord Jesus Christ | Techie | Web Developer | Interior Decorator | Jeweler | Learn, Unlearn, Re-learn