Beginner Roadmap to Python: Part 2

Krupa SawantKrupa Sawant
4 min read

We’ve now covered Python’s basic building blocks — numbers, operators, and type conversions — giving you the foundation to work with any kind of value. Next, we’ll move into Python’s more versatile data structures like lists, tuples, strings, and dictionaries, which allow you to store, organize, and process data in powerful ways.

Lists in Python

Lists are collections that can hold multiple items in order. You can think of them like a shopping list or a lineup of things.

fruits = ["apple", "banana", "cherry"]
print(fruits)  # ['apple', 'banana', 'cherry']

Accessing List Items

You can get items by their position (index). Python indexes start at 0.

print(fruits[0])  # apple
print(fruits[2])  # cherry

Adding Items

Use .append() to add an item at the end:

fruits.append("orange")
print(fruits)  # ['apple', 'banana', 'cherry', 'orange']

Removing Items

Use .remove() to remove by value, and .pop() to remove by index:

fruits.remove("banana")
print(fruits)  # ['apple', 'cherry', 'orange']

popped = fruits.pop(1)
print(popped)  # cherry
print(fruits)  # ['apple', 'orange']

List Length

Find out how many items a list has with len():

print(len(fruits))  # 2

List Slicing

Get parts of a list with list_name[start:end] (end is exclusive).

fruits = ["apple", "banana", "cherry", "orange", "kiwi"]

print(fruits[1:4])  # ['banana', 'cherry', 'orange']
print(fruits[:3])   # ['apple', 'banana', 'cherry']
print(fruits[2:])   # ['cherry', 'orange', 'kiwi']
print(fruits[-3:-1]) # ['cherry', 'orange']

Looping Over Lists

# Simple loop
for fruit in fruits:
    print(fruit)

# Loop by index
for i in range(len(fruits)):
    print(f"Index {i}: {fruits[i]}")

# Loop with both index and value
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

List Comprehensions

List comprehensions give you a shorter and cleaner way to create lists in Python.
Instead of writing a loop and appending to a list, you can do it in one line.

Syntax:

[expression for item in iterable if condition]
  • expression → what you want in the list

  • item → variable representing each value

  • iterable → something you loop over (list, range, string, etc.)

  • if condition (optional) → filter results

Example 1 – Without list comprehension:

numbers = []
for i in range(5):
    numbers.append(i * 2)
print(numbers)  # [0, 2, 4, 6, 8]

Example 2 – With list comprehension:

numbers = [i * 2 for i in range(5)]
print(numbers)  # [0, 2, 4, 6, 8]

Example 3 – With condition:

even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares)  # [0, 4, 16, 36, 64]

💡 Why use it?

  • Shorter code

  • More readable for simple cases


Tuples

Tuples are like lists, but immutable — you can’t change them after creation.

Differences Between Lists and Tuples:

FeatureListTuple
MutabilityMutable (can change)Immutable (cannot change)
Syntax[]()
PerformanceSlightly slowerSlightly faster
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[1])  # banana

# my_tuple[1] = "blueberry"  # ❌ TypeError

Strings in Python

Strings are sequences of characters enclosed in single ('), double ("), or triple quotes (''' / """).

Example:

name = "Python"
print(name)

Common String Methods:

MethodDescriptionExample
.upper()Converts to uppercase"hello".upper()"HELLO"
.lower()Converts to lowercase"HELLO".lower()"hello"
.strip()Removes leading/trailing spaces" hello ".strip()"hello"
.replace(old, new)Replaces a substring"apple".replace("a", "o")"opple"
.split(separator)Splits string into a list"a,b,c".split(",")['a','b','c']
.join(list)Joins elements with a separator" ".join(["I","love","Python"])"I love Python"

Dictionaries in Python

Dictionaries store key-value pairs — like a real dictionary where you look up a word (key) to get its meaning (value).

Example:

person = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

# Access value
print(person["name"])  

# Add new key-value pair
person["email"] = "alice@example.com"

# Remove a key
person.pop("age")

print(person)

Dictionary Notes:

  • Keys must be unique and immutable (strings, numbers, tuples).

  • Values can be any type (string, number, list, another dictionary, etc.).

  • Order is preserved in Python 3.7+.

Wrapping Up

We explored Python’s core building blocks — variables, data types, strings, lists, tuples, and dictionaries. Master these, and you’ll have the foundation to tackle data analysis, machine learning, and AI projects.

0
Subscribe to my newsletter

Read articles from Krupa Sawant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Krupa Sawant
Krupa Sawant