Python 2: Python Essentials — Variables, Data Types, Functions, and Control Flow

Goal:

By the end of this blog, you’ll be able to:

  • Declare variables and understand data types
  • Write basic functions
  • Use if-else conditions, loops, and more

1. Variables and Data Types

In Python, you don’t need to declare types — it’s dynamic.

name = "Prince"         # str
age = 30                # int
height = 5.9            # float
is_analyst = True       # bool
skills = ["Python", "SQL", "Excel"]  # list

To check a type:

print(type(name))  # <class 'str'>

🧠 2. Common Data Structures

List

tools = ["Tableau", "Power BI", "Excel"]
tools.append("Python") 
print(tools[0])  # Tableau

Dictionary

person = {
    "name": "Prince",
    "role": "Data Analyst",
    "skills": ["Python", "SQL", "Visualization"]
}
print(person["role"])  # Data Analyst

Set & Tuple

unique_skills = set(["Python", "Python", "R"])
constants = (3.14, 9.81)  # tuple is immutable

🧾 3. Conditionals

Indentation is critical in Python, and even one misplaced space can break the code.

score = 85

if score >= 90:
    print("Excellent")
elif score >= 75:
    print("Good")
else:
    print("Needs improvement")

🔁 4. Loops

For loop

for tool in tools:
    print(tool)

While loop

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

🧮 5. Functions

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

print(greet("Prince"))

You can also add type hints:

def square(x: int) -> int:
    return x * x

✅ 6. Practice Time

Open a notebook in blog_env, and try:

  • Creating a list of your favorite analytics tools
  • Writing a function that returns how many characters are in a string
  • Using an if statement to check if a number is even or odd
  • Writing a loop that prints numbers 1 to 10

🚀 Bonus: f-strings (String Interpolation)

name = "Prince"
role = "Data Analyst"
print(f"My name is {name} and I work as a {role}.")

📘 Summary

  • Python uses simple, readable syntax
  • You learned variables, conditions, loops, functions
  • This forms the foundation of everything else