Skip to content

Latest commit

 

History

History
101 lines (75 loc) · 1.33 KB

Python.md

File metadata and controls

101 lines (75 loc) · 1.33 KB

Python

Functions

Function Template

def functionname():
    # code

For Loop Template

fruits = ["apple", "banana", "cherry"]
for x in fruits:
    print(x)
    # loop through a list

for x in "persimmon":
    print(x)
    # loop through a string

for x in range(0, 10):
    print(x)
    # 0 to 9 (not including 10)

Dictionaries

Create a new dictionary

mydict = {
    "key1": value1,
    "key2": value2,
    "key3": value3,
}

Add a value to the dictionary

mydict["key4"] = value4

Remove a key and it's value

del mydict["key3"]  

Check the length (Number of pairs)

print len(mydict)

Check if key exists in a given dictionary

"key1" in mydict
# True
"key5" in mydict
# False

for item in mydict:
    if "key1" in mydcit:
        print "Key Found"
        break
    else:
        print "Key not Found"

Print all key and values

for key, val in mydict.items():
    print key, ':', val

Get only the keys from the dictionary

variablename = mydict.keys()
print variablename

Print the values from the dictionary

for item in mydict:
    mydictvalue = mydict[item]
    print mydictvalue

Print sorted dictionary

for key, value in sorted(mydict.items()):
print key, value