[Link].
Data Science - Semester 1
Unit 2: Data Types & Control Structures in Python
Detailed Notes
Python provides a variety of built-in data types and control structures, making programming simple
and powerful. Data Types in Python: 1. Numeric Types: int, float, complex 2. Sequence Types: str,
list, tuple 3. Mapping Type: dict 4. Set Types: set, frozenset 5. Boolean Type: bool 6. None Type:
None Type Conversion: - Implicit Conversion (Type Casting by Python) - Explicit Conversion (Using
functions like int(), float(), str()) Control Structures: 1. Conditional Statements (if, if-else, if-elif-else)
2. Looping Statements (for loop, while loop) 3. Loop Control: break, continue, pass Functions: - A
block of reusable code defined using the def keyword. - Supports default, keyword, and
variable-length arguments.
Example Codes
# Example 1: Data Types
a = 10 # int
b = 3.14 # float
c = "Python" # str
d = [1, 2, 3] # list
print(type(a), type(b), type(c), type(d))
# Example 2: Conditional Statements
x = 20
if x > 0:
print("Positive Number")
elif x == 0:
print("Zero")
else:
print("Negative Number")
# Example 3: Looping
for i in range(1, 6):
print("Number:", i)
n = 5
while n > 0:
print(n)
n -= 1
# Example 4: Break, Continue, Pass
for i in range(1, 6):
if i == 3:
continue
if i == 5:
break
print(i)
for i in range(3):
pass # placeholder
# Example 5: Functions
def greet(name):
return "Hello, " + name
print(greet("Data Science"))
Previous Year University Questions
1. Explain Python's built-in data types with examples. (10 Marks) 2. Write a Python program to
check whether a number is positive, negative, or zero. (5 Marks) 3. What is the difference between
list, tuple, and set? (10 Marks) 4. Write a Python program to display multiplication table of a number
using loops. (10 Marks) 5. Explain break, continue, and pass statements with examples. (5 Marks)
6. Define functions in Python. Write a program to calculate the factorial of a number using functions.
(10 Marks)