Python Basics for Beginners: Data Structures, Loops, Functions, NumPy, Pandas, Matplotlib & Seaborn
Python Basics: Complete Guide for Beginners
Python is one of the most popular and beginner-friendly programming languages. Its simple syntax, vast libraries, and versatility make it ideal for web development, data analysis, artificial intelligence, automation, and more.
🚀 Python Basics
Python is an interpreted, high-level, and general-purpose programming language. It emphasizes readability and reduces the cost of program maintenance.
👉 Key Features of Python:
- Easy to learn and read
- Open-source with vast community support
- Extensive libraries and frameworks
- Supports multiple programming paradigms (Procedural, Object-Oriented, Functional)
📦 Python Data Structures
Data structures are fundamental concepts in programming used to store and organize data efficiently.
1. Lists
- Ordered, mutable collections.
- Can store mixed data types.
fruits = ['apple', 'banana', 'cherry']
2. Tuples
- Ordered, immutable collections.
coordinates = (10, 20)
3. Sets
- Unordered, mutable, no duplicate elements.
unique_numbers = {1, 2, 3}
4. Dictionaries
- Key-value pairs, unordered, mutable.
student = {'name': 'John', 'age': 21}
⚙️ Python Programming Fundamentals
✅ Variables & Data Types
- Numbers: int, float, complex
- Strings: Text data
- Booleans: True or False
✅ Operators
- Arithmetic: +, -, *, /, %, **
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
🔀 Conditions and Branching
Used to make decisions in code.
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
👉 Conditional Statements:
- if
- if...else
- if...elif...else
🔁 Loops in Python
✅ For Loop
for i in range(5):
print(i)
✅ While Loop
count = 0
while count < 5:
print(count)
count += 1
✅ Loop Control Statements:
- break – exits the loop
- continue – skips the current iteration
- pass – does nothing (placeholder)
🧠 Functions in Python
✅ Defining a Function:
def greet():
print("Hello, Welcome!")
✅ Function with Arguments:
def greet(name):
print("Hello", name)
✅ Return Statement:
def add(a, b):
return a + b
✅ Types of Functions:
- Built-in Functions (e.g., len(), print(), type())
- User-defined Functions
📦 Python Packages
✅ Importing a Package:
import math
print(math.sqrt(16))
✅ Popular Python Libraries:
- NumPy – Numerical computations
- Pandas – Data manipulation
- Matplotlib – Data visualization
- Seaborn – Advanced visualization
🔢 Working with NumPy
✅ Installing NumPy:
pip install numpy
✅ Basic Operations:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
✅ Array Operations:
- Mathematical: sum, mean, max, min
- Array slicing and reshaping
📊 Working with Pandas
✅ Installing Pandas:
pip install pandas
✅ Creating DataFrames:
import pandas as pd
data = {'Name': ['John', 'Alice'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
✅ Data Operations:
- Reading:
read_csv()
,read_excel()
- Viewing:
head()
,info()
,describe()
- Manipulating:
drop()
,fillna()
,groupby()
📈 Introduction to Data Visualization
Data visualization transforms data into graphical representations, making insights easier to understand.
✅ Popular Python Visualization Libraries:
- Matplotlib – Basic plotting
- Seaborn – Statistical graphics
📊 Introduction to Matplotlib and Seaborn
1. Matplotlib
Matplotlib is a basic plotting library.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
plt.plot(x, y)
plt.title('Line Chart')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
2. Seaborn
Seaborn is built on top of Matplotlib and offers advanced visualization.
import seaborn as sns
import pandas as pd
data = {'Age': [22, 25, 30, 35], 'Salary': [30000, 50000, 60000, 80000]}
df = pd.DataFrame(data)
sns.barplot(x='Age', y='Salary', data=df)
📌 Conclusion
Python is a versatile language offering tools for data analysis, machine learning, web development, and more. Mastering Python basics, data structures, control flow, functions, and libraries like NumPy, Pandas, Matplotlib, and Seaborn sets a strong foundation for anyone stepping into the programming world.
Comments
Post a Comment