Python

Python Quick Guide Not a python but a snake anyway

I love Python. It’s so simple!! This material below comes from the documentation, as well as from the Codecademy site. For many of these examples, I’m using the Python Shell. The “>>>” is the shell prompt, the unannotated lines below that are the response from the shell. Blocks of Python code, as used in an actual program, are unformatted.

FUNDAMENTALS:

Mathematical operations:
>>> 2+2
4

An equal sign assigns value to a variable:
Bob=1
>>> Bob
1

You can do complex numbers:
>>> D = complex (4, 5)
>>> D
(4+5j)

Variable names can be part of a formula:
>>> A = 10
>>> B = 20
>>> A * B
200

Variables are case sensitive. Strings need to be in double or single quotes.

Strings are indexed:
>>> L=”help”
>>> L[1]
‘e’

(And backwards):
>>> L[-2]
‘l’

Length of a string can be determined thusly:
>>> len(L)
4

Strings can be mathematically manipulated:
>>> A = 3
>>> B = “frank”
>>> A*B
‘frankfrankfrank’

LISTS and DICTIONARIES:

Make a list under one variable name:
>>> a = ['Henry', 'Maude', 10, 999]
>>> a
['Henry', 'Maude', 10, 999]

Chop up a list:
>>> a[0]
‘Henry’
>>> a[1:-1]
['Maude', 10]

Strings can be split into lists:
>>>string = "1408-Hemi-Semi-Ford.jpg"
>>> print string.split('-');
['1408', 'Hemi', 'Semi', 'Ford.jpg']

Add to a list:
>>> a = ['Bob', 'Alice']
>>> a.append('Frank')
>>> print a ['Bob', 'Alice', 'Frank']

Replace an element in a list:
>>> a = [1, 3, 5]
>>> a[1] = a[1] * 7
>>> print a [1, 21, 5]

Remove items from a list (3 ways):
>>> a = [10, 30, 50]
>>> a.pop(1)
>>> 30
>>> print a
[10, 50]

>>> b = ['Bob', 'Frank']
>>> b.remove('Frank')
>>> print b
['Bob']

>>> c = ['Engine', 'Transmission', 'Wheel']
>>> del(c[1])
>>> print c
['Engine', 'Wheel']

Determine the length of a list:
>>> len(a)
4

Clear list:
>>> a[:] = []
>>> a
[]

Sorting Lists:
>>> my_list = ["Tank", "Gun", "Hat"]
my_list.sort
for things in my_list
print things
Gun
Hat
Tank

Create a Dictionary:
Dictionaries are sets of key/value pairs, with the item ("key") on the left and the "value" on the right. A value can be a number, but also can be a string or a Boolean ("true")
>>>Inventory = {"Hats": 53,
"Scarves": 45,
"Shoes": 9

The entire set can be printed (though it will be printed in no specific order):
>>>print Inventory.items()
[('Scarves', 45), ('Hats', 53), ('shoes', 9)]

Print just the keys:
>>>print Inventory.keys()
['Scarves', 'Hats', 'shoes']

Print just the values:
>>>print Inventory.values()
[45, 53, 9]

LOOPS/CONDITIONALS:

If/statements:

>>> c=5
>>> if c == 5: print ‘yay’
yay

If/Elif:
>>> if c == 0: print ‘yay’
elif c == 5: print ‘nay’
nay

If/Else:
>>> if c == 0: print ‘yay’
else:print ‘boo’
boo

For ... in:
>>> my_list = [0,2,3]
for number in my_list:
print 2 * number
0
4
6

While loop:

startingPoint = 0
endingPoint = 10
tally = 1
while startingPoint < endingPoint:
    tally = tally * 3
    startingPoint = startingPoint + 1
print tally
>59049

Functions:
The "print_grades" function is defined, starting in the first line, and is called in the bottom line, along with the dataset "grades":

def print_grades (grades_input):
  for n in grades_input:
    print n
  return

grades = [80, 57, 63, 32]
print_grades(grades)
58

break

READING/WRITING FILES:

Reading the Files in a Directory, With Filter:
>>>Import glob
>>> print glob.glob('*.jpg')
['03-Succulent-DirtyBandits-Blah.jpg', '03-Succulent-Ezo-SinOfEnvy.jpg']

Creates a File:

f= open(FileNameVariable,"w+")
Opens a File
f2= open("/File/Location/", "r")
Reads data in the file previously opened:
Content =f2.read()
Write data to the file created:
f.write(Content)
Closes both files:
f.close()
f2.close()

break

Programming and System Administration Home