Day 2 of Learning Python for Data Science
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
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:
Data types define the kind of value a variable can hold. Python has several built-in data types to handle different kinds of data.
Integers are whole numbers without decimal points.
Example:
x = 10 # Integer
y = -5 # Integer
print(type(x)) # Output: <class 'int'>
Floats are numbers with decimal points.
Example:
a = 3.14 # Float
b = -2.5 # Float
print(type(a)) # Output: <class 'float'>
Strings are sequences of characters enclosed in quotes.
Example: \
text = "Hello, Python!"
print(type(text)) # Output: <class 'str'>
Booleans represent True
or False
values.
Example:
is_python_fun = True
print(type(is_python_fun)) # Output: <class 'bool'>
Typecasting (or type conversion) is converting one data type into another. This is useful when performing operations between different types.
int()
)Example:
float_num = 5.8
int_num = int(float_num)
# Converts float to integer print(int_num)
# Output: 5
float()
)Example:
int_num = 3
float_num = float(int_num)
# Converts integer to float print(float_num)
# Output: 3.0
str()
)Example:
num = 10
text_num = str(num)
# Converts number to string print(text_num)
# Output: '10'
Indexing allows access to individual characters in a string, while slicing extracts a portion of the string.
text = "Python"
print(text[0]) # Output: 'P'
print(text[3]) # Output: 'h'
text = "Python"
print(text[0:4])
# Output: 'Pyth' print(text[2:])
# Output: 'thon'
.upper()
, .lower()
)Example:
text = "hello"
print(text.upper()) # Output: 'HELLO'
print(text.lower()) # Output: 'hello'
.strip()
)Example:
text = " Python "
print(text.strip()) # Output: 'Python'
.replace()
)Example:
text = "I love Java"
print(text.replace("Java", "Python")) # Output: 'I love Python'
Example:
first = "Data"
second = "Science"
result = first + " " + second print(result) # Output: 'Data Science'
x = "123" y = int(x) print(type(y))
sentence = "Let's learn Python!"
7.5
into an integer and print the result.x = "Data" y = "Science" print(x + y)
"Python"[::-1]
?"Hello" * 3
?text = " Machine Learning "
, write a Python command to remove leading and trailing spaces.2024
into a string.print("5" + "5")
?a = "Python"
, write a command to extract the first three characters..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
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.
Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…
Day 4 of Learning Python for Data Science Day 4 of Learning Python for Data…
Test your Python skills with these 20 practice questions and solutions from Day 3 of…
Understanding Python’s conditional statements is essential for controlling the flow of a program. Today, we…