Data Science Python

A Simple Walk-through with Python for Data Science

Pinterest LinkedIn Tumblr
Learn Python Thumbnail

In the previous tutorial, I gave a brief introduction of what Python is about and how popular it’s getting in Data Science. If you’ve missed out on it, I strongly recommend you quickly go through it before this tutorial.

In today’s tutorial, we will learn about some fundamental concepts behind the Python programming language such as Python data types along with some examples. You will also learn about Loops with Python, Condition statements, Functions, Little notes (comments), Classes, and Error handling.

Introduction into Python

Assuming you’ve gone through the previous tutorial, you are now familiar with how Python is a Popular language for Data Science. Let’s start with a legendary welcome introduction into programming, which I’m sure everyone has written about before.

So open up a new file, name it python_tutorial.py, type in this code snippet and execute your code.

If you are coming with experience from other programming languages like C, C++, Java, you would have noticed you will not have to write the main function or class before getting any output displayed.

Data Types in Python

When programming, the basic building blocks are it’s data types. These data types define the type of the variable, or what a value should contain. Since Python is a dynamically typed language, these data types tend to change based on its value. So the interpreter understands what data type to use during execution. Which, of course, doesn’t happen in other programming languages.

The Data types I have mentioned includes:

  • Integers (int): Whole numbers like 10, 12, 50
  • Float (float): Number with a decimal point: 5.5, 45.3, 75.5
  • String (str): An ordered sequence of character: “Welcome”, “NeuraSpike”, “Python.”
  • Lists (list): Ordered sequence of objects: [“David”, 13, “NeuraSpike”, 55.5]
  • Dictionaries (dict): Unordered key and value pairs: {“Name” : “David”, “Company” : “NeuraSpike”}
  • Tuples (tup): Ordered immutable sequence of objects: (“NeuraSpike”, 2020, “Python”, 185.5)
  • Sets (set): Unordered collection of unique objects: {“NeuraSpike”, “Python”}
  • Booleans (bool): Logical values either True or False

Variables in Python

If it’s your first time learning about programming, you might be wondering what variables are. What is the purpose of them? Why do we use them?

You could think of variables as a place you can store values. For example, the example shows how the blog name “NeuraSpike” is allocated to the variable name brand. When you print the variable brand, you will get the value assigned, “NeuraSpike.”

Some rules you should be aware of variable names are:

There are no spaces in variable names; we use “_” instead

Variables names cannot start with numbers

Consist of names alpha-numeric characters (A-Z, a-z, 0-9) or underscore ( _ )

Variable names are case sensitive. As an illustration: the variable brand is different from BRAND

Strings in Python

Let’s say we are given a string, and our job is to convert the whole string assigned to a variable into uppercase or lowercase. How can we do that? With Python we can easily perform this operation in a single line of code:

Occasionally while working with either strings or other types of data structures like lists, tuples, array, dictionaries, etc. We might want to check the length and this can be done using the len() predefined function.

A question for you is what if we want to grab the first letter in our variable name called brand?  We can accomplish this using a process called indexing which we will explore more in-depth with a tutorial related to NumPy.

We can also get the last character in our string:

To extract the section “eura” from the variable brand (“eura” also coincidentally carries the meaning justice in Greek):

Let’s say we want to join two different strings together or concatenate two strings. Python lets us perform this operation by using the add operator (+).

Usually, you might have to break a large string into smaller chunks or strings based on a given separator at some point.

Note: this is the opposite of concatenation we have seen above, which adds two strings together.

It’s vital to know how to check each variable name’s data type using the predefined function called type.

Once you execute the script, you will surely get different results.

List in Python

Now that we have seen how we can manipulate strings, let’s experiment on working with lists. It’s also similar to a data structure called arrays from other programming languages for example Java, in which we can store a bunch of things inside it like Strings and Integers.

Instead of creating multiple variables for different letters of the same type, we can rewrite the process by defining a list and put all these values inside one container of the same type.

To get the list size, we can use the len() function, which returns the total number of items stored in our list.

Once you print out the list letters, you will get the output:

Now, what if we want to add another value, letter “d” and “e” to the list without having the define another list with the same values [‘a’, ‘b’, ‘c’].  We can use the predefined function .append() to add values to our list.

Now we know how to add values. What if we want to delete certain elements within the list? Using the .pop() predefined function, we can easily remove certain values added to the list. For example using .pop() or .pop(-1) “last element” / .pop(0) “first element” function removes either the last or first element by specifying the index within the parenthesis.

