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.
Pivot tables are a powerful tool for summarizing and analyzing data, and Python’s Pandas library…
Welcome to Section 3 of our Data Science Interview Questions series! In this part, we…
Welcome back to our Data Science Interview Questions series! In the first section, we explored…
Data Science Questions in Section 1 focus on the essential concepts of Data Visualization and…
In this article, we’ve compiled 30 carefully selected multiple choice questions (MCQs) with answers to…
Welcome to Day 15 of our Python for Data Science journey!On Day 15, we dived…