Python Essentials 🐍 - ivinnyaraujo/dataengineer-datascience-python GitHub Wiki

Python is a versatile, open-source programming language that has become one of the most popular choices in software development. Its simplicity, readability, and extensive ecosystem of libraries and frameworks make it suitable for a wide range of applications. Python is widely used across diverse domains, including web development, task automation, data analysis, and machine learning, thanks to its adaptability and powerful functionality. This article will cover a very high-level summary of Python essentials.

Variables

Variables store data. You can assign numbers, strings, or any object to a variable.


x = 10
x = "Hello"

Data Types

Data types define the kind of value a variable holds, such as a number, string, list, or dictionary.

Type Example Description
int 42 Integer
float 3.14 Decimal number
str "hello" Text string
bool True/False Boolean (yes/no)
list [1, 2, 3] Ordered, mutable collection
dict {"a": 1} Key-value mapping
tuple (1, 2) Ordered, immutable collection
set {1, 2, 3} Unordered unique elements
NoneType None Represents “nothing” or null

type(42)  # <class 'int'>

Lists

Lists are ordered, changeable collections. Great for storing sequences.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list[0])  # 1

Dictionaries

Dictionaries store data in key-value pairs, useful for structured information.


person = {'name': 'Alice', 'age': 30}
print(person['name'])  # Alice

Methods vs Attributes

  • Methods are actions you can perform on objects (functions).
  • Attributes are properties or metadata about the object.

text = "hello"
print(text.upper())  # Method: HELLO

df.shape  # Attribute: (rows, columns)

Control Flow

Conditional statements let you run code based on conditions.


x = 10
if x > 5:
    print("High")
else:
    print("Low")

Loops

Loops allow repeating code multiple times.

  • for loop (loop over items)

for i in range(3):
    print(i)

  • while loop (loop while condition is true)

n = 0
while n < 3:
    print(n)
    n += 1

Functions

Functions let you reuse blocks of code.


def greet(name):
    return f"Hi, {name}!"

print(greet("Sam"))

Modules and Imports

Modules are files with reusable code. Use import to include them.


import math
print(math.pi)

File Handling

Read and write files using Python.

  • Write to file

with open("file.txt", "w") as file:
    file.write("Hello")

  • Read from file

with open("file.txt", "r") as file:
    print(file.read())

Essential Libraries

Pandas

Data analysis and manipulation using DataFrames.


import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3]})
print(df.head())

NumPy

Efficient numerical operations and arrays.


import numpy as np
arr = np.array([1, 2, 3])
print(np.mean(arr))  # 2.0

Matplotlib

Basic plotting and visualisations.


import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [10, 20, 30])
plt.show()

Seaborn

Statistical plotting built on Matplotlib.


import seaborn as sns
tips = sns.load_dataset("tips")
sns.boxplot(x='day', y='total_bill', data=tips)

Requests

Make HTTP requests and work with APIs.


import requests
response = requests.get("https://api.github.com")
print(response.json())

Datetime

Work with dates and times.


from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))

Data Import and Export

  • Read from CSV

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

  • Write to CSV

df.to_csv('output.csv', index=False)

Summary

Python is beginner-friendly yet powerful. With just the essentials—variables, lists, control flow, functions, and libraries like Pandas or NumPy, it is possible to build scripts, analyse data, and automate tasks.