Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-list

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-list
Showing posts with label python-list. Show all posts
Showing posts with label python-list. Show all posts

Python Accessing values

Python Accessing values



Accessing values

TO access the value associated with an individual key give the name of the dictionary and then place the key in a set of square brackets. If the key you're asking for is not in the dictionary, an error will occur.
You can also use the get() method, which returns None insteadof an error if the key doesn't exist. You can also specify a default value to use if the key is not in the dictionary.

#Getting the value associated with a key 
alien_0 = {'color': 'green', 'points':5}

print(alien_0['color'])
print(alien_0['point'])
#Getting the value with get()
alien_0={'color':'green'}

alien_color = alien_0.get('color')
alien_points = alien_0.get('points', 0)

print(alien_color)
print(alien_points)

Screenshot 

Accessing Python

 

Python Modifying Values

Python Modifying Values



Modifying values

You can modify the value associated with any key in a dictionary. To do so give the name of the dictionary and enclose the key in sqaure brackets, then provide the new value for that key.

#Modifying values in a dictionary
alien_0 = {'color': 'green', 'points':5}
print(alien_0)

#change the aliein's color and point value.
alien_0['color'] = 'yellow'
alien_0['points'] = 10
print(alien_0)

Screenshot 

 

Python Slicing a list

Python Slicing a list





Slicing a list

You can work with any set of elements from a list. A portion of a list is called a slice. To slice a list start with the index of the first item you want, then add a colon and the index after the last item you want. Leave off the first index to start at the beginning of the list, and aleave off the last index to slice through the end of the list.

#Getting the first three items 
finishers = ['kai', 'abe', 'ada', 'gus', 'zoe']
first_three = finishers[:3]
#Getting the middle three items
middle_three = finishers[1:4]
#Getting the last three items
last_three = finishers[-3:]

Screenshot 

 

Python Copying a list

Python Copying a list




Copying a list

To copy a list make a slice that starts at the first item and ends at the last item. If you try to copy a list without using this approach, whatever you do to the copied list will affect the original list as well.

#Making a copy of a list
finishers =['kai', 'abe', 'ada', 'gus', 'zoe']
copy_of_finishers = finishers[:]
print(copy_of_finishers)

Screenshot

python copy list

 

Python Container Types

Python Container Types

Container Types in python
  • Ordered sequences, fast index acess , repeatable values 
       
code
#ordered sequences, fast index access, repeatable values
list [1, 5, 9] ["x", 11, 8.9] ["mot"]
tuple (1, 5, 9) 11, "y", 7.4  ("mot",)
str bytes (ordered sequences of chars/bytes)
#Key container, no a priori order , fast key access, each key is unique
#dictionary

 dict {"key":"value"}
dict (a=3, b=4, k="v")
#(key/value associations)
{1:"one", 3:"three", 2:"two", 3.14:"n"}
#collection
set{"key1","key2"}  {1, 9, 3, 0}
#keys=hashable values (base types, immutables...)  frozenset immutable set

Example
 
a=[1,5,9]
b=["x",11,8.9]
c=["mot"]
d=(1,5,9)
e=11,"y",7.4
f=("mot",)
g={"key":"value"}
j={"key1","key2"}
k={1,9,3,0}

print a
print b
print c
print d
print e
print f
print g
print j
print k

Screenshot

python container