DEV Community

Owen Avedi
Owen Avedi

Posted on

Python 101 Basics for Data Analysis

Python is a very readable programming language that became the number one choice for data analysis, mainly because it is easy to read, has a large number of ready-made tools, is free, and works everywhere.

Step-by-step guide on how to start

  1. Install the Python version of your choice on your computer.

  2. Check if Python has been successfully installed by running this in your CMD: python --version

  3. Install a text editor of your choice, such as VS Code, Sublime Text, or Anaconda.

Foundations to starting Python coding like a pro

Python is an interpreted programming language, meaning you are supposed to write Python (.py) files in a text editor, then put those files into a Python interpreter to be executed.

We can write our first Python file, let's call it test.py, which can be done in any text editor.

print("Hello World")
Enter fullscreen mode Exit fullscreen mode

After running this code in the Python file, save your file. Open CMD, navigate to the directory where you saved the file, and run:

You have just written and executed your first Python Program.

1. Python Indentation

Indentation refers to the spaces in the beginning of a code line

In many programming languages, indentation is optional and used only for readability. In Python, it is part of the syntax, meaning Python uses indentation to determine the structure of your code.

If indentation is incorrect, Python will raise an error. Python uses indentation to indicate a block of code.

Python convention is to use 4 spaces for each indentation level.

Example:
Correct indentation

weight = 80
if weight >= 70:
   print("You are overweight")
Enter fullscreen mode Exit fullscreen mode

Wrong indentation

weight = 80
if weight >= 70:
print(You are overweight")
Enter fullscreen mode Exit fullscreen mode

You have to use the same number of spaces in the same block of code, or Python will return an error.

2. Python Variables

A variable is a name used to store data in memory. In Python, a variable is created when you assign a value to it:

name = "Python"
version = 3
level = "Beginner"

print(name)
print(version)
print(level)
Enter fullscreen mode Exit fullscreen mode

The equal sign = is called the assignment operator. Basically means store the value on the right side, the variable on the left.

Rules for naming Variables

  • Must always start with a letter or underscore
  • Can contain letters, numbers, and underscores
  • Cannot contain spaces, fill spaces with underscore
  • Cannot contain special symbols
  • It is case sensitive

3. Comments

Python has a commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment:

#This is a comment
print("Python Basics")
Enter fullscreen mode Exit fullscreen mode

4. Python Data Types

Data type tells Python what kind of value is stored and what operations can be performed on it. Major data types include:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Numeric data types

  • Integer (int) - Whole numbers
version = 3
level = 1

print(version, level)
print(type(version))
Enter fullscreen mode Exit fullscreen mode
  • Float (float) - Decimal numbers
weight = 80.6
price = 100.40

print(weight, price)
print(type(weight))
Enter fullscreen mode Exit fullscreen mode
  • Complex numbers - used in advanced math
z = 3 + 2j
print(z)
print(type(z))
Enter fullscreen mode Exit fullscreen mode

Strings

Strings store text and are written in quotes.

name = "Python"
level = "Beginner" 

print(name)
print(level)
print(type(name))
Enter fullscreen mode Exit fullscreen mode

Boolean type

Booleans represent True or False.

print(10 > 9)
a = 10
b = 9

print(a > b)
print(b > a)
Enter fullscreen mode Exit fullscreen mode
version = 3
print(version >= 2)
Enter fullscreen mode Exit fullscreen mode

Lists

Lists are ordered and mutable collections.

coding_languages = ["Python", "HTML", "CSS"]
print(coding_languages)
Enter fullscreen mode Exit fullscreen mode

Tuples

Tuples are ordered but immutable.

language = ("Python", 3, "Beginner")
print(language)
print(type(language))
Enter fullscreen mode Exit fullscreen mode

Range

Range represents a sequence of numbers.

print(list(range(5)))
print(list(range(1,10)))
print(list(range(3,20,2)))
Enter fullscreen mode Exit fullscreen mode

Dictionaries

This stores key-value pairs.

language = {
            "name": "Python",
            "version":  3,
            "level": "Beginner"
           }

print(language)
print(type(language))
Enter fullscreen mode Exit fullscreen mode

Sets

Sets store unique values and remove duplicates automatically.

numbers = {1, 2, 2, 3, 4}
print(numbers)
print(type(numbers))
Enter fullscreen mode Exit fullscreen mode

Frozen sets

Immutable version of sets.

f = frozenset([1, 2, 2, 3, 4])
print(f)
print(type(f))
Enter fullscreen mode Exit fullscreen mode

Type conversion

version = int("3")
price = float(100)
level = str(1)

print(version, type(version))
print(price, type(price))
print(level, type(level))
Enter fullscreen mode Exit fullscreen mode

Difference between mutable and immutable data types

Mutable data types are the ones that can be changed after creation. Immutable cannot be changed.
Mutable data types:

  • list
  • dict
  • set
  • bytearray

Immutable data types:

  • int
  • float
  • bool
  • str
  • tuple
  • range
  • frozenset

Those are the basics to start being comfortable with Python. Happy Coding.

Top comments (0)