Python for Data Science: Part II
Python is a powerful programming language used extensively in the field of data science. It has become the go-to language for data scientists because of its simplicity, flexibility, and the availability of various data science libraries. It is one of the top programming skills sought by employers. This is a quick tutorial for Python for Data Science.
Python Basics
In the first part of the tutorial, we have seen how to install python and important libraries.
Get Serawork Wallelign’s stories in your inbox
Join Medium for free to get updates from this writer.
In this tutorial, we will cover some of the essential Python basics, including data types and variables, control flow statements, functions and modules, and list and dictionary comprehensions.
Data Types and Variables
In Python, everything is an object, and every object has a type. Some of the common data types in Python include integers, floating-point numbers, strings, booleans, and lists. To create a variable in Python, you simply need to assign a value to it using the equal sign (=). Here’s an example:
x = 10
y = 3.14
name = "John"
is_active = TrueIn the example above, we have created four variables: x, y, name, and is_active. x is an integer, y is a floating-point number, name is a string, and is_active is a boolean.
Control Flow Statements
Control flow statements are used to control the flow of execution of a program. The three main types of control flow statements in Python are if-else statements, for loops, and while loops.
If-else Statements
If-else statements are used to execute a block of code if a certain condition is met. Here’s an example:
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")In the example above, we have used an if-else statement to check whether x is greater than 5. If the condition is true, the first block of code will be executed, and if it’s false, the second block of code will be executed.
For Loops
For loops are used to iterate over a sequence of values, such as a list or a string. Here’s an example:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)In the example above, we have used a for loop to iterate over the fruits list and print each item.
While Loops
While loops are used to execute a block of code as long as a certain condition is true. Here’s an example:
i = 1
while i <= 5:
print(i)
i += 1In the example above, we have used a while loop to print the numbers from 1 to 5.
Functions and Modules
Functions and modules are used to organize code and make it reusable. A function is a block of code that performs a specific task, and a module is a file that contains a collection of functions and variables.
Functions
To define a function in Python, you use the “def” keyword, followed by the function name and the function parameters. Here’s an example:
def add_numbers(x, y):
return x + yIn the example above, we have defined a function called add_numbers that takes two parameters (x and y) and returns their sum.
Modules
To use a function from a module, you first need to import the module using the “import” keyword. Here’s an example:
import mathx = math.sqrt(25)
print(x)In the example above, we have imported the math module and used the sqrt function to calculate the square root of 25.
List and Dictionary Comprehensions
List and dictionary comprehensions are a concise way to create lists and dictionaries in Python.
List Comprehensions
To create a list using list comprehensions, you use a for loop and an expression to generate the list. Here’s an example:
numbers = [1, 2, 3, 4, 5]
squares = [x ** 2 for x in numbers]
print(squares)In the example above, we have used a list comprehension to generate a list of squares of the numbers in the “numbers” list.
Dictionary Comprehensions
To create a dictionary using dictionary comprehensions, you use a for loop and a key-value expression to generate the dictionary. Here’s an example:
fruits = ["apple", "banana", "orange"]
fruit_lengths = {fruit: len(fruit) for fruit in fruits}
print(fruit_lengths)In the example above, we have used a dictionary comprehension to generate a dictionary that contains the lengths of the fruits in the “fruits” list.
Conclusion
In this tutorial, we have covered some of the essential Python basics, including data types and variables, control flow statements, functions and modules, and list and dictionary comprehensions. These concepts are fundamental to Python programming and will form the building blocks for more advanced topics in Python. In Part III we will discuss NumPy for Data Science.






