Labs ICT
Pro Login

Python Fundamentals

Variables, loops, functions — the building blocks.

Python Fundamentals

Let's make sure your Python fundamentals are solid before we dive into data science libraries. If you already know Python well, feel free to skip ahead — but a quick refresher never hurts.

Variables and Data Types

Python is dynamically typed, which means you don't declare variable types. Just assign a value and Python figures out the rest. This makes it fast to write but you need to be careful about what type your data actually is.


name = "Alice"
age = 30
height = 5.7
is_student = True

print(type(name))
print(type(age))
    

In data science, you'll work mostly with numbers (int and float) and strings. The bool type shows up when you're filtering data with conditions.

Try it Yourself →

Lists and Dictionaries

Lists are ordered collections — think of them as a shopping list. Dictionaries are key-value pairs — like a real dictionary where you look up a word and find its definition. Both are used constantly in data science.


scores = [85, 92, 78, 95, 88]
print(sum(scores) / len(scores))

person = {"name": "Bob", "age": 25, "city": "NYC"}
print(person["name"])
    

Notice how we computed the average of scores in one line? That's the kind of concise code Python excels at. In practice, you'll use pandas DataFrames instead of raw lists, but understanding these fundamentals makes everything easier.

Try it Yourself →

Loops and List Comprehensions

Loops let you iterate over data. But Python's real superpower is list comprehensions — they let you transform data in a single, readable line.


numbers = [1, 2, 3, 4, 5]
squared = [n ** 2 for n in numbers]
print(squared)

even = [n for n in numbers if n % 2 == 0]
print(even)
    
Try it Yourself →

Functions

Functions let you package logic into reusable blocks. In data science, you'll write functions to clean data, create features, or perform calculations that you need to repeat.


def celsius_to_fahrenheit(c):
    return c * 9 / 5 + 32

temps_c = [0, 20, 37, 100]
temps_f = [celsius_to_fahrenheit(t) for t in temps_c]
print(temps_f)
    
Try it Yourself →

Key Takeaways

  • Python is dynamically typed — variables don't need type declarations
  • Lists and dictionaries are your bread and butter for data manipulation
  • List comprehensions are concise and Pythonic ways to transform data
  • Functions help you write reusable, testable code