Python for Data Science: Unleashing the Power - Part 1 of Complete Series

Python for Data Science: Unleashing the Power - Part 1 of Complete Series

Complete Python for Data Science by Abhishek Gupta

In this blog post, you will learn about the basics of Python. This article is part of our Learn Complete Python for Data Science series, where we cover everything in Python for Data Science. This article serves as the first part of the series.

If you are confident with Python or are an experienced Python developer, then hold on—I bet you will learn something new, and in the end, you will find it worth your time. You can also follow this article series for a Python refresher or from an interview point of view.

In this post, we will cover Python3 at the introductory level. The topics covered are as follows:

  1. What exactly is Python and its application?

  2. Deep dive into print()

  3. Touch upon the Data Types in Python

  4. Python Variables

    1. Discuss some interview questions

    2. Stylish variables declaration techniques

  5. Comments in Python

  6. Keywords and Identifiers

  7. Taking input using input()

  8. Type Conversions

If you have ever worked in Python, you might remember your first Python class 😉

Introduction to Python and its Application

According to Python Docs, "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development and for use as a scripting or glue language to connect existing components. Python's simple, easy-to-learn syntax emphasizes readability and reduces the cost of program maintenance."

Ufff😵, it's a long introduction. Let's break it down into simple language. The first line says it is an interpreted programming language, which means Python code is executed line by line into machine code. Therefore, Python is comparatively slow, but don't worry; you won't feel it.

If you are interested in learning more about Interpreted language and Complied language, I would recommend you to read https://builtin.com/software-engineering-perspectives/compiler-vs-interpreter

What is "Object Oriented" in the definition? Well, it is nothing but a programming style. We will talk about it in detail in this series.

What does "high-level" mean in the definition? It means you don't have to code Python in the binary language; you can use the English language.

We will cover the remaining words from the definition in the later part of this article. By the end, you will automatically understand the definition.

Application of Python

  1. Web Development: You can create websites and web applications using frameworks like Django or Flask.

  2. Data Science and Machine Learning (DSML): Python is heavily used for processing and analyzing data, making it a top choice for data scientists. Libraries like NumPy, Pandas, and TensorFlow are popular in this domain. We will explore Python DSML's perspective.

  3. Automation and Scripting: Python is excellent for writing small scripts to automate repetitive tasks, making it a handy tool for system administrators and anyone looking to automate their workflow.

  4. Artificial Intelligence: Python is used in AI projects and research. Libraries such as PyTorch and scikit-learn are employed for machine learning tasks. We will delve into these topics on this page.

  5. Game Development: While not as common as in other domains, Python is used to develop games, and there are libraries like Pygame designed for this purpose.

  6. Desktop Applications: You can build desktop applications using frameworks like Tkinter or PyQt.

  7. Network Servers: Python can be used to create network servers and handle network protocols.

Overall, learning Python is worth it. So let's learn.

Print output in Python

The basic need of a programming language is to print the output, and in Python, we have the print() function. Now, for those of you who don't know what a function is, don't worry. For now, understand that anything with parentheses x() is a function, where x can be anything—literally anything, like my name or your name. We will dedicate an entire blog or two to functions, so there's no need to worry.

print("Hello World")
# Hello World

This is how we print a statement in Python. Observe the syntax. The line with # is the output.

Now, here's an interesting thing. What if I need to print multiple statements together?

print("Hello", "World", "Abhishek")
# Hello World Abhishek

Python is that simple. You can put as many statements as you want in a single print() function. But have you noticed something in the output? All the statements are separated by a space. Why so? Can we change this functionality? 🤔

Yes, we can! How? Let's find out!

There is a setting inside this print() function, sep. The default value of the sep parameter is a space, and that's why we get those spaces. If you want, we can change it. Let's see how.

print("Hello", "World", "Abhishek", sep="/")
# Hello/World/Abhishek

Instead, / you can type anything your name too.

Now, observe the following code snippet.

print("Hello")
print("World")
print("Abhishek")

# Hello
# World
# Abhishek

I am sure this time you have observed something in the output, and you may have already started thinking about it. Yes, why is the output on a new line every time? This is the default behaviour, and we can change it too.

There is another setting inside the print() function, end. The default value of the end parameter is, which is an escape sequence character that means a new line.

Instead, you can type anything—your name too.

print("Hello", end="-")
print("World", end="-")
print("Abhishek")

# Hello-World-Abhishek

This was our discussion around the print statement.

Data Types in Python

Data type indicates the kind of data you are going to store in a variable. For now, understand that a variable is like a container. We will cover each of the data types in detail, but for this blog, just take a bird's eye view.

Python has primarily 10 Data Types.

  1. Integer: It means a number.

     print(16)
     #16
    
  2. Float: Decimal in programming is known as Float.

     print(16.07)
     # 16.07
    
  3. Boolean: It stands for True or False

    💡
    True is interpreted as 1 and False is interpreted as 0
     print(True)
     # True
    
     print(False)
     # False
    
  4. String: It means text.

     print("Abhi")
     # Abhi
    
  5. Complex Number: That complex number in maths.

     print(2+3j)
     # (2+3j)
    

    You can access the real part using real and imaginary part using imag.

     x = 2+3j
    
     print(x.real)
     # 2.0
    
     print(x.imag)
     # 3.0
    

    In the above code, I stored complex numbers in a variable named x and then printed out the real and imaginary parts using x.real and x.imag

  6. List: It's similar to an array in C++ but with a completely different architecture.

     print([1, 2, "Hello", True])
     # [1, 2, 'Hello', True]
    

    You can store anything inside a list. It is enclosed inside square brackets[]

  7. Tuple: You can say it's a cousin of List.

     print((1, 2, "Hello", True))
     # (1, 2, 'Hello', True)
    

    It is enclosed under round braces()

  8. Sets: That set in Maths, the same concept in programming.

     print({1, 2, 3, 4, 5})
     # {1, 2, 3, 4, 5}
    

    It is enclosed under curly braces{}

  9. Dictionary: It's a key-value pair, super useful.

     print({
         "name": "Abhi",
         "age": 20
     })
     # {'name': 'Abhi', 'age': 20}
    

    It is also enclosed under curly braces{}but with a key-value pair

  10. NoneType: It means nothing.

    x = None
    

    It is used to initialize a variable.

