## Python

Python Quick Guide

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.

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’

Defining and Executing a Function:
Script:

```number = 12345
def digit_sum(numberFeed):
numberCalc = numberFeed + 1
return(numberCalc)

WhatWeGetBack = digit_sum(number)
print WhatWeGetBack
```
>12346

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

Reading a File, Writing to a File:

```
#Creates a File:
f= open(FileNameVariable,"w+")

#Opens a File
f2= open("/File/Location/", "r")

#Reads data in the file previously opened

#Write data to the file created
f.write(Content)

#Closes both files
f.close()
f2.close()
```

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']

>>> 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:
>>>Inventory = {'Hats' : 15, ...'Scarves' : 2, ...'Shaws' : 31}
>>> print Inventory['Hats']
15
In dictionaries, the entries on the left are called keys. The ones on the right are the values. They can be called, i.e. "print Inventory.keys".

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:
Script:

```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 dadatset "grades" (Not shown):

```def print_grades (grades_input):