Sunday 20 February 2022

Python#03

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered** and changeable. No duplicate members.

Python Lists

    • Used to store collection of values in a single variable. 
    • Created using []
    • Items are ordered, changeable, and allow duplicate values.
    • Items are indexed, the first item has index [0], the second item has index [1] etc.
    • Has defined order, order can't be changed. 
    • New items will be appended to the list.
    • list is mutable, meaning that we can change, add, and remove items in a list after it has been created
    • lists are indexed, lists can have items with the same value: can be duplicated

EX7. List Operations [Creation]

# List operation

#creating a list

ilist = [1,2,3,4,5]

flist = [1.0,2.0,3.0,4.0,5.0]

slist = ['Tom', 'Jerry', 'Mickey']

#Finding length of lists
print('Length of lists created : ',len(ilist),len(flist),len(slist))

#print data types of list items
print('Data Types of lists created : ',type(ilist),type(flist),type(slist))


Result: Integer, float and string lists are created.

Length of lists created :  5 5 3
Data Types of lists created :  <class 'list'> <class 'list'> <class 'list'>

In the following example explains the important list operations are discussed with code and output. This example is self explanatory list functions, usage and output result are given below. These code you can practice and play with index numbers in this list.
# List operation

# Appending List
print('Before Appending ..........')
slist = ['Tom', 'Jerry', 'Mickey']
print(slist)
slist.append('Dick')
slist.append('Harry')
print('After Appending ..........')
print(slist)
#Print ['Tom', 'Jerry', 'Mickey', 'Dick', 'Harry']


print('Before Inserting ...the index.......')
slist = ['Tom', 'Jerry', 'Mickey']
print(slist)
slist.insert(0,'Dick')
print(slist)
slist.insert(2,'Harry')
print('After Inserting ..........')
print(slist)
#Prints ['Dick', 'Tom', 'Harry', 'Jerry', 'Mickey']


print('Before Popping ..........')
slist = ['Tom', 'Jerry', 'Mickey']
print(slist)
slist.pop()
print('After Popping . removing one element at the end.....')
print(slist)
#Prints ['Tom', 'Jerry']


print('Before Slicing ....slice(start:end:increment)......')
slist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(slist[2:7])
# Prints ['c', 'd', 'e', 'f', 'g']


print("With Negative indexing")
slist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(slist[:-2])
#print ['a', 'b', 'c', 'd', 'e', 'f', 'g']

print("With Negative indexing")
slist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(slist[::-1])
#print ['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'] .. Reverse the list

print("With Specifying step")
slist = [0,1,2,3,4,5,6,7,8,9,10]
print(slist[2:8:2])
#print [2, 4, 6]


print("before ...modifying List Values")
alist = [0,1,2,3,4,5,6,7,8,9,10]
print(alist)
slist[2:4] = ['a','b', 'c']
print("After ..modifying List Values list[start:end] = [values]")
print(alist)
#print [0, 1, 'a', 'b', 'c', 4, 5, 6, 7, 8, 9, 10]

print("Before ...Deleting List Values")
alist = [0,1,2,3,4,5,6,7,8,9,10]
print(alist)
alist[2:4] = []
print("After ..Deleting index 2,3,4 values]")
print(alist)
#print [0, 1, 4, 5, 6, 7, 8, 9, 10]

# Copying lists
print('Copying Lists')
alist = ['a', 'b', 'c', 'd', 'e']
blist = alist[:]
print(alist, blist)
# Prints ['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'c', 'd', 'e']
print(blist is alist)
# Prints False

print('Before sorting .......')
slist = ['Tom', 'Jerry', 'Mickey']
print(slist)
#prints ['Tom', 'Jerry', 'Mickey']
slist.sort()
print('After Sorting........')
print(slist)
#prints ['Jerry', 'Mickey', 'Tom']

Result:

Before Appending ..........
['Tom', 'Jerry', 'Mickey']
After Appending ..........
['Tom', 'Jerry', 'Mickey', 'Dick', 'Harry']
Before Inserting ...the index.......
['Tom', 'Jerry', 'Mickey']
['Dick', 'Tom', 'Jerry', 'Mickey']
After Inserting ..........
['Dick', 'Tom', 'Harry', 'Jerry', 'Mickey']
Before Popping ..........
['Tom', 'Jerry', 'Mickey']
After Popping . removing one element at the end.....
['Tom', 'Jerry']
Before Slicing ....slice(start:end:increment)......
['c', 'd', 'e', 'f', 'g']
With Negative indexing
['a', 'b', 'c', 'd', 'e', 'f', 'g']
With Negative indexing
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
With Specifying step
[2, 4, 6]
before ...modifying List Values
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After ..modifying List Values list[start:end] = [values]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Before ...Deleting List Values
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After ..Deleting index 2,3,4 values]
[0, 1, 4, 5, 6, 7, 8, 9, 10]
Copying Lists
['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'c', 'd', 'e']
False
Before sorting .......
['Tom', 'Jerry', 'Mickey']
After Sorting........

['Tom', 'Jerry', 'Mickey']
After Appending ..........
['Tom', 'Jerry', 'Mickey', 'Dick', 'Harry']
Before Inserting ...the index.......
['Tom', 'Jerry', 'Mickey']
['Dick', 'Tom', 'Jerry', 'Mickey']
After Inserting ..........
['Dick', 'Tom', 'Harry', 'Jerry', 'Mickey']
Before Popping ..........
['Tom', 'Jerry', 'Mickey']
After Popping . removing one element at the end.....
['Tom', 'Jerry']
Before Slicing ....slice(start:end:increment)......
['c', 'd', 'e', 'f', 'g']
With Negative indexing
['a', 'b', 'c', 'd', 'e', 'f', 'g']
With Negative indexing
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
With Specifying step
[2, 4, 6]
before ...modifying List Values
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After ..modifying List Values list[start:end] = [values]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Before ...Deleting List Values
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After ..Deleting index 2,3,4 values]
[0, 1, 4, 5, 6, 7, 8, 9, 10]
Copying Lists
['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'c', 'd', 'e']
False
Before sorting .......
['Tom', 'Jerry', 'Mickey']
After Sorting........
["Jerry;, 'MIckey','Tom']

Results are given and self explanatory with comments

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...