Day 2: Mastering Python Data Types, Type Conversion, and F-Strings


Hello, Python Enthusiasts!
Welcome to Day 2 of my 100 Days of Python journey! Today, I delved deeper into Python’s fundamentals, focusing on data types, operations, type conversion, and the simplicity of F-Strings for string formatting. Let’s explore what I learned!
1. Understanding Python Data Types
Python offers a wide range of data types that define how data is stored and manipulated. The primary ones include:
int
for integers (e.g.,19
)float
for decimal numbers (e.g.,19.5
)str
for strings (e.g.,"Ayushi"
)bool
for boolean values (True
orFalse
)
By using the type()
function, I can check the type of any variable:
# Checking variable data types
age = 19
name = "Ayushi"
print(type(age)) # Output: <class 'int'>
print(type(name)) # Output: <class 'str'>
Understanding these basics sets the stage for efficient data handling and manipulation.
2. Arithmetic and Logical Operations
Python allows us to perform a variety of operations on numerical data:
# Arithmetic operations
a = 10
b = 5
sum_result = a + b # Addition
difference = a - b # Subtraction
product = a * b # Multiplication
division = a / b # Division
print(f"Sum: {sum_result}, Difference: {difference}, Product: {product}, Division: {division}")
I also explored logical operators like and
, or
, and not
to combine conditions:
# Logical operations
x = True
y = False
print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False
These tools are essential for building dynamic and interactive programs.
3. Type Conversion Made Easy
Python makes it straightforward to convert data between types using functions like int()
, float()
, and str()
.
Type conversion allows us to change one data type to another. This is useful when performing operations with different data types or displaying values in a specific format.
Here’s an example where I converted my age to a string for string concatenation:
# Type conversion example
age = 19
next_age = age + 1
# Converting integer to string for concatenation
print("I am " + str(age) + " years old, and next year I will be " + str(next_age) + ".")
Without str()
, this would have resulted in an error. It’s crucial to ensure compatibility when combining different data types.
4. F-Strings: A Modern Way to Format Strings
One of the most powerful features I discovered today was Python's f-strings, which easily format strings with variables. Instead of concatenating strings or using string formatting methods, this allow you to embed expressions directly within string literals using curly braces {}
.
# Using F-strings for string formatting
name = "Ayushi"
age = 19
print(f"My name is {name}, and I am {age} years old. Next year, I'll be {age + 1}.")
Join Me!
If you’re also on a journey to learn Python or have insights to share, I’d love to hear from you in the comments! Whether you're a beginner like me or an experienced programmer, let’s explore this incredible language together.
Until then, happy coding! 😊
Subscribe to my newsletter
Read articles from Ayushi singla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
