Sunday, April 20, 2025

Python Lists

In lists, we can have numbers, characters, words - you are not limited by data type:


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
names = ["Samantha", "John", "Michael"]

print(numbers)
print(names)

Result: 


[1, 2, 3, 4, 5, 6, 7, 8, 9]
['Samantha', 'John', 'Michael']
>>> 

Here, just as for strings, we can use type() function:


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
names = ["Samantha", "John", "Michael"]

print(type(numbers))
print(type(names))

Result: 


<class 'list'>
<class 'list'>
>>> 

Function len() will return number of elements:


numbers = [1, 2, 1, 2, 1, 2]
names = ["Samantha", "John", "Samantha", "John"]

print(len(numbers))
print(len(names))

Result: 


6
4
>>> 

Tu turn string into list, we will use list() function:


x = "something"
y = list(x)

print(y)
print(len(y))
print(type(y))

Result: 


['s', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g']
9
<class 'list'>
>>> 

In next tutorial, we will extract elements of lists using indexes.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...