Note: we can decide on any element we desire.

We can also sort a list using the .sort() predefined method. Let’s create an unsorted list to see how it works.

It’s also important to know when using the .sort() predefined function; we cannot sort a list that contains both string (‘str’) and integers (‘int’). We will certainly get an error message (‘TypeError’) on the output console. We will see how we can resolve these error messages to avoid disrupting our program during execution time.

Dictionary in Python

Dictionaries in Python are an unordered data structure written with curly brackets and store key and value pairs.

Creating a dictionary of a person will look like this:

If we want to get a specific value, for example, the name, title, and brand, we can easily do it like this:

In the example, the keys are the “name,” “title,” and “brand” we can access these values with the .keys() predefined function.

We can also extract all the values using the .values() predefined function.

We can easily remove all items from the dictionary using the predefined clear() function.

Take away points from Python Dictionaries:

  • The keys must be unique and cannot have duplicates.

For example, having two keys with the same keys “name” assigned with “David Praise,” and another name is given a value “Sharry.” Once you print the output, it’s different.

Note: David Praise no longer exists in the person dictionary created. Suppose you are using an intelligent Integrated development environment like PyCharm. You will surely get a warning about having duplicate keys. Make sure you check out the PyCharm Community version developed by Intellij.

The values can be any data type mentioned above, such as Strings, Integers, Lists, and yes, even dictionaries.

Sets in Python

It’s important to know when working with sets, duplicates values are not allowed, and the elements order changes. Unlike lists, we could have a variety of values, and the order remains the same; however, using sets, that’s not possible.

We could also take the union between two sets: all the elements in set a and all the elements in set b.

We could also take the intersection, which is the elements the two sets have in common.

And the difference between two sets, written  letter_a – letter_b, is the set of all elements in letter_a that are not elements of letter_b.

We can also convert a list into a set if we are not interested in duplicate values.

If you’ve enjoyed the tutorial up until now, you should click on the “Click to Tweet Button” below to share on twitter. 😉

Check out a simple walkthrough with Python tutorial Click To Tweet

Tuples in Python

Tuples are a collection of objects which are ordered and immutable. Unlike lists, you can’t change a value once it’s already defined.

When working with lists, we can modify the values inside of it. However, when working with tuples, that’s not possible.

If and Else Statement

When you are making applications with any programming language, you will perform many decisions, and that’s when using condition statements comes into great use. These conditional statements I’ve mentioned allows us to execute a section of our code when a specific condition is satisfied.

It’s crucial to know these conditional statements are coupled with logical conditions from mathematics like:

  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b

Let’s write a piece of code using these logical conditions. Here we have the value 20 stored in a variable age, and we are checking if the age(20) is greater than the value 18.

It’s also vital to use indentation in Python to indicate a block of code to get your code compiled and executed successfully.

After the if statement, I used indentation then the print statement (“Legal to drive”) in the snippet. It belongs to the if statement if the age is greater than 18. Otherwise, the else statement if the age happens to be strictly less than 18.

Another example could be to check if a number is positive, negative, or zero. Here we used the elif (read as “else if”) statement, which runs when the condition within the if statement doesn’t hold.

Another case could be having a nested if statement meaning another if statement clause within an existing if statement to check if a person is of legal age to drive when the number is positive.

Another type of a conditional expression is called the Ternary Operator. This operator lets us test a condition in a single line of code, replacing a multiline if-else seen above to avoid having a compact code.

Note: I only advise you to use this only if you have a simple one-liner. However, avoid having these types if the decision you have to make is complex, to make your code more readable.

For loops in Python

Using loops in Python allows us to iterate through tuples, lists, dictionaries, sets, or any data structure available. For more in-depth knowledge of data structures, be sure to visit this site.

Using the range() function, we can create a sequence of numbers, starting from 0 by default, and increments by 1(by default), and stopping before a specific number since we start counting from 0 by default, only if not specified differently.

We can also sum all the numbers from 0 up until 9 using a for loop, instead of manually writing print(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8+ 9).

Using the for loop, we can start from 2 instead of 0 by default, incrementing by 2 and stop before 10.

Using a for loop, we can also decide to populate an empty list, with values from 0 up until 9.

In the above example, we have used over 4 lines of code to populate an empty list. However, we can do this in 2 lines by writing a for loop within an existing list. Then once executed, the values are being populated right inside of the empty list created.

