Python Data Types: Strings, Numbers, Collections, and Useful Built-in Tools
MY LEARNING JOURNEY · 06
Learning Python with Chai aur Code, one video at a time.
Learning source: Chai aur Code — Python Video 6
1. A Quick Overview of What We'll Learn
Before getting deeper into Python, we got a short overview of what the playlist will cover.
Some of the topics include:
- Data Types: Different kinds of values we can work with in Python.
- Functions: Reusable blocks of code that perform specific tasks.
- Classes: A way to define our own objects and organize related data and behaviour.
- Modules: Python files containing reusable code that we can import into other files.
We'll also explore some more advanced topics later, such as:
- Decorators
- Generators
- Iterators
- Metaprogramming
It's interesting to see how the playlist moves from basic concepts towards more advanced Python features.
2. A Useful VS Code Shortcut: Writing Comments
While coding, sometimes we want to write notes or temporarily prevent a line of code from executing.
That's where comments come in.
In Python, we use the # symbol to write a single-line comment.
For example:
# This is a comment
print("Hello, Python!")
The Python interpreter ignores comments when executing the program.
VS Code shortcut
One useful shortcut is:
Ctrl + /
It toggles comments for the selected line or lines.
We can also select multiple lines and use the same shortcut to comment or uncomment them together.
This is particularly helpful when experimenting with code.
3. What Are Markdown Files (.md)?
While exploring files in VS Code, we also came across .md files.
Markdown files are similar to plain text files in the sense that we can edit them using a text editor. However, they support formatting through simple text symbols.
For example, we can write:
# Main Heading
## Subheading
**This text is bold.**
*This text is italic.*
- First point
- Second point
- Third point
When rendered, these symbols turn into formatted content.
What can Markdown files contain?
Markdown supports:
- Headings
- Bold and italic text
- Bullet points and numbered lists
- Links
- Images
- Tables
- Code blocks
Where are Markdown files used?
They are commonly used for:
- Project documentation
- Notes
- GitHub
README.mdfiles - Blog articles
- Tutorials and technical guides
We can write Markdown in VS Code and preview the formatted result using the Open Preview option, usually available through the editor's preview icon or the Command Palette.
We can also view the same file as plain text.
I found this interesting because Markdown lets us create properly formatted documentation without needing a complicated editor.
4. Data Types and Objects in Python
From our earlier discussion about objects, we already know that Python treats values as objects.
So, when talking about the type of an object, we can also refer to its data type or object type.
For example:
age = 20
name = "Python"
numbers = [1, 2, 3]
We can check their types using:
type(age)
type(name)
type(numbers)
Output:
<class 'int'>
<class 'str'>
<class 'list'>
Here:
intis the type of the integer object.stris the type of the string object.listis the type of the list object.
This connects to the idea that everything we work with in Python is represented through objects.
5. Understanding Parentheses, Square Brackets, and Curly Braces
One thing we need to be careful about in Python is that these three symbols have different uses:
| Symbol | Common uses |
|---|---|
() |
Function calls, grouping expressions, tuples |
[] |
Lists, indexing, slicing |
{} |
Dictionaries and sets |
For example:
# Parentheses
result = (2 + 3)
# Square brackets
numbers = [1, 2, 3]
# Curly braces
person = {"name": "Alex", "age": 20}
A small distinction worth remembering: an empty pair of curly braces {} creates an empty dictionary, not an empty set.
To create an empty set, we use:
my_set = set()
6. Mathematical Operations in Python
Python supports mathematical operations using familiar operators.
One interesting thing we learned is that exponentiation is represented using **.
For example:
2 ** 4
Output:
16
This means:
[ 2^4 = 2 \times 2 \times 2 \times 2 = 16 ]
It's important not to confuse ** with ^.
In Python, ^ is the bitwise XOR operator, not the exponentiation operator.
7. Exploring Python's Math and Random Modules
Python provides several built-in modules that make common tasks easier.
Let's look at two examples.
A. Math Module
The math module provides mathematical constants and functions.
First, we import it:
import math
We can access the value of pi using:
math.pi
Notice that pi doesn't have parentheses because it's a constant provided by the module, not a function.
We can also use functions such as:
math.sqrt(25)
math.factorial(5)
Output:
5.0
120
B. Random Module
The random module helps us generate random values and make random selections.
First:
import random
We can generate a random floating-point number between 0 and 1 using:
random.random()
We can also randomly select an element from a sequence:
random.choice([1, 2, 3, 4, 5, 6])
This is useful when we want Python to make a random selection from a collection of values.
For example, it can simulate a simple dice roll.
8. Exploring Strings and Their Methods
Now let's explore strings.
A string is a sequence of characters, and Python provides several ways to work with it.
A. Finding the Length of a String
We can use the built-in len() function:
name = "Python"
len(name)
Output:
6
This tells us the number of characters in the string.
B. Accessing Characters Using Indexing
We can access individual characters in a string using square brackets.
name = "Python"
name[0]
Output:
'P'
Python uses zero-based indexing, which means the first character is at index 0, not 1.
We can also access characters from the end using negative indexing.
name[-1]
Output:
'n'
Here:
name[0]gives the first character.name[1]gives the second character.name[-1]gives the last character.name[-2]gives the second-last character.
C. String Slicing
We can extract a portion of a string using slicing.
For example:
name = "Python"
name[1:3]
Output:
'yt'
Why?
Because Python uses the following slicing format:
string[start:end]
The starting index is included, but the ending index is excluded.
So, name[1:3] includes:
- Index
1→y - Index
2→t
It stops before index 3.
This is why the result is "yt".
D. Are Strings Internally Lists?
One thing I initially thought was that strings were treated as lists internally.
That's not exactly correct.
Strings and lists are both sequence types, which means they support operations such as indexing, slicing, and finding their length.
However, they are different object types.
The biggest distinction we've already discussed is mutability:
- Strings are immutable: We cannot change an individual character in an existing string.
- Lists are mutable: We can change, add, or remove their elements.
For example:
name = "Python"
# This will raise a TypeError
name[0] = "J"
But with a list:
numbers = [1, 2, 3]
numbers[0] = 10
print(numbers)
Output:
[10, 2, 3]
So, strings and lists share some sequence operations, but they are not the same internally.
9. What Does dir() Do?
Another useful built-in function is dir().
It helps us explore the attributes and methods available on an object.
For example:
name = "Python"
dir(name)
This displays the names of attributes and methods associated with the string object.
But here's something interesting: dir() isn't limited to strings.
We can use it with objects of different data types.
For example:
numbers = [1, 2, 3]
dir(numbers)
We can also try:
dir(10)
dir({})
dir(set())
This makes dir() useful when we're exploring Python and want to understand what operations are available for a particular object.
10. Accessing Values in a Dictionary
Dictionaries store information as key-value pairs.
For example:
person = {
"name": "Alex",
"age": 20,
"city": "Ahmedabad"
}
To access a particular value, we use its key inside square brackets:
person["name"]
Output:
'Alex'
Similarly:
person["age"]
Output:
20
The general syntax is:
dictionary_name["key_name"]
If the key doesn't exist, this approach raises a KeyError.
We can also use .get() when we want to avoid an error for a missing key:
person.get("country")
This returns None if the key isn't present, unless we provide a default value.
11. Working With Files
Another topic briefly mentioned was working with files.
Python allows us to open files and perform operations such as:
- Reading their contents
- Writing new content
- Appending information
For example, Python has a built-in open() function for working with files.
We'll explore file handling in more detail later in the playlist.
12. What About Sets?
Sets were also mentioned, but we'll explore them in a later lesson.
For now, the important thing to remember is that sets are a collection type in Python.
They are useful when we want to work with unique elements and set operations.
For example:
numbers = {1, 2, 3, 3, 4}
print(numbers)
Output:
{1, 2, 3, 4}
Notice that duplicate values are removed.
We'll get into more detail about sets when we reach that topic.
13. My Key Takeaways
Here's what I took away from this lesson:
- Python has different object types, including integers, strings, lists, dictionaries, and sets.
- We use
#to write comments, andCtrl + /is a useful VS Code shortcut. - Markdown files (
.md) are useful for formatted documentation and notes. - Parentheses, square brackets, and curly braces have different purposes.
- Python uses
**for exponentiation, not^. - The
mathandrandommodules provide useful mathematical and random operations. - Strings support indexing and slicing, but they are immutable.
dir()helps us explore the attributes and methods available on objects.- Dictionaries store key-value pairs, and we can access values using their keys.
- Python also supports file handling and sets, which we'll explore further.
Final Thoughts
This lesson felt like a quick tour of several Python features rather than a deep dive into one particular concept.
I especially liked exploring dir() because it gives us a way to discover what we can do with different objects instead of memorizing every method.
I also found the Markdown section useful because it connects directly to something I'm doing outside the playlist: documenting my learning journey.
There are still plenty of topics to explore, especially sets and file handling, but this overview gives me a better idea of what's coming next.
This is part of my Python learning journey, where I document what I understand from each Chai aur Code lecture, the questions I encounter, and the concepts I explore along the way.
What's one Python built-in function or feature you think beginners should explore early? I'd love to discover more useful tools as I continue learning.
About the author
- As a newbie, learning Python, bit by bit and error by error. I write about the things I learn, the queries I have and things I finally understand. If you’re learning as well, let’s do this together!
Comments
Join the discussionNo comments yet. Be the first to comment.
Python Learning Journey
I'm documenting my journey of learning Python through Chai aur Code, one video at a time. I'll share what I understand, the questions I encounter, the concepts I explore with AI, and the code I experiment with along the way. My goal is to learn by questioning, explaining concepts in my own words, and sharing the journey publicly. I'm a beginner, and I'm always open to suggestions that can help me learn better.

