Python Notes-6

In continuation from
Python Notes-5
Python Notes-4
Python Notes-3
Python Notes-2
Python Notes-1



d = {1:2, 'q':2}

key:value pair

Unlike lists, items in dictionaries are unordered.

So access is only via key not by index. There is no first item or last item in a python dictionary data structure.

The order of items matters for determining whether two lists are the same, it does not matter in what order the key-value pairs are typed in a dictionary. Enter the following into the interactive shell:

>>> spam = ['cats', 'dogs', 'moose']
>>> bacon = ['dogs', 'moose', 'cats']
>>> spam == bacon
>>> eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'}
>>> ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'}
>>> eggs == ham

for key, value in d.items()

for key in d.keys() 

or just for key in d


for i in range(15):

for i in range(10):
for i in range(0,10):
for i in range(0,10,1):

all equals

you can make for loop count down

for i in range(5, -1, -1):

while True:

while a < 5:

Conditional statementif else elif

if name == 'Alice':
    print('Hi, Alice.')
elif name == ‘Bob’
   print(‘hi Bob’);
    print('Hello, stranger.')

mystring = "hello"
myfloat = 10.0
myint = 20

# testing code
if mystring == "hello":
    print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
    print("Integer: %d" % myint)

When used in conditions, 0, 0.0, and ” (the empty string) are considered False,

When you assign a list to a variable, you are actually assigning a list reference to the variable. A reference is a value that points to some bit of data, and a list reference is a value that points to a list. e.g.

❶ >>> spam = [0, 1, 2, 3, 4, 5]
❷ >>> cheese = spam
❸ >>> cheese[1] = ‘Hello!’
>>> spam
[0, ‘Hello!’, 2, 3, 4, 5]
>>> cheese
[0, ‘Hello!’, 2, 3, 4, 5]
The code changed only the cheese list, but it seems that both the cheese and spam lists have changed.

This is not true for c++ STL vector

To be continued …


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s