Python Basics: An Introduction for Beginners

17 July 2024

EN Python Basics_ An Introduction for Beginners

Python is one of the most popular programming languages, especially in the field of data analysis. It is versatile, easy to learn, and has a rich community, making it an ideal choice for beginners. In this article, I will introduce the basics of Python to help you start your journey with programming and data analysis. I will focus on the most important aspects of the language, such as variables, data types, data structures, functions, and the basics of the pandas library.

Introduction

Before we start, it is worth understanding why Python is so popular among data analysts. Python offers simple syntax that is easy to learn and read. It also has a rich ecosystem of libraries, such as pandas, numpy, and matplotlib, which make working with data, analyzing it, and visualizing it much easier.

Installing Python

Before we start writing code in Python, we need to install it.The best way of a data analyst is to use the anaconda distribution: https://www.anaconda.com/ 

Variables and Data Types

Variables

In Python, variables are created automatically when assigning a value. We do not need to declare their types in advance, which makes working with Python quick and intuitive.

x = 5

y = "Hello, World!"

Data Types

Python supports various data types, such as:

  • int: integers
  • float: floating-point numbers
  • str: strings
  • bool: boolean values (True/False)

We can check the type of a variable using the type() function.

print(type(x))  # Output: <class 'int'>

print(type(y))  # Output: <class 'str'>

Data Structures

Lists

Lists are one of the most important data structures in Python. They allow you to store multiple values in a single variable.

numbers = [1, 2, 3, 4, 5]

print(numbers[0])  # Output: 1

Dictionaries

Dictionaries store values in key-value pairs. They are ideal for storing related data.

person = {

    "name": "John",

    "age": 30,

    "city": "New York"

}

print(person["name"])  # Output: John

Tuples

Tuples are similar to lists, but they are immutable. Once created, their contents cannot be changed.

coordinates = (10.0, 20.0)

print(coordinates[0])  # Output: 10.0

Functions

Functions allow you to organize code into reusable modules. We define functions using the def keyword.

def greet(name):

    return f"Hello, {name}!"

print(greet("Alice"))  # Output: Hello, Alice!

Working with the pandas Library

pandas is one of the most important libraries for data analysts. It enables efficient work with tabular data.

Installing pandas

We can install the pandas library using pip.

pip install pandas

Importing the Library

import pandas as pd

Creating a DataFrame

A DataFrame is the basic data structure in pandas, resembling a table.

data = {

    "name": ["Alice", "Bob", "Charlie"],

    "age": [25, 30, 35]

}

df = pd.DataFrame(data)

print(df)

Loading Data from a File

pandas makes it easy to load data from various formats, such as CSV, Excel, or SQL.

df = pd.read_csv("data.csv")

print(df.head())

Basic Data Operations

pandas offers many functions for data manipulation. We can filter, group, and aggregate data.

# Filtering data

filtered_df = df[df['age'] > 30]

# Grouping data

grouped_df = df.groupby('city').mean()

List of Basic Operations in Python for Data Analysts

  1. Variables and data types: int, float, str, bool
  2. Data structures: lists, dictionaries, tuples
  3. Functions: defining and calling functions
  4. pandas: creating DataFrames, loading data, filtering, grouping, aggregating

Conclusion

The basics of Python are essential for anyone who wants to start working in data analysis. Thanks to the simplicity of Python’s syntax and its versatility, we can quickly and efficiently process data, create reports, and visualize results. I hope this article helped you understand the basics of Python and encouraged you to further learn and explore its capabilities. I encourage you to experiment with the code and discover how Python can make your work with data easier.

Other interesting articles:

Prefer to read in Polish? No problem!

That’s all on this topic. Analyze in peace!

Did you like this article 🙂?
Share it on Social Media 📱
>>> You can share it on LinkedIn and show that you learn something new every day.
>>> You can throw it on Facebook – and perhaps help a friend of yours who is looking for this.
>>> And remember to bookmark this page, you never know if it won’t come handy in in the future.

You prefer to watch 📺 – no problem
>>> Subscribe and watch my English channel on YouTube.

Ja Ci ją z przyjemnością wyślę. Za darmo. Bez spamu.

Poradnik Początkującego Analityka

Video - jak szukać pracy w IT

Regularne dawki darmowej wiedzy, bez spamu.