Python for Data Science

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 Data Types in Python

Python supports multiple data types, each serving a unique purpose. Some of the most commonly used data types include:

  • Strings: 'sneha', 'hello'
  • Integers: 45, -45, 0
  • Float (Decimals): 2.6, 5.78
  • Boolean: True, False

Writing a Program to Identify Data Types

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 in Python

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.

Typecasting Challenge

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.")

Handling Errors in Typecasting

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.

Indexing and Slicing in Strings

Python allows us to access individual characters and substrings using indexing and slicing.

Indexing in Python Strings

In Python, string indices start from 0.

word = "Python"
print(word[0])  # P
print(word[-1])  # n (last character)

Slicing Strings

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

Conclusion

On Day 5 of Python learning, we explored fundamental concepts such as:

  • Different data types in Python.
  • Typecasting and handling conversion errors.
  • Indexing and slicing strings effectively.

Practice Question

  • Write a program that asks the user to input three values:
  • Write a program that asks the user to input a string containing a number (e.g., “123”). Convert this string into an integer, add 50 to it, and print the result.
  • What happens if the string is “abc” instead?
  • Write a program that takes a user-input string and prints:
  • Write a program that takes a user-input string and a character. Count and print how many times the character appears in the string. Example: Input: “hello world”, ‘o’ → Output: 2.
  • Write a program to check if a user-input string is a palindrome (a string that reads the same backward as forward). Example: Input: “madam” → Output: “Palindrome”.
  • Write a program using if-elif-else statements to find the largest of three numbers entered by the user.
  • Write a program that takes a single character as input and checks whether it is a vowel, consonant, or neither (e.g., a digit or symbol). Use if-elif-else statements for this.
  • Write a program to calculate the sum of all even numbers from 1 to a user-defined number (inclusive) using a while loop.
  • Write a program that takes two numbers as input and prints all the prime numbers between them using nested loops. Example: Input: 10, 20 → Output: 11, 13, 17, 19.
  • Write a program that takes an integer as input and calculates the sum of its digits using a loop. Example: Input: 123 → Output: 6 (1 + 2 + 3).
  • Write a program that takes an integer and reverses its digits. Example: Input: 1234 → Output: 4321
  • Check for Armstrong Number, A number is an Armstrong number if the sum of its digits raised to the power of the number of digits equals the number itself. Example: Input: 153 → Output: Armstrong number (1³ + 5³ + 3³ = 153)
  • Write a program that calculates the factorial of a given number using a loop. Example: Input: 5 → Output: 120 (5 × 4 × 3 × 2 × 1)
  • Write a program to print the first N numbers of the Fibonacci sequence using a loop. Example: Input: 7 → Output: 0, 1, 1, 2, 3, 5, 8
  • Write a program that takes an integer as input and counts the number of digits in it. Example: Input: 34567 → Output: 5
  • Write a program that finds the GCD (Greatest Common Divisor) of two numbers using a loop. Example: Input: 48, 18 → Output: 6
  • Write a program that calculates the Least Common Multiple (LCM) of two numbers using loops. Example: Input: 4, 5 → Output: 20
  • Write a program that checks whether a given number is a palindrome (same forward and backward). Example: Input: 121 → Output: Palindrome Input: 123 → Output: Not a palindrome

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

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…

2 days 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…

2 days ago

Day 4 of Learning Python for Data Science

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

2 days 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…

3 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…

3 days ago

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

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

3 days ago