From the previous example mentioned above, we can also iterate through a dictionary using a for loop. It’s important to know we specify a key and value index after calling the person.items() predefined function, which returns an array of key and value pairs. You may even print out person.item() on your console to see the result.

While Loops in Python

The while loop is another primitive loop command apart from the for loop. However, it works a bit differently. With this loop, we can execute a set of statements as long as a specific condition is true.

If we decided to run the statement, this is known as an infinite statement. Meaning it has no stopping condition because the condition statement is always true (number is always less than 5).


On the other hand, if we decided to increment the value number by one after each iteration, once the condition is not satisfied (number 6 is less than 5), this is False. Right there is our stopping condition.

We can also decide to add the else statement right after the while loop, and once the while statement isn’t true, the else statement is then executed.

Break and Continue in Python

You will undoubtedly use two keywords: the break and continue statement when working with both the while and for loops. Let’s see an example of working with the break keyword.

As the value stored in the number variable increases by 1 after each iteration, once the value equals 3, the break statement terminates the loop.

Using the continue statement, once the number equals 3 the print statement and any other instructions below are ignored. Also, looking at the output, you will notice the number 3 isn’t printed out.

Functions in Python

One great benefit of writing functions within your code is avoiding to duplicate code. This reduces a complicated program into smaller and more manageable chunks, which reduces our program’s overall complexity.

Once written, the block of function is executed once called.

When calling these functions, we can pass data known as parameters into a function that can return data. To define a function in Python, we use the def keyword.

Comments in Python

Another great practice while writing code is adding comments that describe the piece of code and what the functionality is all about. You could also refer to them as little notes for yourself.

Writing comments with Python is very simple. It’s done using the hashtag sign.

Another type of comment is the multi-line comment used when writing a large text description of code and can be done in this way:

Classes and Objects in Python

Classes are vital to understand. A class is a blueprint for creating anything in the real world you can think of that you can represent in code. From this blueprint, we can create one or many objects.

Let’s take for example, a car and a blueprint is the specification of how this car is created. With this class created, we should specify the attributes, which are anything that relates to the car. For example, the year, color, cost, model, make, etc.  Let’s see an example with Python.

We also have behaviors that represent what the object we created can do. For example, we can drive the car, turn the steering left or right, make a quick break, or even accelerate.

This is the world of object-oriented programming, which involves creating a bunch of classes and connecting them. It’s also good to know these behaviors can be accessed using the dot notation. Let’s see an example.

Handling Errors

If you have written some code up until now, you will have gotten some error messages, which are either:

  • Syntax errors: Error in the source code
  • Exceptions: Error detected during execution

While coding, most of these exceptions are not taken care of by programs, and Python allows for errors and exceptions to be handled by the program. To do so, we will need to use both the try and except statements, which are:

  • Try: Test a block of code for errors
  • Except: Handle the Error
  • Finally: Executes code regardless of the result of the try-except block.

For example, if we divided a number by zero will get a zero division error:

However, we can resolve this by warping the code inside of a try and except block.

For more information about error handling be sure to visit this site.

As you’ve made it to the end of this tutorial I strongly recommend you visit sites like HackerRank or LeetCode to practice problem solving which will help smoothen and accelerate your learning pace. Finally remember keep practicing. Practicing makes Improvements. Until then, you will hear from me next week.

Conclusion

In this post, you discovered the basic concept behind Python. You have learned about Python Data Types, Variables and String, Lists, Dictionaries, Sets, Tuples, if-else statements, for and while loops, break and continue statement, functions, comments, classes, and objects, and finally handling errors in Python.

Do you have any questions about Python or this post? Leave a comment and ask your question. I’ll do my best to answer.

To get all access to the source code used in all tutorials, leave your email address in any of the page’s subscription forms.

Further Reading

We have listed some useful resources below if you thirst for more reading.

Articles

Books

To be notified when this next blog post goes live, be sure to enter your email address in the form!

10 Comments

    • David Praise Chukwuma Kalu Reply

      It’s my pleasure. Feel free to share among your network.

  1. This is the most comprehensive post i’ve ever read related to Python. Please keep writing more content.

  2. I love the design on your site also your cheatsheet is well detailed. Thank you.

  3. You have the best content related to Python on the internet. Thank you for sharing. I’m looking up for more of your tutorials.

  4. Your content are top notch. Keep on writing more posts related to Python.

Write A Comment