If you want to know about the data type of any variable we can use the type() function.

x = "Abhi"
print(type(x))
# class 'str'

After reading this, you might have lots of confusion but don't worry. We will cover everything in detail, and you will have a crystal-clear idea about what exactly these things are!

Variables in Python

Variables are nothing but containers for future use.

my_name = "Abhishek"
print(my_name)
# Abhishek

In the above code snippet, my_name is a variable because it holds a value, in this case, a string value "Abhishek"

Now, let's discuss some concepts related to variables from the Python3 Level 1 interview.

Static Typing Vs Dynamic Typing

Static Typing

Static typing is like informing the computer precisely what kind of data your variable is going to hold before you use it in your program.

int x = 2

This is how you declare a variable in statically typed languages like C, C++, Swift, or Java. You declare the data type, here int, which means variable x is going to hold only integer values.

Dynamic Typing

Dynamic typing is like letting your computer figure out what kind of data your variable stores. You don't have to declare the type; you can use a variable for numbers, then switch and use it for words later. The computer only checks the type when the program is running, so errors might show up later.

x = 2

This is how you declare a variable in dynamically typed languages like Python, JavaScript, and Ruby. Here x is a variable.

Static Binding Vs Dynamic Binding

Static Binding

Static binding means we can not change the data type of the variable once it is declared.

int x = 3
cout << x;
x = "Hello" // Error Line
cout << x;

In the above C++ code snippet, a variable named x is declared as an int, and in the next line, I have reassigned it as a string, and therefore, it will throw an error. This entire concept is known as Static binding.

Dynamic Binding

Dynamic binding is exactly the opposite of static binding, which means we can change the data type of a variable once it is declared.

x = 3
print(x)
x = "Abhi"
print(x)

# 3
# Abhi

In the above Python code snippet, a variable named x is declared and assigned an integer value. After this, the same variable x is reassigned with a string value, and this is a completely valid syntax.

If you have ever coded in any one of the statically typed languages like C, C++, Java, or Swift, you might find this behaviour a bit weird, and it may be the reason for many errors in your code. However, this is how Python is designed, and its core principle is code reusability and simplicity.

💡
Try to avoid the Dynamic Binding situation.

Stylish variables declaration techniques

Python programmers love stylish code and here are some stylish variable declaration techniques.

a, b, c = 1, 2, 3
print(a, b, c)

# 1 2 3

Here we assigned value to three variables a, b and c in a single line.

a = b = c = 5
print(a, b, c)

# 5, 5, 5

Here we assigned a single value to three different variables a, b and c

💡
Pro Tip: Don't use this syntax because these stylish methods may confuse someone, and it's not a good practice. This was just for your understanding so that you don't get confused whenever you see this kind of code.

We will cover a lot of stylish syntax in this article series.

Comments in Python

Comments are a part of every programming language, and we have comments in Python too. They are non-executable statements. When we write a comment, the Python interpreter ignores it.

# This is a comment line

The line # is a comment. In programming, a lot of the time, you have to read someone else's code, and you understand their code by the comments written. It's great practice to write comments and make your code readable.

💡
Remember, a coder without comments is like a great batsman who can hit runs on every ball but every time makes his non-striker batsman run out.

Keyword

Keywords are special words in a programming language. In Python, we also have 36 reserved keywords. The Python interpreter suggests that we don't use these words as variable names because these special words have their unique meanings in Python. If we use these words as variable names, the Python interpreter will get confused and throw an error.

Here is the list of all keywords in Python. No need to remember these words; you will automatically recall them when you use them in the program. But for now, have a look

Python Keywords DigitalOcean

Taking Input in Python

Taking input from users is one of the essential features of any programming language as it allows users to interact with the program. In Python, we have an input function to perform this task.

Here's how we can use it

input("Input here...")

When you store an input, Python automatically stores it as a string because it can be easily converted to any other data type.

Type Conversion

Type conversion means changing the variable's data type from one data type to another.

There are two kinds of Type Conversions in Python:

  1. Implicit Type Conversion

  2. Explicit Type Conversion

Python automatically converts the data type of a variable in certain situations. For example:

print(2 + 3.4)
# 5.4

In the above example, we are adding an integer and a float value, which is not technically possible in a programming language. However, internally Python converts the integer to a float, and then the addition happens.

But how about the below situation

print(2 + '3')

The above code will throw a, stating that, which simply means that Python cannot add an integer and a string. In such cases, we externally have to convert the data type, and this is where External Type Conversion comes into play.

Refer to the code snippet below

print(2 + int('3'))
# 5

In the above snippet, we externally converted the string to an integer using the int() function. In Python, we have a variety of functions for external type conversion from one data type to another.

Type Conversion in Python

This concludes our discussion on the very basics of Python. In the next article, we will explore Operators, Modules, Loops, and Conditional Statements. These articles are part of our "Learn Complete Python for Data Science" series.

If you enjoyed this article, show some support and follow my work on GitHub and LinkedIn.

I am your fellow Abhishek Gupta. I will see you in the next article. Till then, stay tuned, keep learning, and keep smiling! 😊