Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Basics Part 6: Type Conversion and Lists Explained for Beginners
Python Basics – Part 6: Type Conversion and Lists
In this part of my 5-week Python training series, we’ll cover two key concepts that appear everywhere in Python:
type conversion (casting) and lists, one of the most important data structures.
Type Conversion
Changing a value from one type to another is called type conversion or casting.
int()
Converts a string or float to an integer.
input_str = "12345"
input_int = int(input_str)
print(input_int, type(input_int))
Output:
12345 <class 'int'>
Now you can perform arithmetic operations:
result = input_int + 2000
print(result)
Output:
14345
💡 Strings cannot be used directly in math operations:
# result = input_str + 2000 ❌ TypeError
print(int(input_str) + 2000) # ✅
str()
Converts numbers to strings.
number = 123
output = "The number is " + str(number)
print(output)
Output:
The number is 123
You can combine this with random numbers:
from random import randint
lotto = randint(1, 49)
print("Your lottery number is " + str(lotto))
float()
Converts integers or numeric strings into floating-point numbers.
print(float(123))
print(float("123.45"))
Output:
123.0
123.45
bool()
Converts a value into a Boolean (True or False).
print(bool("Hello")) # True
print(bool("")) # False
For numbers:
print(bool(0)) # False
print(bool(-123)) # True
print(bool(0.0)) # False
print(bool(777.7)) # True
Rule:
Only empty strings (""), zero (0, 0.0), and empty collections (like [], {}, set()) evaluate to False.
Everything else is True.
Lists
Lists are ordered, mutable sequences — perfect for storing multiple values in one variable.
Creating Lists
list1 = [1, 3, 5, 7]
print(list1)
print(type(list1))
Output:
[1, 3, 5, 7]
<class 'list'>
Indexing
List indices start at 0.
print(list1[0]) # 1
print(list1[1]) # 3
print(list1[2]) # 5
print(list1[3]) # 7
Modifying Elements
list1[0] = 11
print(list1)
Output:
[11, 3, 5, 7]
Mixed Data Types
Lists can store values of different types.
list1 = [1, 2.2, "Hello", True, [1, 2]]
print(list1)
Output:
[1, 2.2, 'Hello', True, [1, 2]]
Why Lists Are Useful
Instead of storing separate variables:
name1 = "Peter"
name2 = "Paul"
name3 = "Mary"
name4 = "Jane"
You can store them all in one list:
names = ["Peter", "Paul", "Mary", "Jane"]
print(names)
Common List Methods
.append()
Adds an element to the end of a list.
list2 = [1, 3, 5, 7, 9]
list2.append(23)
print(list2)
Output:
[1, 3, 5, 7, 9, 23]
.pop()
Removes and returns the last item of the list.
list3 = [1, 3, 5, 7, 9, 11]
list3.pop()
print(list3)
Output:
[1, 3, 5, 7, 9]
You can also capture the removed value:
deleted = list3.pop()
print(deleted, list3)
Output:
9 [1, 3, 5, 7]
Combining Lists and Dictionaries
You can combine two lists into pairs using zip():
t = [1, 2, 3]
v = ['a', 'b', 'c']
for x, y in zip(t, v):
print(x, y)
Output:
1 a
2 b
3 c
Creating Dictionaries from Lists
t = [1, 2, 3]
v = ['a', 'b', 'c']
d = dict(zip(v, t))
print(d)
Output:
{'a': 1, 'b': 2, 'c': 3}
Accessing dictionary values:
print(d['a']) # 1
Built-in list() Function
Converts any iterable into a list.
print(list(range(5)))
print(list(range(1, 30, 2)))
print(list("Hello"))
Output:
[0, 1, 2, 3, 4]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
['H', 'e', 'l', 'l', 'o']
Sorting Lists
Use .sort() to sort a list in place.
list4 = [11, 33, 54, 47, 19, 111]
list4.sort()
print(list4)
Output:
[11, 19, 33, 47, 54, 111]
Descending order:
list4.sort(reverse=True)
print(list4)
Output:
[111, 54, 47, 33, 19, 11]
Sorting strings:
list5 = ['D', 'A', 'B', 'F', 'P']
list5.sort()
print(list5)
Output:
['A', 'B', 'D', 'F', 'P']
Comparing Strings and Numbers
print('A' < 'B') # True
print(65 < 62) # False
Strings are compared using their ASCII values.
Summary
In this lesson, we learned:
- How to convert between data types (
int(),str(),float(),bool()) - Basic list operations: creation, indexing, modification
- Useful list methods:
.append(),.pop(),.sort() - Using
zip()to combine lists anddict()to create dictionaries - Converting iterables to lists with
list()
Next up: String methods, slicing, and indexing in depth!
