Blog

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

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

  1. What will be the output of the following code? x = "123" y = int(x) print(type(y))
  2. Extract the substring “learn” from the given string: sentence = "Let's learn Python!"
  3. Convert the float 7.5 into an integer and print the result.
  4. Write a Python program to convert “Hello World” to uppercase.
  5. What is the output of the following code? x = "Data" y = "Science" print(x + y)
  6. Find the length of the string “Python Programming” using a built-in function.
  7. Write a Python code to extract “Science” from the string “Data Science”.
  8. What will be the result of "Python"[::-1]?
  9. How can you check if a string starts with a specific letter? (Use an example)
  10. Replace all occurrences of “a” with “o” in the string “Data Analysis”.
  11. Convert the string “PYTHON” to lowercase.
  12. What will be the output of "Hello" * 3?
  13. Extract the last 3 characters from the string “Data Science”.
  14. Given a string text = " Machine Learning ", write a Python command to remove leading and trailing spaces.
  15. Convert the integer 2024 into a string.
  16. What function is used to determine the data type of a variable? Provide an example.
  17. Write a Python program to check whether a given string is empty or not.
  18. What is the output of print("5" + "5")?
  19. Given a variable a = "Python", write a command to extract the first three characters.
  20. 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:

Spread the love

Recent Posts

Python Practice Questions & Solutions Day 5 of Learning Python for Data Science

Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…

1 day ago

Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing

Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…

1 day ago

Python Practice Questions & Solutions Day 4 of Learning Python for Data Science

Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…

1 day ago

Day 4 of Learning Python for Data Science

Day 4 of Learning Python for Data Science Day 4 of Learning Python for Data…

1 day ago

Practice Questions and Answers for Day 3 of Learning Python for Data Science

Test your Python skills with these 20 practice questions and solutions from Day 3 of…

2 days ago

Day 3 of Learning Python for Data Science

Understanding Python’s conditional statements is essential for controlling the flow of a program. Today, we…

2 days ago