0% found this document useful (0 votes)
3 views4 pages

Python Data Types Exercises Solutions

The document provides a complete reference for Python data types, including strings, integers, floats, booleans, lists, tuples, sets, and dictionaries. Each section includes practical solutions and examples for common operations related to each data type. The content is structured to facilitate understanding and application of Python programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Python Data Types Exercises Solutions

The document provides a complete reference for Python data types, including strings, integers, floats, booleans, lists, tuples, sets, and dictionaries. Each section includes practical solutions and examples for common operations related to each data type. The content is structured to facilitate understanding and application of Python programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Data Types - Complete Reference

String (str) - Solutions

1. Create a string and print first/last character:


text = "Python"
print(text[0], text[-1])

2. Check if "Python" exists:


sentence = "I love Python programming"
print("Python" in sentence)

3. Reverse a string:
s = "Hello"
print(s[::-1])

Integer (int) - Solutions

1. Swap without temp variable:


a, b = 5, 10
a, b = b, a
print(a, b)

2. Factorial using integers:


num = 5
fact = 1
for i in range(1, num+1):
fact *= i
print(fact)

3. Even or odd:
n = 7
print("Even" if n % 2 == 0 else "Odd")

Float (float) - Solutions

1. Convert int to float:


num = 5
f = float(num)
print(num, f)

Page 1
Python Data Types - Complete Reference

2. BMI calculation:
weight = 70.0
height = 1.75
bmi = weight / (height ** 2)
print(bmi)

3. Round to 3 decimals:
value = 3.1415926
print(round(value, 3))

Boolean (bool) - Solutions

1. Check range:
x = 15
print(10 <= x <= 20)

2. Convert to bool:
print(bool(0), bool(""), bool([1, 2, 3]))

3. Use and/or/not:
a, b = True, False
print(a and b, a or b, not a)

List (list) - Solutions

1. Even numbers from list:


nums = [1, 2, 3, 4, 5, 6]
evens = [n for n in nums if n % 2 == 0]
print(evens)

2. Reverse without reverse():


nums = [1, 2, 3]
print(nums[::-1])

3. Merge without '+':


a = [1, 2]
b = [3, 4]
a.extend(b)

Page 2
Python Data Types - Complete Reference

print(a)

Tuple (tuple) - Solutions

1. Single element tuple:


t = (5,)
print(type(t))

2. Unpack tuple:
person = ("Alice", 25, "Engineer")
name, age, job = person
print(name, age, job)

3. Concatenate tuples:
t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2)

Set (set) - Solutions

1. Intersection:
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)

2. Remove duplicates from list:


lst = [1, 2, 2, 3]
unique = list(set(lst))
print(unique)

3. Subset check:
a = {1, 2}
b = {1, 2, 3}
print(a.issubset(b))

Dictionary (dict) - Solutions

1. Student grades:

Page 3
Python Data Types - Complete Reference

students = {"Ali": 85, "Sara": 90}


for name, grade in students.items():
print(name, grade)

2. Merge dictionaries:
a = {"x": 1}
b = {"y": 2}
merged = {**a, **b}
print(merged)

3. Word frequency:
sentence = "this is a test this is"
words = sentence.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
print(freq)

Page 4

You might also like