
Embark on your data science journey with our Day 2: Guide to Learning Python for Data Science. Understand Python’s key features, variable assignments, and the importance of comments in coding.
Also Read: Day 1 of learning Python For data science: A Beginner’s Guide
Why are Data Types Important?
Data types are the foundation of programming. They determine what kind of operations can be performed on a value and how it is stored in memory. Using the correct data type helps in:
- Efficient memory management
- Preventing errors during operations
- Improving code readability and debugging
Data Types in Python
Definition:
Data types define the kind of value a variable can hold. Python has several built-in data types to handle different kinds of data.
Key Data Types:
Integer (int)
Integers are whole numbers without decimal points.
Example:
x = 10 # Integer
y = -5 # Integer
print(type(x)) # Output: <class 'int'>
Float (float)
Floats are numbers with decimal points.
Example:
a = 3.14 # Float
b = -2.5 # Float
print(type(a)) # Output: <class 'float'>
String (str)
Strings are sequences of characters enclosed in quotes.
Example: \
text = "Hello, Python!"
print(type(text)) # Output: <class 'str'>
Boolean (bool)
Booleans represent True
or False
values.
Example:
is_python_fun = True
print(type(is_python_fun)) # Output: <class 'bool'>
Typecasting in Python
Definition:
Typecasting (or type conversion) is converting one data type into another. This is useful when performing operations between different types.
Common Typecasting Functions:
Converting to Integer (int()
)
Example:
float_num = 5.8
int_num = int(float_num)
# Converts float to integer print(int_num)
# Output: 5
Converting to Float (float()
)
Example:
int_num = 3
float_num = float(int_num)
# Converts integer to float print(float_num)
# Output: 3.0
Converting to String (str()
)
Example:
num = 10
text_num = str(num)
# Converts number to string print(text_num)
# Output: '10'
String Indexing and Slicing
Definition:
Indexing allows access to individual characters in a string, while slicing extracts a portion of the string.
String Indexing
- Strings are indexed starting from 0.
- Example:
text = "Python"
print(text[0]) # Output: 'P'
print(text[3]) # Output: 'h'
String Slicing
- Allows retrieving a substring using a range.
- Example:
text = "Python"
print(text[0:4])
# Output: 'Pyth' print(text[2:])
# Output: 'thon'
String Methods and Operations
Common String Methods:
Changing Case (.upper()
, .lower()
)
Example:
text = "hello"
print(text.upper()) # Output: 'HELLO'
print(text.lower()) # Output: 'hello'
Removing Whitespaces (.strip()
)
Example:
text = " Python "
print(text.strip()) # Output: 'Python'
Replacing Substrings (.replace()
)
Example:
text = "I love Java"
print(text.replace("Java", "Python")) # Output: 'I love Python'
String Concatenation
Example:
first = "Data"
second = "Science"
result = first + " " + second print(result) # Output: 'Data Science'
Practice Questions
- What will be the output of the following code?
x = "123" y = int(x) print(type(y))
- Extract the substring “learn” from the given string:
sentence = "Let's learn Python!"
- Convert the float
7.5
into an integer and print the result. - Write a Python program to convert “Hello World” to uppercase.
- What is the output of the following code?
x = "Data" y = "Science" print(x + y)
- Find the length of the string “Python Programming” using a built-in function.
- Write a Python code to extract “Science” from the string “Data Science”.
- What will be the result of
"Python"[::-1]
? - How can you check if a string starts with a specific letter? (Use an example)
- Replace all occurrences of “a” with “o” in the string “Data Analysis”.
- Convert the string “PYTHON” to lowercase.
- What will be the output of
"Hello" * 3
? - Extract the last 3 characters from the string “Data Science”.
- Given a string
text = " Machine Learning "
, write a Python command to remove leading and trailing spaces. - Convert the integer
2024
into a string. - What function is used to determine the data type of a variable? Provide an example.
- Write a Python program to check whether a given string is empty or not.
- What is the output of
print("5" + "5")
? - Given a variable
a = "Python"
, write a command to extract the first three characters. - What is the difference between
.lower()
and.casefold()
in Python strings?
Today, we explored Python’s fundamental data types, typecasting, string indexing, slicing, and string operations. These concepts form the foundation for working with data in Python. Stay tuned for Day 3, where we’ll dive into conditional statements and loops!
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
Also Read:
- Python Practice Questions & Solutions Day 5 of Learning Python for Data Science
- Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing
- Python Practice Questions & Solutions Day 4 of Learning Python for Data Science
- Day 4 of Learning Python for Data Science
- Practice Questions and Answers for Day 3 of Learning 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.