Tag: number form sort length ESS%s Delete animal LAN
Python's data structure is: list, tuple, dictionary
List:
Function: Data structure for processing ordered items
List=["A", ' B ', ' V ', ' d ')
# Print Length
Print (Len (list))
# Loop Print
For I in list:
Print I
# Insert data at the end of the sequence
List.append ("5")
For I in list:
Print I
Print (List[0])
# Delete an element in a sequence
Del List[0]
Print (List[0])
# Sort the list
List.sort ()
Print (list)
Meta-group
Function: Similar to the same list
Difference: Tuples are immutable and cannot be modified
# using tuples
Zoo = (' Wolf ', ' Elephant ', ' penguin ')
print ' number of animals in the zoo is ', Len (Zoo)
New_zoo = (' monkey ', ' Dolphin ', zoo)
print ' number of animals in the new Zoo is ', Len (New_zoo)
print ' All animals in new Zoo is ', New_zoo
print ' Animals brought from the old Zoo is ', new_zoo[2]
print ' Last animal brought from the old Zoo is ', new_zoo[2][2]
Dictionary
Function: key and value contact. The key is unique. No order.
Tag form: D = {key1:value1, key2:value2}
# use dictionary
ab = {' Swaroop ': ' [email protected] ',
' Larry ': ' [emai L protected] ',
' Matsumoto ': ' [email protected] ',
' spammer ': ' [email protected] '
}
Print "Swaroop s address is%s"% ab[' Swaroop ']
# Adding a key/value pair
ab[' Guido '] = ' [email protected ] '
# Deleting a key/value pair
del ab[' spammer ']
Len (AB)
for name, address in Ab.items ():
Print (name, add RESS)
Sequence:
includes: list, tuple, String
Feature: index operator, slice operator
use:
# using sequence
Shoplist = [' Apple ', ' mango ', ' carrot ', ' banana ']
# Indexing or ' Subscription ' Operation
print ' item 0 is ', shoplist[0]
print ' Item 1 is ', shoplist[1]
print ' Item 2 Is ', shoplist[2]
print ' Item 3 are ', shoplist[3]
print ' Item-1 is ', shoplist[-1]
print ' Item-2 ', shoplist[-2]
# Slicing on a list
print ' Item 1 to 3 are ', Shoplist[1:3]
print ' Item 2 to end ', shoplist[2:]
print ' item 1 to-1 is ', shoplist[1:-1]
print ' Item "start to End", shoplist[:]
# slicing on a string
name = ' Swaroop '
P Rint ' characters 1 to 3 are ', Name[1:3]
print ' characters 2 to end are ', name[2:]
print ' characters 1 to-1 is ', name[ 1:-1]
print ' characters start to end is ', name[:]
Use of strings:
# string Use
name = ' Swaroop ' # This is a string object
If Name.startswith (' Swa '):
print ' Yes, the string starts with ' Swa '
If ' a ' in name:
print ' Yes, it contains the string ' a '
If Name.find (' War ')! =-1:
print ' Yes, it contains the string ' War '
delimiter = ' _*_ '
MyList = [' Brazil ', ' Russia ', ' India ', ' China ']
Print Delimiter.join (mylist)
Python Basics: Data structure Learning