A Beginner’s Guide to Python 3 for Data Science — Part 1

Khan Anik Rahman
4 min readDec 12, 2022

--

Python has made the world of analytics and development accessible to pretty much everyone who is willing to learn. The aim of this series is to empower the reader with the necessary Python knowledge to deep dive into data science.

In this part, we will talk about

  • The print() Function
  • Basic Data Types
  • The type() Function
  • Type Conversion
  • Variables

Let’s start by writing a single line of Python code.

print("Hello World!")

Output

Here, print() is a Python function. With this function, we are asking our computer to display what is written within the parenthesis. We can do all sorts of different actions with functions. These can range from simply showing the text “Hello World!” on our screen to solving extremely complex mathematical problems.

In the code block above, notice how the sentence “Hello World” is enclosed by double quotations. This tells that the sentence Hello World! is a string, a Python text data type.

Let’s Talk About the 4 Basic Data Types

  • Integer (int): Integers are whole numbers. For example, 1, 20, 300.
  • Floating Point (float): Floating point numbers have both integer and fractional parts. These parts are separated by a decimal point. For example, 1.2, 20.30, 300.400.
  • String (str): This data type represents texts. You can use single or double quotes to build these.
  • Boolean (bool): This data type represents logical values. These can only be True or False. Note, the capitalization is important!

You can find out the data type with the type() function. Make sure to use print() if you want to see the results.

print(type(6))
print(type(6.6))
print(type("Hello World!"))

Output

Type Conversion

Every data type has its own function, such as

  • int()
  • float()
  • str()
  • bool()

You can use these functions to change the type of data.

For example, the number 6 is an integer by definition. But, we can convert this integer into a floating point number using the following code, and let’s also print the type to check if the conversion was successful.

type(float(6))
print(type(float(6)))

Output

The output shows that we have successfully converted the integer number 6 to a floating point number.

Variables

Now that you are familiar with data types, let’s talk a bit about variables: you can think of variables as a way to store data that you can access or update as needed.

Suppose you want to keep track of how much you’re spending on groceries. You can add the prices like this: 13 + 45 + 60 + 40. But then, if you want to add in a couple more values, you will have to copy over the above addition and add the new values. This is very inefficient and requires a lot of repetition.

We can resolve this by using variables. Let’s store the same values added in the above code in a variable, x.

x = 13 + 45 + 60 + 40

We call the = symbol the assignment operator because we are using it to assign the value on the right side to the variable on the left side. Now every time we call x, we are actually calling the sum of the grocery prices.

Try out the lines below.

x = 13 + 45 + 60 + 40
x = x + 25
x_avg = x/5

The first line is adding the four values on the right side and is assigning sum to the variable x.

The second line is adding a new value of 25 to the existing value of x.

The third line is calculating the average of all 5 values by dividing x by 5.

And, that ends our discussion for today!

Ending Notes

  • Make sure to try out the codes yourself.
  • If any topics seem a bit difficult, read the article again after trying out the codes. Things will make more sense that way.
  • You can run the codes in your browser using Replit. Sign up for free and start coding immediately; no installation is required.

Quote of the Day

“In every day, there are 1,440 minutes. That means we have 1,440 daily opportunities to make a positive impact.” — Les Brown

--

--