Table of Contents
Beginners in the field of data science who are not familiar with programming often have a hard time figuring out where they should start.
With hundreds of questions about how to get started with Python for Data Science on various forums, this post is an answer to settle all those questions.
So, here are the fundamentals to help you with programming in Python.
A basic Python curriculum can be broken down into 4 essential topics that include:
- Data types (int, float, strings)
- Compound data structures (lists, tuples, and dictionaries)
- Conditionals, loops, and functions
- Object-oriented programming and using external libraries
Let’s go over each one and see what are the fundamentals you should learn.
Learn Coding in your Language! Enroll Here!
1. Data Types and Structures
The very first step is to understand how Python interprets data.
Starting with widely used data types, you should be familiar with integers (int), floats (float), strings (str), and booleans (bool). Here’s what you should practice.
Type, typecasting, and I/O functions:
- Learning the type of data using the
type()
method.
type('Harshit')
# output: str
- Storing values into variables and input-output functions (
a = 5.67
) - Typecasting — converting a particular type of variable/data into another type if possible. For example, converting a string of integers into an integer:
astring = "55"
print(type(astring))
# output: <class 'str'>
astring = int(astring)
print(type(astring))
# output: <class 'int64'>
Once you are familiar with the basic data types and their usage, you should learn about arithmetic operators and expression evaluations (DMAS) and how you can store the result in a variable for further use.
answer = 43 + 56 / 14 - 9 * 2
print(answer)
# output: 29.0
Strings:
Knowing how to deal with textual data and their operators comes in handy when dealing with the string data type. Practice these concepts:
- Concatenating strings using
+
- Splitting and joining the string using the
split()
andjoin()
method - Changing the case of the string using
lower()
andupper()
methods - Working with substrings of a string
Are you aspiring for a booming career in IT? If YES, then dive in |
||
Full Stack Developer Course |
Python Programming Course |
Data Science and Machine Learning Course |
2. Compound data structures (lists, tuples, and dictionaries)
1: Which of the following algorithms is most suitable for classification tasks?
Lists and tuples (compound data types):
One of the most important and commonly used data structures in Python are lists. A list is a collection of elements, and the collection can be of the same or varied data types.
Understanding lists will eventually pave the way for computing algebraic equations and statistical models on your array of data.
Here are the concepts you should be familiar with:
- How multiple data types can be stored in a Python list.
- Indexing and slicing to access a specific element or sub-list of the list.
- Helper methods for sorting, reversing, deleting elements, copying, and appending.
- Nested lists — lists containing lists. For example,
[1,2,3, [10,11]]
. - Addition in a list.
alist + alist
# output: ['harshit', 2, 5.5, 10, [1, 2, 3], 'harshit', 2, 5.5, 10, [1, 2, 3]]
Multiplying the list with a scalar:
alist * 2
# output: ['harshit', 2, 5.5, 10, [1, 2, 3], 'harshit', 2, 5.5, 10, [1, 2, 3]]
Tuples are an immutable ordered sequence of items. They are similar to lists, but the key difference is that tuples are immutable whereas lists are mutable.
Concepts to focus on:
- Indexing and slicing (similar to lists).
- Nested tuples.
- Adding tuples and helper methods like
count()
andindex()
.
Dictionaries
These are another type of collection in Python. While lists are integer indexed, dictionaries are more like addresses. Dictionaries have key-value pairs, and keys are analogous to indexes in lists.
To access an element, you need to pass the key in squared brackets.
Concepts to focus on:
- Iterating through a dictionary (also covered in loops).
- Using helper methods like
get()
,pop()
,items()
,keys()
,update()
, and so on.
3. Conditionals, Loops, and Functions
Conditions and Branching
Python uses these boolean variables to assess conditions. Whenever there is a comparison or evaluation, boolean values are the resulting solution.
x = True
ptint(type(x))
# output: <class bool>
print(1 == 2)
# output: False
The comparison in the image needs to be observed carefully as people confuse the assignment operator (=
) with the comparison operator (==
).
Learn to code from industry experts! Enroll here
Boolean operators (or, and, not)
These are used to evaluate complex assertions together.
or
— One of the many comparisons should be true for the entire condition to be true.and
— All of the comparisons should be true for the entire condition to be true.not
— Checks for the opposite of the comparison specified.
Concepts to learn :
if
,else
, andelif
statements to construct your condition.- Making complex comparisons in one condition.
- Keeping indentation in mind while writing nested
if
/else
statements. - Using boolean,
in
,is
, andnot
operators.
Loops
Often you’ll need to do a repetitive task, and loops will be your best friend to eliminate the overhead of code redundancy. You’ll often need to iterate through each element of a list or dictionary, and loops come in handy for that. while
and for
are two types of loops.
get into most in demand career by learning python programming !
List Comprehension
A sophisticated and succinct way of creating a list using and iterable followed by a for
clause.
For example, you can create a list of 9 cubes as shown in the example above using list comprehension.
# list comprehension
cubes = [n** 3 for n in range(1,10)]
print(cubes)
# output: [1, 8, 27, 64, 125, 216, 343, 512, 729]
Functions
While working on a big project, maintaining code becomes a real chore. If your code performs similar tasks many times, a convenient way to manage your code is by using functions.
A function is a block of code that performs some operations on input data and gives you the desired output.
Using functions makes the code more readable, reduces redundancy, makes the code reusable, and saves time.
Python uses indentation to create blocks of code. This is an example of a function:
def add_two_numbers(a, b):
sum = a + b
return sum
We define a function using the def
keyword followed by the name of the function and arguments (input) within the parentheses, followed by a colon.
The body of the function is the indented code block, and the output is returned with the return
keyword.
You call a function by specifying the name and passing the arguments within the parentheses as per the definition.4. Object-Oriented programming and using external libraries
We have been using the helper methods for lists, dictionaries, and other data types, but where are these coming from?
When we say list or dict, we are actually interacting with a list class object or a dict class object. Printing the type of a dictionary object will show you that it is a class dict object.
These are all pre-defined classes in the Python language, and they make our tasks very easy and convenient.
Objects are instance of a class and are defined as an encapsulation of variables (data) and functions into a single entity. They have access to the variables (attributes) and methods (functions) from classes.
Using External Libraries/Modules
One of the main reasons to use Python for data science is the amazing community that develops high-quality packages for different domains and problems. Using external libraries and modules is an integral part of working on projects in Python.
These libraries and modules have defined classes, attributes, and methods that we can use to accomplish our tasks. For example, the math
library contains many mathematical functions that we can use to carry out our calculations. The libraries are .py
files.
You should learn to:
- Import libraries in your workspace
- Using the
help
function to learn about a library or function - Importing the required function directly.
- How to read the documentation of the well-known packages like pandas, numpy, and sklearn and use them in your projects
Grab the opportunity to learn Data Science with Entri! Click Here
Introduction To Machine Learning using Python
Machine learning is a type of artificial intelligence (AI) that provides computers with the ability to learn without being explicitly programmed. Machine learning focuses on the development of Computer Programs that can change when exposed to new data. Here, we will see basics of Machine Learning, and implementation of a simple machine learning algorithm using python.
Setting up the environment
Python community has developed many modules to help programmers implement machine learning. In this article, we will be using numpy, scipy and scikit-learn modules. We can install them using cmd command:
pip install numpy scipy scikit-learn
A better option would be downloading miniconda or anaconda packages for python, which come prebundled with these packages. Follow the instructions given here to use anaconda.
Machine Learning overview
Machine learning involves a computer to be trained using a given data set, and use this training to predict the properties of a given new data. For example, we can train a computer by feeding it 1000 images of cats and 1000 more images which are not of a cat, and tell each time to the computer whether a picture is cat or not. Then if we show the computer a new image, then from the above training, the computer should be able to tell whether this new image is a cat or not.
The process of training and prediction involves the use of specialized algorithms. We feed the training data to an algorithm, and the algorithm uses this training data to give predictions on a new test data. One such algorithm is K-Nearest-Neighbor classification (KNN classification). It takes a test data, and finds k nearest data values to this data from test data set. Then it selects the neighbor of maximum frequency and gives its properties as the prediction result. For example if the training set is:
petal_size | flower_type |
---|---|
1 | a |
2 | b |
1 | a |
2 | b |
3 | c |
4 | d |
3 | c |
2 | b |
5 | a |
Now we want to predict flower type for petal of size 2.5 cm. So if we decide no. of neighbors (K)=3, we see that the 3 nearest neighbors of 2.5 are 1, 2 and 3. Their frequencies are 2, 3 and 2 respectively. Therefore the neighbor of maximum frequency is 2 and flower type corresponding to it is b. So for a petal of size 2.5, the prediction will be flower type b.
Our Other Courses | ||
MEP Course | Quantity Surveying Course | Montessori Teachers Training Course |
Performance Marketing Course | Practical Accounting Course | Yoga Teachers Training Course |