Back to website
Mayank Aggarwal Learning Center

Phase 02 · Python FoundationsLesson 2 of 8

Data Types, Strings & Indexing

Numbers, booleans and strings, plus indexing and slicing.

  • 10:10 video
  • 2 min read
  • Beginner
01

The problem

Every value in a program has a type: a number, some text, a yes/no answer. The type decides what you can do with the value. You can add two numbers, but you can't sensibly add a number to a sentence. Understanding types (and especially strings) removes a huge class of beginner errors.

02

The concept

Python's core types#

TypeExampleUsed for
int42, -7Whole numbers: counts, indexes, ages
float3.14, 0.5Decimal numbers: prices, scores, probabilities
boolTrue, FalseYes/no answers and conditions
str"hello"Text of any kind
NoneNone"No value yet" or "nothing returned"

Use type() to check any value:

Python
type(42)        # <class 'int'>
type(3.5)       # <class 'float'>
type("AI")      # <class 'str'>
type(True)      # <class 'bool'>

Converting between types#

Values coming from users, files or APIs often arrive as text, even when they look like numbers. Convert them explicitly:

Python
age_text = "25"
age = int(age_text)      # 25 (now a number)
price = float("99.50")   # 99.5
label = str(10)          # '10'

Strings are sequences#

A string is an ordered sequence of characters, so you can reach into it by position (index). Indexes start at 0, and negative indexes count from the end.

Python
s = "Python"
#    P  y  t  h  o  n
#    0  1  2  3  4  5
#   -6 -5 -4 -3 -2 -1

s[0]    # 'P'
s[-1]   # 'n'

Slicing: taking a piece#

A slice uses start:stop:step and returns a new string. The stop index is not included.

Python
s = "Machine Learning"
s[0:7]    # 'Machine'   (indexes 0–6)
s[8:]     # 'Learning'  (from 8 to the end)
s[:3]     # 'Mac'
s[::2]    # 'McieLann' (every 2nd character)
s[::-1]   # 'gninraeL enihcaM' (reversed)

Strings are immutable#

You can't change a string in place. s[0] = "J" raises an error. Instead, string operations return new strings:

Python
name = "ada"
name.upper()          # 'ADA'
name.capitalize()     # 'Ada'
name.replace("a", "o")  # 'odo'
print(name)           # still 'ada'

Formatting with f-strings#

The cleanest way to build text from values is an f-string:

Python
model, score = "GPT", 0.912
print(f"{model} scored {score:.1%}")  # GPT scored 91.2%
03

Try it

Your task · about 10 minutes

With s = "Machine Learning", print s[0], s[-1], s[0:7] and s[::-1]. Predict each output before you run it.

04

Recap & next

Key takeaways

  1. Core types: int, float, bool, str and None. Use type(x) to check a value's type.
  2. Strings are immutable sequences of characters: you can read them but not change them in place.
  3. Indexing starts at 0; negative indexes count from the end (s[-1] is the last character).
  4. Slicing is s[start:stop:step]; stop is excluded, and s[::-1] reverses a string.
  5. Convert between types explicitly: int("42"), float("3.5"), str(10).

Quick check

2 questions · instant feedback

1What does "Python"[0:3] return?
2Python strings are…

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next