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

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-global-variables
Showing posts with label python-global-variables. Show all posts
Showing posts with label python-global-variables. 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 Removing key-value pairs

Python Removing key-value pairs




Removing key-value pairs

You can remove any key-value pair you want from a dictionary. To do so use the del keywords and dictionary name, followed by the key in square brackets. This will delete the key and its associated value.

#Deleting a key-value pair
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)

del alien_0['points']
print(alien_0)

Python variables Assignment

Python variables Assignment



Variables in Python

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.




code
#assignment=binding of a name with a value 
#1. evaluation of right side expression value
#2. assignment in order with left side names
x=1.2+8+sin(y)
a=b=c=0 #assignment to some value
y, z, r=9.2, -7.6, 0 #multiple assignment
a, b=b, a #values swap
a, *b=seq #unpacking of sequence in
*a, b=seq #item and list
x+=3 #increment=x=+3
x-=2 #decrement=x=x-2
x=None #undefined (constant value)
del x #remove name x