Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing
Python supports multiple data types, each serving a unique purpose. Some of the most commonly used data types include:
'sneha'
, 'hello'
45
, -45
, 0
2.6
, 5.78
True
, False
To understand data types better, let’s create a simple Python program where the user inputs different types of values. We will then check their data types using the type()
function.
name = input('Enter your name: ')
age = int(input('Enter your age: '))
balance = float(input('Enter your balance: '))
print(name, type(name))
print(age, type(age))
print(balance, type(balance))
Output
Enter your name: Sneha
Enter your age: 25
Enter your balance: 1000.50
Sneha <class 'str'>
25 <class 'int'>
1000.5 <class 'float'>
Typecasting refers to converting one data type into another. Python allows type conversion using functions such as:
int()
– Converts a value to an integer.float()
– Converts a value to a float.str()
– Converts a value to a string.Let’s create a program that takes a string input containing a number, converts it to an integer, adds 50, and prints the result.
try:
user_input = input("Enter a string containing a number: ")
number = int(user_input)
result = number + 50
print(result)
except ValueError:
print("Invalid input. Please enter a string containing a number.")
If the user enters a non-numeric string like “abc”, the program will raise a ValueError
. The try-except
block ensures the program handles such errors gracefully.
Python allows us to access individual characters and substrings using indexing and slicing.
In Python, string indices start from 0.
word = "Python"
print(word[0]) # P
print(word[-1]) # n (last character)
Slicing allows us to extract a portion of a string using the syntax [start:end:step]
.
text = "Hello, World!"
print(text[0:5]) # Hello
print(text[7:]) # World!
print(text[::-1]) # Reverse the string
On Day 5 of Python learning, we explored fundamental concepts such as:
Understanding these basics is essential for writing robust Python programs. Keep practicing with different inputs to solidify your understanding!
LINK FOR THE SOLUTION OF ABOVE QUESTION
We hope this article was helpful for you and you learned a lot about data analyst interview from it. If you have friends or family members who would find it helpful, please share it to them or on social media.
Join our social media for more.
Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Welcome to Day 13 of Learning Python for Data Science! Today, we’re focusing on three…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…
Welcome to Day 12 of Learning Python for Data Science. Today, we’ll dive into Pandas,…
NumPy Array in Python is a powerful library for numerical computing in Python. It provides…
Welcome to Day 9 of Learning Python for Data Science. Today we will explore comprehensions,…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…