Python 3.x Tools

This is not an exhastive walkthrough of python 3.x. Regardless, I did my best to include everything that I thought would give the reader a solid foundation and reference. I find most of the time that knowing how to do something, to achieve a goal, is only half the battle—the other half is knowing what you need to do.





Intergratred Development Environment (IDE)

An IDE is an application used to facilitate software development. They often consist of at least a source code editor, automation tools, and a debugger. Essentially, if Microsoft Word is an application for written documnets—IDEs are for writing software.

There are a few really good IDEs for python. I would suggest Spyder from the Anaconda distribution. Anaconda is a distribution of packages mainly for scientific programming and development. It includes an IDE called Spyder. I've included a image of the Spyder IDE Below. Other IDEs for python include PyCharm—you could also use Visual Studio.

Subsequently on this site I'm going to be displaying code in 'code blocks'. When you see these blocks it indicates that what you are looking at is code.

                    
'''
This is a developer comment. This is in a code block.
Below are three variables that assign three different data. 
'''
In[]: x = 44
In[]: y = "ThisIsStringData"
In[]: dict_0 = {'int_dict': [1, 2, 3]} 
                    
                

Spyder IDE Spyder IDE On the bottom right is the Ipython console—the this interprets code. on the left is the code editor—you can write code there and run it in the console. On the upper right are some very useful tools like a plot viewer and a variable explorer. The two redacted lines are directories. The one on the right is your working directory and the left is the file path to your current file. Regardless, I highly suggest using Spyder.

You can find the Anaconda Distribution here. I highly recomend it.



Some Notes on setting up an anaconda environment:

Python has many pre-built modules. A module is pre-written code that can be used for whatever task or project. There will be times when you may import something that may not be in your current environment. You will have to install it in a Conda environment. This is true if you need anything that is not pre-packaged with the Anaconda Suite. In some cases it would be prudent to create a new environment to house specific modules becuase sometimes modules conflict with one another.

Some Notes on the Python Interpreter:

                
#A hashtag in the code denotes a developer comment on one line 
#you can also use this to preserve code you are debugging if you 
#think a line, or several lines, of code are creating an error. 

'''
Python is case sensitive!
Text encased in three single quotes,
denotes a multi-line comment or
code that has been "commented out". 
'''

"""
There is no difference between single and 
double quotes in python. These are implemented 
as per the coders context and preference—but it 
is best to use them consistently within blocks of code. 
""" 

'''
Python reads code like you would read a book. 
From left to right and then down. It stores and interprets
information depending on how the code is written. 
It can go back to previously written code depending on how
code is written and what tools have been used--if new to 
coding this may not make sense yet but may be more clear later. 
'''
                
            


Python Enhancement Proposal (PEP 8)

Writen english has a style guide depending on the industry or profession. For example a professional book or magazine editor would likely use the Chicago Manual of Style to edit a manuscript for professional publication. An acedemic work for a scientific journal may reference the American Psychological Association (APA) or Modern Language Association (MLA) style guides when writing a paper submission. Or, a legal editor may reference the Bluebook: A Uniform System of Citation when writing something for say Westlaw. These are all writing convention style guides for different industries.

Just like the style guides mentioned above Python also has an industry style guide known as Python Enhancement Proposals (PEP). Specifically PEP 8 is a style guide for writing python code.

Admittedly, not all of my code on this site follows correct conventions of PEP 8. Although, I have done my best to adhere to it. Being familiar with PEP 8 is a good first step and may help avoid headaches in the future.

You can find the PEP 8 style guide on python.org here.



Variable Assignment

The next, very simple, thing you can do is assign variables. Essentially you are telling the interpreter that SomeVariableObject = SomeData. The interpreter will store the assinged variable and data into memory for later use.

Basic Variable Assignment Rules:
1. Must start with a letter or underscore
2. Must only contain letters, numbers, or underscores
3. Cannot be reserved words
4. Are case sensitive

Simple variable assignment:

                    
'''
This is a developer comment. This is inside of a code block. Below are two 
variables that assign two different data. 
'''
In[]: x = 20 
In[]: y = 5
#now you can access this data and do things with it. Let's get the sum of x and y. 
In[]: x + y
Out[]: 25
                    
                

Multi-variable assignment: you can assign multiple variables to different data, or the same data, in one line.

                    
#assigns x = 1, y = 2, z = 3 
In[]: x, y, z = 1,2,3  

#assign all three variables to 5: 
In[]: x = y = z = 5
                    
                
















Reserved Words

Here is a list of the most common reserved words in python:

                 
'''
Reserved words are read by the Python interpreter to have a special meaning. 
These all do something that Python will automatically know to do. They are already
defined and you must avoid declaring objects by these names. 
'''
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield']
                 
            

If you name a variable a reserved word you will get a syntax error:

                
In[]: global = 9
Out[]: File "<ipython-input-13-eecb54146638>", line 1
        global = 9
        ^
    SyntaxError: invalid syntax
                
            

Compound Statements: You can have two separte statements in the same line with a semicolon.

        
In[] y = 10 ; x = 6 ; z = "SomeStringData"  #assigns data to variables x, y, and z
In[] print(z, x + y)
Out[]: 
SomeStringData 16
        
    


Operators

There are several types of operators. Most all of them can be chained to for more complex calls.

Arithmetic Operators:


'''
Operator	  Name	                 Example	
+	          Sum	                  x + y	
-	          Difference	          x - y	
*	          Product      	          x * y	
/	          Quotient	          x / y	
%	          Modulus	          x % y 
**	          Exponentiation	  x ** y	
//	          Floor division	  x // y
'''

Comparison Operators:


"""
Operator        Name                       Example	 
==             Equal                      x == y	
!=             Not equal                  x != y	
>             Greater than                x > y	
<             Less than                   x < y	
>=            Greater than or equal to    x >= y	
<=            Less than or equal to       x <= y
"""

Identity Operators:


'''
Operator     Description                                                Example
is            Retrurns True if both variables are the same object       x is y
is not        Returns True if both variables are not the same object    x is not y

'''







Bitwise Operators: You will also encounter bitwise operators. These operate a little differently:


'''
Operator	Name	    Description
& 	        AND	    Sets each bit to 1 if both bits are 1
|	        OR	    Sets each bit to 1 if one of two bits is 1
^	        XOR	    Sets each bit to 1 if only one of two bits is 1
~ 	        NOT	    Inverts all the bits
'''

Assignment Operators: These assign a variable and/or apply an operation:


'''
Operator        Description                                   Example
=               Assigns data to a variable                     x = 5 
+=              Assigns data to a variabale and adds           x += 5  
-=              Assigns data to a variabale and subtracts      x -= 9 
*=              Assigns data to a variable and multiplies      x *= 23
/=              Assigns data to a variable and divides         x /= 23
%=              Assigns data to a variable and modulus         x %= 4
**=             Assigns data to a variable and exponentiation  x **= 10
//=             Assigns data to a variable and floor division  x //= 2
'''
#Assignment operators are useful if you need to redefine a variable: 
In[]: x = 5 
In[]: x += 10 ; print(x)
Out[]: 15 

Membership operators:


'''
Operator     Description                                                                    Example
in           Returns a boolean value whether the specified value is in the present object    x in y
not in       Returns a boolean value whether the specified value is not in present object    x not in y
'''


Data Types

Understanding data types and what they do is very important. Here are the common data types used in Python (and computer/data science generally). They are the main building-blocks for organizing and manipulating data.


Basic data types:


'''
Type	     Example
integer	        5
float	        5.0
string	        "SomeString"
list	        [1, 2, 3]
tuple	        ("this", "is", "a", "tuple", "of", "strings")
range	        range(5)
boolean	        True
Dictionary     {"name":"Jerry", "age":25}
'''








Rules for sequence and mapping types:


a = ["desk", "pen", "book"] #list (ordered, changeable, duplicates allowed) 
a = ("cow", "chicken", "sheep") #tuple (ordered, unchangeable, duplicates allowed)
x = {"name" : "John", "age" : 36} #dict (unordered, changeable, indexed, No Duplicate Keys) 
x = {"apple", "banana", "cherry"} #set (unordered, unindexed, No Duplicates, iterate faster)
You are in a very good spot once you understand data types and their respective methods. Methods are built in attributes of data types in python. You use them to manipulate data and "do" things.


Basic Indexing

Some data types in Python have what is called an index. This can be benificial for accessing data within data. Here are some examples:


#an index always starts with 0 
#the syntax for accessing the index of data is applied with square brackets
#think of it like some_indexed_data_obj[start:stop:step]

#let's first look at the index of a very simple string
In[] some_string = "This is a string."

#so let's slice index 0 from this string
In[]: some_string[0]
Out[]: 'T' 

#now let's slice the first four data from some_string
In[]: some_string[0:4]
Out[]: 'This'

#and now let's slice all the even indexed data in some_string
In[]: some_string[0::2]
Out[]: 'Ti sasrn.'
    

Basic list indexing example:


#lists also have an index
#let's acces the first data in a List
In[]: some_list = ['this', 'is', 'a', 'list', 'of', 'strings'] 

In[]: some_list[0]
Out[]: 'this'

#or let's access the third 
In[]: 'a'

#or we can access the last string in the list of strings
In[]: some_list[-1]
Out[]: 'strings'

#now you can access the a sting in the list and data in that string
In[]: some_list[-1][0:2]
Out[]: 'st'

#say we had a list of numbers that represented some data
In[]: another_list = [10, 8, 4, 9, 7, 17]

#let's pull the first number and add 30
In[]: another_list[0] + 30
Out[]: 40

#or subract the first number from the last number
In[]: another_list[-1] - another_list[0]
Out[]: 7
        

Basic dictionary indexing:


In[]: dict_ex_1 = {'key_1': [7, 6, 5, 3, 4],
                   'key_2': ['France', 'Italy', 'Spain', 'Morocco', 'Germany'],
                   'key_3': [1000, 745, 662, 321, 478]}

#access the entire list at key value 'key_2'
In[]: dict_ex_1['key_2']
Out[]: ['France', 'Italy', 'Spain', 'Morocco', 'Germany']

#now access 'Italy' in that list
In[]: dict_ex_1['key_2'][1]
Out[]: 'Italy'




You will also encounter nested data types. This is like data within data. For example nested lists are lists within lists. Or, a dictionary within a dictionary. Or even a list within a list within a dictionary. It can get rather complex. Here are some examples:

Nested list indexing:


#a list of lists also has an index 
#thus the first list can be accessed with index slicing
#here is our list of lists:

In[]: some_list_of_lists = [['dog', 'cat', 'mouse'], [30, 24, 7], ['lost', 'found', 'unknown']]

#let's access the first list
In[]: some_list_of_lists[0]
Out[]: ['dog', 'cat', 'mouse']

#say we want to access the second item and only the second item of the first list
In[]: some_list_of_lists[0][1]
Out[]: 'cat'

#or access the last data in the last string of the first list of strings
In[]: some_list_of_lists[0][2][-1]
Out[]: 'e'
    

Basic nested dictionary indexing example:


#note here that the dictionary keys like 'dict_key_1' are arbitrary; they could be anything
#also 1, 2, and 3 at the beggining of each dictionary is the index of the dictionaries 
#and could also be anything
In[]: nested_dict_ex = {1 : {'dict_key_1':[1, 2, 3], 'another_key_d_1':['some', 'values', 'in', 'a', 'list of vals']},
                        2 : {'dict_key_1': ['dog', 'cat', 'mouse']},
                        3 : {'dict_key_1': ['sad', 'happy', 'indiferent']}}

#access the second dictionary
In[]: nested_dict_ex[2]
Out[]: {'dict_vals_2': ['dog', 'cat', 'mouse']}

#use a key value to access the first list of dictionary 2 
In[]: nested_dict_ex[2]['dict_key_1']
Out[]: ['dog', 'cat', 'mouse']

#use a list index to access the second data in dictionary 2 key 1 list 1 (index list 0)
#In plain language you might say; nested_dict_2[NestedDictionaryIndex][NestedDictionaryKey][NestedDictionaryListIndex]
In[]: nested_dict_ex[2]['dict_key_1'][1]
Out[]: 'cat'

#access the last data in the indexed string
In[]: nested_dict_ex[2]['dict_key_1'][1][-1]
Out[]: 't'
        


Basic Function Syntax:

Understanding functions early when learning Python is a very important. I'll be honest I avoided a solid understanding of functions when I first started. For me, functions were a very abstract concept—at least when I first started. Regardless, you will use them and you must understand the basics of how they work and what they do.

The basic rules of functions:


'''
1. functions are started (instantiated) by def 
2. function names should describe what the function does 
3. function arguments (parameters) can range from 0 to whatever is needed  
4. the body of a function is the functions own local scope 
5. outside of the function is the global scope
5. functions return something or nothing (None)
'''

def funct_name(some_funct_argument):
    '''
    This is the function DocString. It tells the user what 
    the function does and how to use it. In this case: 

    pass one int or float and the function adds 5.
    Ex: funct_name(10)
    '''
    some_funct_argument += 5
    return some_funct_argument

#call the function in the console
In[]: funct_name(10)
OUT[]: 15

In[]: funct_name(10.3265)
Out[]: 15.3265
        














Another basic function with some call and assignment methods:


In[]: def do_somthing_with_2_vals(val_1, val_2): 
            '''
            Given two numeric values the function returns the quotient of 
            value one and two and then adds 5. 
            '''
            y = val_1 / val_2 + 5
            return y

In[]: do_somthing_with_2_vals(20, 10)
Out[]: 7.0

In[]: do_somthing_with_2_vals(10, 20)
Out[]: 5.5

#you can assign the output to an obj variable:
In[]: fifty_and_ten = do_somthing_with_2_vals(50, 10)
In[]: fifty_and_ten
Out[]: 10.0

#you can assign the output to an obj variable and perform another operation:
In[]: some_object_var = do_somthing_with_2_vals(50, 10) + 6
In[]: some_object_var
Out[]: 16.0
        


Basic Iteration:

Iteration is another very important tool that you will use in Python. Iteration is a process that repeats through something over and over to generate a desired outcome.
Here is an example:


'''
Say we have a list of integers that represent a some variable. 
It could be anything from the price of an apple over several 
thousand stores, or the ages of a population between 18 and 95. 
For brevity we'll just use a range of numbers between 0 and 19. 
They carry no significant meaning accept to show this example. 
'''
#assign an obj to a list of int: 
In[]: ex_lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

#Let's print all the numbers greater than 15 with list iteration using a for loop:
In[]: for i in ex_lst:                   #For every item in ex_lst
          if i > 15:                     #If that item is greater than 15 (a condition)
             print(i, end=', ')          #print that item and end each item with a comma and a space (an outcome)

Out[]: 16, 17, 18, 19, 

#Note that print is a built in function in python that is used to display data or a message;
#print() does not return actual data it's only a way to display data

#Let's say we wanted to subtract 5 from every item that we print: 
In[]: for i in ex_lst:
          if i > 15: 
             print(i - 5, end=' ')       #print that item but subtract five and separte them by a space

Out: 11 12 13 14 

#we can create a separate list to get the data into a separate obj: 
In[]: gt_15 = []                       #an empty list 
In[]: for i in ex_lst:                 #for every item in ex_lst
          if i > 15:                   #if the item is greater than 15
             gt_15.append(i)           #add that item to the list 

In[]: gt_15     #call the new obj 
Out[]: [16, 17, 18, 19]    

#or change the data 'inplace' meaning change the obj itself
In[]: for i in ex_lst: 
          ex_lst[i] = i - 9

In[]: ex_lst
Out[]: [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#I highly suggest experimenting with iteration; there is a lot you can do:
            

Basic Comprehension:

Basic comprehension is shorthand syntax for creating lists and/or dictionaries.


#In a very simple example create a list of numbers between 0 and 10
In[]: comp_ex_1 = [i for i in range(0, 11)] ; print(comp_ex_1)
Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#for our list in comp_ex_1 let's use iteration to make a 
#new list of all the numbers greater than 5
In[]: greater_than_5 = [i for i in comp_ex_1 if i > 5]; print(greater_than_5)
Out[]: [6, 7, 8, 9, 10]

'''
comprehension can be very useful for applying something like a function to
large amounts of data. For example, say you were analyzing the lexical complexity 
of words in whatever data. One measure you may need to attain is the lenth of 
each word used therein. So let's apply this to the following list: 
'''
In[]: words_list = ['tenebrous', 'bibulous', 'do', 'not', 'know',
                    'many', 'complexity', 'for', 'now']

#now with comprehension we can apply a function to each to yield a 
#new list that contains the length of each word 
#Note here that len() is a built in function in python that returns the integer length of some specified data
In[]: length_word_list = [len(i) for i in words_list] ; print(length_word_list)
Out[]: [9, 8, 2, 3, 4, 4, 10, 3, 3]

#so from here let's add five to every item in greater_than_5 instantiated above
In[]: [i + 1 for i in greater_than_5]
Out[]: [7, 8, 9, 10, 11]

#similarly the same can be done for keys and values in dictionaries
#I have mostly used this to create dictionaries from 2 separate lists

#create our lists
In[]: list_1 = [i for i in range(0, 5)]
In[]: list_2 = ['cats', 'dogs', 'mice', 'cows', 'chickens']
#comprehension to convert them 
In[]: dict_ex_from_lists = {k : v for (k,v) in zip(list_1, list_2)}

In[]: print(dict_ex_from_lists)
Out[]: {0: 'cats', 1: 'dogs', 2: 'mice', 3: 'cows', 4: 'chickens'}
        


Pythons Import System and dot syntax:

Python has many pre-built modules that you can import into a program. A pre-built modual is a collection of code you can call to accomplish a task. Here's a really simple example of very useful built-in module:


In[]: import string  #this imports the string module and now you can access some nifty pre-built code
In[]: string.ascii_letters 
Out[]: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  #a string of all the ascii letter characters

In[]: string.punctuation #This is dot syntax; the dot after string calls the attribute or method of an object
Out[]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' 
'''
Things like the above can be exremely good resources to tap into. As you 
learn more and more you combine tools you learn to make something function 
like you want it to. 
'''
    

Built-in Functions

Python also has a lot of built in functions, which are also very useful. You can check them all out in Python's documentation here.


"""A few said built-in functions are type(), int(), float()"""
#type() returns the data type of the object passed:
In[]: x = 11; y = 3.1587 ; z = "SomeString"
In[]: type(x)
Out[]: int
In[]: type(y)
Out[]: float
In[]: type(z)
Out[]: str

#int() converts a numeric input to an integer:
In[]: int(y)
Out(): 3 

#float converts an numeric input to a float:
In[]: float(x)
Out[]: 11.0

#convert string data to a list of strings
In[]: list(z)
Out[]: ['S', 'o', 'm', 'e', 'S', 't', 'r', 'i', 'n', 'g']
        


String Methods

A string is just text. Strings have an index that starts at 0.

slice a string:


In[]: s_1 = 'This is a string and only one string.'

#Access string data at index[x]

#index 0
In[]: s_1[0]
Out[]: 'T' 

#index 4
In[]: s_1[4]
Out[]: ' '

#index 0 to 8
In[]: s_1[0:8]
Out[]: 'This is ' 

#index 0 to 20 stepping 2 at a time
In[]: s_1[0:20:2]
Out[]: 'Ti sasrn n'

Count the number of specified occurances in a string:


In[]: x = 'this is a string'
In[]: x.count('t')
out[]: 2

#Counts the number of 'i's between two index positions
In[]: x.count('i',3,12) #between index 3 and 12
Out[]: 1

Find the index of a string data:


In[]: x = 'this is a string and only one string'
In[]: x.find('and')
#returns the index number where the first instance 'and' begins
Out[]: 17   
#Between a range from the given point
In[]: x.find('a',10,20)
Out[]: 17
In[]: x.find('a',18,20)
Out[]: -1 #neg 1 means is not found

Get the index of an item in a string:


In[]: st_1 = 'this is a string and only one string because it is a string'
In[]: st_1.index('is a')
Out[]: 5 

#with start and stop range
In[]: st_1.index('is a',10,59)
Out[]: 48

Insert cammas into a large integer:


In[]: x = 12567854225521
In[]: '{:,}'.format(x)
Out[]: '12,567,854,225,521'

Strip leading and trailing whitespace from a string:


In[]: st_data = '   this is a string       '
In[]: st_data.strip()
Out[]: 'this is a string'

Return whether a string is alphanumeric:


In[]: a = 'a_string' ; b = 'a8string' ; c = 'a2_string'
In[]: a.isalnum()
Out[]: False
In[]: b.isalnum()
Out[]: True
In[]: c.isalnum()
Out[]: False

Input a backslash to print it:


In[]: print('\\')  #a single backslash is not interpreted as a backslash 
Out[]: \

Return the truth value of ends with:


Ends with truth value: 
In[]: x = 'this is a string and only one string'
In[] x.endswith('ing') 
Out[]: True
#between an index range
In[]: x.endswith('is',0,7)
Out[]: True

Find the index of the first instance in a string starting at the end:


In[]: data = 'this is a string and only one string'
In[]: data.rfind('string')
Out[]: 30

#start and stop range can also be used 

Replace data in a string with other data:


    In[]: data = 'this is a string and only one string'
    In[]: data.replace(' ','')
    Out[]: 'thisisastringandonlyonestring'
    
    #using count; first x instances replace
    In[]: x = 'ha ha ha ha ha ha'
    In[]: x.replace('ha','gaww',3)
    Out[]: 'gaww gaww gaww ha ha ha'

The format string method to insert data into a string:


In[]: 'Hello, {0}, you are a complete {1}'.format('Bob',"Robot")
Out[]: 'Hello, Bob, you are a complete Robot'

Truth value for whether a string is alphabetical:


In[]: a = 'stringdata' ; b = 'string99data'
In[]: print(a.isalpha(), b.isalpha(), end= ' ')
Out[]: True False
#also for numeric()

Remove punctuation from a string or list of dirty strings:


In[]: def remove_punc(data): 
            punc = '''!()-[]{};:'"\, <> ./?@#$%^&*_~'''
            if isinstance(data,str) == True:
                for i in data:  
                    if i in punc:  
                        data = data.replace(i, "")
            elif isinstance(data,list) == True:
                data = [remove_punc(i) for i in data]
            return data

In[]: dirty_lst_of_str = ['Th$$is.', 'is!', 'a?', '!very-dirty$$','Li$$st', '!)of', 'str@@ing']
In[]: remove_punc(dirty_lst_of_str)
Out[]: ['This', 'is', 'a', 'verydirty', 'List', 'of', 'string']








Return the length of a string:


In[]: st_1 = 'this is a string and only one string because it is a string'
In[]: len(st_1)
Out[]: 59

Convert a string to upper case or lowercase:


In[]: x = 'this is a single string' 
In[]: print(x.upper(),x.lower())
Out[]: THIS IS A SINGLE STRING this is a single string

Capitolize the first letter of a string:


In[]: x = 'thing is a string'
In[]: x.capitalize()
Out[]: 'This is a string'

Print a string backwards:


In[]: z = 'This is a string and only one string'
In[]: print(z[50:-50:-1])   
Out[]: 
gnirts eno ylno dna gnirts a si sihT

Get the last nth data from a string after a specified mark:


In[]: file = r'path/to/file/filename.extension'
In[]: file.split('/')
Out[]: ['path', 'to', 'file', 'filename.extension']

In[]: file.split('/')[-1]
Out[]: 'filename.extension'

In[]: file.split('.')[-1]
Out[]: 'extension' 

Join data into a string for every instance of the string:


In[]: data_str_1 = 'ABC'
In[]: joinder = ','
In[]: joinder.join(data_str_1)
Out[]: 'A,B,C'
#Ex 2
In[]: data_to_join = ' and '
In[]: data_to_join.join(data_str_1)
Out[]: 'A and B and C'

Add data to a string every x amount of space using list format, join(), and list comprehension:


In[]: data = 'BBAAIIEEPPSSLL'
In[]: ', '.join(data[i:i+2] for i in range(0,len(data),2)) 
Out[]: 'BB, AA, II, EE, PP, SS, LL'

Remove unwanted data from a dirty string:


In[]: dirty_ex_str = '    woah      d#@$* '
In[]: cleaned_ex_str = dirty_ex_str.strip(' d#@$*') ; print(cleaned_ex_str)
Out[]: woah

Print a new line from within a string (uses an escape character):


In[]: print('data is data\n because it is data')
Out[]: 
data is data
  because it is data

Return all the capitol words in a list of string:


In[]: s_2 = ['This', 'is', 'A','list','of', 'Strings', 'and', 'only', 'one','list','of', 'strings.']
In[]: [w for w in s_2 if w.istitle()]   #list comprehension
Out[]: ['This', 'A', 'Strings']

Return words that endwith x data in a list of strings:


In[]: s_2 = ['This', 'is', 'A','list','of', 'Strings', 'and', 'only', 'one','list','of', 'strings.']
In[]: [w for w in s_2 if w.endswith('s')]
Out[]: ['This', 'is', 'Strings']

In[]: [w for w in s_2 if w.endswith('.')]
Out[]: ['strings.']

Return a lowcase list of strings:


In[]: s_2 = ['This', 'is', 'A','list','of', 'Strings', 'and', 'only', 'one','list','of', 'strings.']
In[]: [w.lower() for w in s_2]  #w.upper will return all uppercase 
Out[]: ['this', 'is', 'a', 'list', 'of', 'strings', 'and', 'only', 'one', 'list', 'of', 'strings.']

Center and pad a string:


In[]: x = 'this is a string'
In[]: x.center(24)
Out[]: '    this is a string    '

#center with fill character
In[]: x.center(24,'.')
Out[]: '....this is a string....'

Split a string for every specific instance of x:


In[]: abc_data = 'a,b,c,d,e,f,g,h,i,j'
In[]: abc_data.split(',')
Out[]: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Separate a single string of mixed data into separate lists; this also works for a list of mixed data:


    In[]: mixed_data_str = 'ab456cde'
    In[]: def sep_funct_ex(somestring):
              alpha = []
              nums = []
              for i in somestring: 
                  try: 
                      if float(i).is_integer() == True:
                          nums.append(i)
                  except ValueError:
                      alpha.append(i)
              return [alpha, nums]
    
    In[]: separated_data = sep_funct_ex(mixed_data_str)
    In[]: separated_data
    Out[]: [['a', 'b', 'c', 'd', 'e'], ['4', '5', '6']]


List Methods

Lists are a type of sequencing data that can contain other datatypes within. List elements include:
1. indexed
2. mutable (changeable)
3. can contain duplicates

Return a list of numbers in sequence from a specified range:


In[]: nums = []
In[]: for i in range(0,10):
            nums.append(i)
In[]: print(nums)
Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Return the length of a list:


In[]: ex_lst = ['dog','fox','jumped'] ; ex_num_lst = [1,2,3]
In[]: print(len(ex_lst)) ; print(len(ex_str)); print(len(ex_num_lst)) 
Out[]: 
3
3

Return the average length of a string in a list of strings:


In[]: s_2 = ['This', 'is', 'A','list','of', 'Strings', 'and', 'only', 'one','list','of', 'strings.']
In[]: avg_s_2 = sum(map(len, avg_s_2)) / len(avg_s_2)
In[]: avg_s_2
Out[]: 3.6666666666666665

Replace a range of items at a specified index with contents of an iterable:


In[]: ex_obj = ['dog','wing','foo','book','cat','bird']
In[]: ex_obj[1:4]='good'; print(ex_obj)
Out[]: ['dog', 'g', 'o', 'o', 'd', 'cat', 'bird']

Return a truth value for every item that starts with specific data:


In[]: ex_lst = ['dog', 'wing', 'foo', 'book', 'cat', 'bird', 'rhino']
In[]: for i in ex_lst:
            print(i.startswith('b'), end=' ')
Out[]: False False False True False True False 

Combine two lists of strings as a set (drops duplicates):


In[]: var1 = ['a' , 'b', 'c', 'd']
In[]: var2 = ['a' , 'b', 'e', 'f'] 
In[]: x = list(set(var1 + var2))
In[]: x
Out[]: ['f', 'd', 'c', 'a', 'e', 'b']

Return the index of duplicate items in a list:


In[]: var_str_lst = ['The', 'small', 'brown', 'fox', 'jumped',
                     'over','the','lazy','dog','and','the',
                     'dog','chased','the','fox']
In[]: [i for i,d in enumerate(var_str_lst) if d == 'the']
Out[]: [6, 10, 13]

Return all x items from a list of strings that exist at least once:


In[]: var_str_lst = ['The', 'small', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
In[]: new_list = []
In[]: for i in var_str_lst:
            new_list = [i for i in 'zaeiou']
In[]: print(new_list)
Out[]: ['z', 'a', 'e', 'i', 'o', 'u']

Merge two lists into a list of tuples:


In[]: keys_2 = ['year','name','subject','score'] ; values_2 = [1999,'Andrew','calculus',80]
In[]: lst_of_tup = list(zip(keys_2,values_2)) ; print(lst_of_tup)
Out[]: [('year', 1999), ('name', 'Andrew'), ('subject', 'calculus'), ('score', 80)]

Reverse the order of a list of strings:


In[]: lst_1 = ['a','b','c']
In[]: for i in reversed(lst_1): 
            print(i)
Out[]: 
c
b
a

Iterate through a list and replace a specific value if found:


In[]: another_lst = ['dog','fox','jumped']
In[]: new_list = [i.replace('dog','lion') for i in another_lst]; print(new_list)
Out[]: ['lion', 'fox', 'jumped']

Convert a string item by index to a list of strings:


In[]: var_lst = ['The small brown fox jumped over the lazy dog','and then foobar', 'another string', 'last']
In[]: var_lst[0].split()
Out[]: ['The', 'small', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']

Append data to each string in a list of strings:


In[]: var_str_doo = [x + 'doo' for x in var_str] ; print(var_str_doo)
Out[]: ['Thedoo', 'smalldoo', 'browndoo', 'foxdoo', 'jumpeddoo', 'overdoo', 'thedoo', 'lazydoo', 'dogdoo']

Append a random list of int to an empty list:


In[]: import random
In[]: E = []
In[]: for i in random.sample(range(0,30),10):   #a random number between 0 and 30 ten times.
          E.append(i)
In[]: E 
Out[]: [8, 5, 26, 23, 29, 6, 12, 2, 14, 22]

Return all items from a list that equal some data to a new list:


In[]: var_str_lst = ['The', 'small', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
In[]: new_list = [i for i in var_str_lst if i == 'fox']; print(new_list)
Out[]: ['fox']

#Also works for list obj:
In[]: another_lst = ['dog','fox','jumped']
In[]: new_list_2 = [i for i in var_str_lst if i in another_lst]; print(new_list_2)
Out[]: ['fox', 'jumped', 'dog']

Remove unwanted data from a list of dirty strings:


In[]: dirty_lst_str = ['good  $','#cats','&&bad','***  birds'] ; cleaned_lst_str = []
In[]: for i in dirty_lst_str:
            cleaned_lst_str.append(i.strip(' $#&*'))
In[]: print(cleaned_lst_str)
Out[]: ['good', 'cats', 'bad', 'birds']

Nested list comprehension:


In[]: nested_lst = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
#Transpose rows and cols
In[]: [[row[i] for row in nested_lst] for i in range(4)]
Out[]: [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
#alt with built in functions
In[]: list(zip(*nested_lst))
Out[]: [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

Pass a list through a dictionary and return the values to a list:


In[]: alpha_dict = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 
                   11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 
                   20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

In[]: ten_rand_nums = [5, 19, 20, 22, 13, 23, 4, 4, 10, 14, 9]
In[]: [alpha_dict[k] for k in ten_rand_nums]
Out[]: ['e', 's', 't', 'v', 'm', 'w', 'd', 'd', 'j', 'n', 'i']

Separate data into two plus lists from a mixed datatype list:


In[]: ex_1 = [1,2,3,'a','b','c',5,2,4,'x','5','z']  #mixed datatype list
In[]: def sep_funct_ex(mixed_data):
            alpha = []
            nums = []
            for i in mixed_data: 
                try: 
                    if float(i).is_integer() == True:    
                        nums.append(i)
                except ValueError:
                    alpha.append(i)
            return [alpha, nums]

In[]:sep_data = sep_funct_ex(ex_1)  #returns a nested list
In[]: sep_data
Out[]: [['a', 'b', 'c', 'x', 'z'], [1, 2, 3, 5, 2, 4, '5']]

Access data in nested list: 
In[]: sep_data[0]
Out[]: ['a', 'b', 'c', 'x', 'z']
In[]: sep_data[0][0]
Out[]: 'a'

In[]: sep_data[1]
Out[]: [1, 2, 3, 5, 2, 4, '5']
In[]: sep_data[1][0]
Out[]: 1

In[]: alpha, nums = sep_funct_ex(ex_1)   #returns alpha and nums in two separate list obj

In[]: alpha
Out[]: ['a', 'b', 'c', 'x', 'z']

In[]: nums
Out[]: [1, 2, 3, 5, 2, 4, '5']











Replace an item in a list at index x:


In[]: ex_obj = ['dog','wing','foo','book','cat','bird']
In[]: ex_obj[1] = 'good'; print(ex_obj)
Out[]: ['dog', 'good', 'foo', 'book', 'cat', 'bird']

Remove an item from a list at index x:


In[]: ex_obj = ['dog','wing','foo','book','cat','bird']
In[]: del ex_obj[4] ; print(ex_obj)
Out[]: ['dog', 'wing', 'foo', 'book', 'bird']

Remove all items in a list:


In[]: num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In[]: num_list.clear() ; print(num_list)
Out[]: []

Convert list of strings to one string:


In[]: s_2 = ['This', 'is', 'A','list','of', 'Strings', 'and', 'only', 'one','list','of', 'strings.']
In[]: ListToStr = ' '.join(map(str,s_2))
In[]: ListToStr
Out[]: 'This is A list of Strings and only one list of strings.'

Access the first x items in a range:


In[]: for i in range(50,1000,50)[0:4]:
          print(i)
Out[]: 
50
100
150
200

Return every x item in a list of items:


In[]: var_str_lst = ['The', 'small', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
In[]: print(var_str_lst[0::3])
Out[]: ['The', 'fox', 'the']

Sort a list alphabetically:


In[]: ex_lst = ['zed','poll','apple']
In[]: for i in sorted(ex_lst):
            print(i, end=' ')
Out[]: apple poll zed
#ALT
In[]: ex_lst = [i for i in sorted(ex_lst)] ; print(ex_lst, end=' ')
Out[]: ['apple', 'poll', 'zed'] 

Some string formatting examples:


In[]: ex_var = 'dog' ; ex_var = ['dog','wing','foo','book','cat','bird']

In[]: '{0}'.format(ex_var)  #returns the entire obj
Out[]: 'dog'

In[]: '{0}'.format(ex_var)  #returns the entire obj
Out[]: "['dog', 'wing', 'foo']"

In[]: '{0[1]}'.format(ex_var_2) #returns an item in x position
Out[]: 'wing'

In[]: '{0[3]} , {0[0]} , {0[5]}'.format(ex_var) #returns multipul from dif positions
Out[]: 'book , dog , bird' 

In[]: 'This is a string that inserts item {0[3]} from the object'.format(ex_var)
Out[]: 'This is a string that inserts item book from the object'

Add (append) an item to the end of a list:


In[]: ex_obj = ['dog','wing','foo','book','cat','bird']
In[]: ex_obj.append('rhino') ; print(ex_obj)
Out[]: ['dog', 'wing', 'foo', 'book', 'cat', 'bird', 'rhino']

Append items to a list from another list that start with specific data:


In[]: ex_lst = ['dog', 'wing', 'foo', 'book', 'cat', 'bird', 'rhino'] 
In[]: b_words = []
In[]: for i in ex_lst:
            if i.startswith('b') == True:
                b_words.append(i)
In[]: print(b_words) 
Out[]: ['book', 'bird']

Count the number of a specific item in a list:


In[]: var_str_lst = ['The', 'small', 'brown', 'fox', 'jumped',
                     'over','the','lazy','dog','and','the',
                     'dog','chased','the','fox']
In[]: var_str_lst.count('fox')
Out[]: 2

Return all x items from a string to a list:


In[]: var_str = 'The small brown fox jumped over the lazy dog'
In[]: [i for i in var_str if i in 'aeiou']
Out[]: ['e', 'a', 'o', 'o', 'u', 'e', 'o', 'e', 'e', 'a', 'o'] 

Iterate through two or more list (or sequence data generally) with format():


In[]: lst_1 = ['a','b','c'] ; 
In[]: lst_2 = ['first letter','second letter','third letter']
In[]: for i, p in zip(lst_1,lst_2):
            print('{0} {1}'.format(i,p))
Out[]: 
a first letter
b second letter
c third letter

Return the index postition of items in a list or series:


In[]: another_lst = ['dog','fox','jumped']
In[]: for i, v in enumerate(another_lst):
            print(i,v)
Out[]: 
0 dog
1 fox
2 jumped

Return conditioned integers from a list of integers:


In[]: num_lst = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#Return all the even numbers from a list using list comp and modulo
In[]: evens = [i for i in num_lst if i%2 == 0] ; print(evens)
Out[]: [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]  #modulo uses absolute values of int
#Return all the odd numbers from list using list comp and modulo
In[]: odds = [i for i in num_lst if i%2 != 0] ; print(odds)
Out[]: [-9, -7, -5, -3, -1, 1, 3, 5, 7, 9]
#Return all the nums divisible by x int:
In[]: by_3 = [i for i in num_lst if i%3 == 0]; print(by_3)  #i%3 indicates the int that the must evenly div
Out[]: [-9, -6, -3, 0, 3, 6, 9]
#Return all nums with a comparison operator:
In[]: greater_than_5 = [i for i in num_lst if i > 5] ; print(greater_than_5)
Out[]: [6, 7, 8, 9]
#Return nums with a chained comparison operator: 
In[]: more_than_2_less_than_7 = [i for i in num_lst if i > 2 and i < 7] ; print(more_than_2_less_than_7)
Out[]: [3, 4, 5, 6]

Convert a list to a set:


In[]: ListObj = ['The', 'small', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
In[]: SetObj = set(ListObj)
In[]: SetObj
Out[]: {'The', 'brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'small', 'the'}

Generate a random list of integers:


In[]: import random
In[]: rand_int = []
In[]: for i in range(10):
            rand_int_2.append(random.randrange(0,11))
In[]: print(rand_int)
Out[]: [6, 4, 10, 8, 3, 10, 4, 10, 2, 0]

List Comprehension syntax: 
import random
[random.randint(1, 6) for _ in range(6)]  #returns a list of 6 random numbers range(6); between 1 and 6 randint(1,6) 

Convert two lists to a dictionary:


In[]: keys=[1,2,3] ; values=['a','b','c']
In[]: dictionary_ex = dict(zip(keys,values)) ; print(dictionary_ex)
Out[]: {1: 'a', 2: 'b', 3: 'c'}

Insert data to a given index into a list:


In[]: ex_lst_1 = [1,2,3,4,5,6] 
In[]: ex_lst_1.insert(2,99) ; print(ex_lst_1)
Out[]: [1, 2, 99, 3, 4, 5, 6]
In[]: ex_lst_1.insert(2,'peeps') ; print(ex_lst_1)
Out[]: [1, 2, 'peeps', 99, 3, 4, 5, 6]

Pass each item in a list through a function using list comprehension:


In[]: var_int_lst = [1,2,3,4,5,6,7,8,9,10]
In[]: def ex_funct(some_int_val): 
            some_int_val = some_int_val + 5
            return some_int_val
In[]: [ex_funct(i) for i in var_int_lst]
Out[]: [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]


Dictionary Methods

Dictionaries are very useful for mapping many different types of data. They contain what are called key and value pairs. The syntax looks like this:

some_dict_obj = {"k_1": "v", "k_2": "v"}

In the above k_1 and k_2 represent key values, which is are not limited to a string. It could be a integer too. One limitation is that you can't have duplicate key values.

The "v" in the above represents dictionary values. They are also not limited to strings. They could lists or integers, or really whatever data you want.

Access data at index x from complex dictionary:


In[]: multi_val_dict_ex = {'names':['jon','juan','sam'],
                           'ages':[55,66,32],
                           'scores':[45,100,90]}
In[]: multi_val_dict_ex['names']
Out[]: ['jon', 'juan', 'sam']
In[]: multi_val_dict_ex['names'][1]
Out[]: 'juan' 
In[]: multi_val_dict_ex['names'][1][2] #returns the index[2] of the index[1]  
Out[]: 'a' 

Append a dictionary to another dictionary (if the keys are duplicates they will replace the dict passed in the function):


In[]: ex_1_dict = {1:'dict',2:'ex',3:'one'}; ex_2_dict = {4:'haha',5:69,6:'dodding'}
In[]: ex_1_dict.update(ex_2_dict) 
In[]: print(ex_1_dict)
Out[]: {1: 'dict', 2: 'ex', 3: 'one', 4: 'haha', 5: 69, 6: 'dodding'}

Convert two lists to a dictionary:


In[]: keys_2 = ['year','name','subject','score'] ; values_2 = [1999,'Andrew','calculus',80]
In[]: d_ex_2 = dict(zip(keys_2,values_2)) ; print(d_ex_2)
Out[]: {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}

Apply an operator to a dictionary value:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: d_ex_2['score'] + 10
Out[]: 90

Truth value of whether a value is in a dictionary:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: 'score' in d_ex_2
Out[]: True
In[]: 'age' in d_ex_2
Out[]: False
#Reversed 
In[]: 'score' not in d_ex_2
Out[]: False
In[]: 'age' not in d_ex_2
Out[]: True

Delete a dictionary key value:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: del d_ex_2['name'] ; print(d_ex_2)
Out[]: {'year': 1999, 'subject': 'calculus', 'score': 80}

Iterate through a dictionary:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: for k,v in d_ex_2.items():
            print(k,':',v)
Out[]: 
year : 1999
name : Andrew
subject : calculus
score : 80

Dictionary comprehension to generate a dictionary:


In[]: {x: x+1 for x in (1,2,3)}
Out[]: {1:2, 2:3, 3:4}

Return the min and max values from a key in dictionary:


In[]: multi_val_dict_ex = {'names':['jon','juan','sam'],
                           'ages':[55,66,32],
                           'scores':[45,100,90]}
In[]: print(max(multi_val_dict_ex['scores']), min(multi_val_dict_ex['scores']) , sep='\n')
Out[]: 
100
45

Filter key:values with key in dictionary:


In[]: alpha_dict = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 
                    8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 
                    15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 
                    22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

In[]: even_alpha_dict = dict()
In[]: for (k,v) in alpha_dict.items():  #for loop and if statement pull even key values
            if k % 2 == 0: 
                even_alpha_dict[k] = v
In[]: print(even_alpha_dict)
Out[]: {2: 'b', 4: 'd', 6: 'f', 8: 'h', 10: 'j', 12: 'l', 14: 'n', 16: 'p', 18: 'r', 20: 't', 22: 'v', 24: 'x', 26: 'z'}

In[]: odd_alpha_dict = dict()
In[]: for (k,v) in alpha_dict.items():   #for loop and if statement pull odd key values
            if k % 2 != 0: 
                odd_alpha_dict[k] = v
In[]: print(odd_alpha_dict)
Out[]: {1: 'a', 3: 'c', 5: 'e', 7: 'g', 9: 'i', 11: 'k', 13: 'm', 15: 'o', 17: 'q', 19: 's', 21: 'u', 23: 'w', 25: 'y'}

In[]: divisible_by_ten_dict = dict()
In[]: for (k,v) in alpha_dict.items():
            if k % 10 == 0: 
                divisible_by_ten_dict[k] = v
In[]: print(divisible_by_ten_dict)
Out[]: {10: 'j', 20: 't'}

Filter dictionary keys and values with lambda:


In[]: var_str_dict = {0: 'The', 1: 'small', 2: 'brown', 3: 'fox', 4: 'jumped', 
                      5: 'over', 6: 'the', 7: 'lazy', 8: 'dog'}

Out[]: even_var_str_dict = dict(filter(lambda item: item[0] % 2 == 0, var_str_dict.items()))
In[]: print(even_var_str_dict)
Out[]: {0: 'The', 2: 'brown', 4: 'jumped', 6: 'the', 8: 'dog'}

Pass values in dictionary through a variable input (*argv) function (output format needs work):


In[]: def wage_funct(*argv):
          shift_dif = {'1st':1,'2nd':1.1,'3rd':1.25,'4th':1.35}
          hour_wage = []
          for k,v in shift_dif.items():
              for i2 in argv:
                  hour_wage.append(float(v)*i2)
          return print(hour_wage,end=', ')

In[]: wage_funct(1,2,3,4,5)
Out[]: [1.0, 2.0, 3.0, 4.0, 5.0, 1.1, 2.2, 3.3000000000000003, 4.4, 5.5, 1.25, 2.5, 3.75, 5.0, 6.25, 1.35, 2.7, 4.050000000000001, 5.4, 6.75], 

Dictionary comprehension:


In[]: dict_1 = {'names':['jon','juan','sam'],
                'ages':[55,66,32],
                'scores':[45,100,90]}

#generate a dictionary of squared vals from a range:
In[]: nums = range(1,21)
In[]: gen_sqrs = {n:n**2 for n in nums}

#Returns dict of squared values greater than 90 from gen_sqrs dict:
In[]: sqrs_gr_thn_90 = {k:v for (k,v) in gen_sqrs.items() if v > 90}

#add as many if statements as needed
In[]: true_cond = {k:('True' if v%2 == 0 else 'False') for (k,v) in gen_sqrs.items()}

Pring a dictionary as a table:


    In[]: multi_val_dict_ex = {'names':['jon','juan','sam'],
    'ages':[55,66,32],
    'scores':[45,100,90]}
In[]: for k,v in multi_val_dict_ex.items():
names, ages, scores = v
print ("{:<10} {:<10} {:<10}".format(names, ages, scores))
Out[]: 
jon        juan       sam       
55         66         32        
45         100        90      

In[]: for k,v in multi_val_dict_ex.items():
names, ages, scores = v
print (k, ' : ', "{:<10} {:<10} {:<10}".format(names, ages, scores))
Out[]: 
names  :  jon        juan       sam       
ages  :  55         66         32        
scores  :  45         100        90  




















Access and manipulate data by specific index of dictionary:


In[]: multi_val_dict_ex = {'names':['jon','juan','sam'],
                           'ages':[55,66,32],
                           'scores':[45,100,90]}
In[]: multi_val_dict_ex['scores'][1]*2   #multiplies index[1] of 'scores' by 2
Out[]: 200

Create a dictionary from a range {dictionary comprehension}:


In[]: nums = range(1,11)
In[]: gen_dict_1 = {n:n**2 for n in nums} ; print(gen_dict_1)  #returns the **2 of every n in nums
Out[]: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

In[]: gen_dict_2 = {n:n**2 for n in nums if n%2 == 0} ; print(gen_dict_2) #adds if condition for even nums
Out[]: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}

Access a dictionary value:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: d_ex_2['year']
Out[]: 1999

Return dictionary keys as a list:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: list(d_ex_2)
Out[]: ['year', 'name', 'subject', 'score']
#alt sorted alphanumerically
In[]: sorted(d_ex_2)
Out[]: ['name', 'score', 'subject', 'year']

Construct a dictionary from a list of tuple key:value pairs:


In[]: d_ex_2 = {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}
In[]: lst_of_tup = [('year', 1999), ('name', 'Andrew'), ('subject', 'calculus'), ('score', 80)]
In[]: dict(lst_of_tup)
Out[]: {'year': 1999, 'name': 'Andrew', 'subject': 'calculus', 'score': 80}

Create a dictionary from keyword args:


In[]: dict(year=1999, name='andrew', subject='calculus', score=80)
Out[]: {'year':1999, 'name':'andrew', 'subject':'calculus', 'score':80}

Add and remove a value to a key:


In[]: multi_val_dict_ex = {'names':['jon','juan','sam'],
                           'ages':[55,66,32],
                           'scores':[45,100,90]}
In[]: multi_val_dict_ex['names'].append('andrew') ; print(multi_val_dict_ex)
Out[]: {'names': ['jon', 'juan', 'sam', 'andrew'], 'ages': [55, 66, 32], 'score': [45, 100, 90]}

#Remove data with del
In[]: del multi_val_dict_ex['names'][2:4]
In[]: print(multi_val_dict_ex)
Out[]: {'names': ['jon', 'juan'], 'ages': [55, 66, 32], 'scores': [45, 100, 90]}

Access a nested dictionaries values:


In[]: people = people = {1: {'name': 'John', 'age': '27', 'answer': 1},
                         2: {'name': 'Marie', 'age': '22', 'anser': 5}}
In[]: people[3] = {}

In[]: people[3]['name'] = 'Luna'
In[]: people[3]['age'] = '24'
In[]: people[3]['answer'] = 6
In[]: 
In[]: print(people[3])
Out[]: {'name': 'Luna', 'age': '24', 'answer': 6}
Access a nested dictionary data: 
In[]: people[1]
Out[]: {'name': 'John', 'age': '27', 'answer': 1}
In[]: people[2]['name']
Out[]: 'Marie' 
In[]: people[2]['name'][0]
Out[]: 'M'

Filter key:values in a dictionary by values:


In[]: var_str_dict = {0: 'The', 1: 'small', 2: 'brown', 3: 'fox', 
                      4: 'jumped', 5: 'over', 6: 'the', 7: 'lazy', 8: 'dog'}

In[]: for k,v in var_str_dict.items():
          if len(v) > 3:
             print(v, end=' ')
Out[]: small brown jumped over lazy

Dictionary values greater than 3 to a new dictionary: 
In[]: word_values_greater_than_three = dict()
In[]: for k,v in var_str_dict.items():
          if len(v) > 3:
             word_values_greater_than_three[k] = v

In[]: print(word_values_greater_than_three)
Out[]: {1: 'small', 2: 'brown', 4: 'jumped', 5: 'over', 7: 'lazy'}

Filter current dictionary based on values by dictionary comprehension: 
In[]: var_str_dict = {k:v for k, v in var_str_dict.items() if len(v) > 3}
In[]: print(var_str_dict)
Out[]: {1: 'small', 2: 'brown', 4: 'jumped', 5: 'over', 7: 'lazy'}

In[]: var_str_dict = {k:v for k, v in var_str_dict.items() if v == 'dog'}
In[]: print(var_str_dict)
Out[]: {8: 'dog'}

Filter a dictionary using dictionary comprehension:


In[]: var_str_dict = {0: 'The', 1: 'small', 2: 'brown', 3: 'fox', 
                      4: 'jumped', 5: 'over', 6: 'the', 7: 'lazy', 8: 'dog'}
    
In[]: odd_var_str_dict = {key:value for (key,value) in var_str_dict.items() if key % 2 != 0} #pulls odd val keys
In[]: print(odd_var_str_dict)
Out[]: {1: 'small', 3: 'fox', 5: 'over', 7: 'lazy'}

Dictionary comprehension (Con't):


In[]: dict_1 = {'names':['jon','juan','sam'],
                'ages':[55,66,32],
                'scores':[45,100,90]}

#generate a dictionary of squared vals from a range:
In[]: nums = range(1,21)
In[]: gen_sqrs = {n:n**2 for n in nums}

#Returns dict of squared values greater than 90 from gen_sqrs dict:
In[]: sqrs_gr_thn_90 = {k:v for (k,v) in gen_sqrs.items() if v > 90}

Dictionary comprehension (Con't):


#With multi-conditions in comp:
In[]: multi_conditions = {k:v for (k,v) in gen_sqrs.items() if v > 150 if v < 350} #add as many if statements as needed

#add as many if statements as needed
true_cond = {k:('True' if v%2 == 0 else 'False') for (k,v) in gen_sqrs.items()}

Print a dictionary as a table:


In[]: multi_val_dict_ex = {'names':['jon','juan','sam'],
                           'ages':[55,66,32],
                           'scores':[45,100,90]}
In[]: for each_row in zip(*([i] + (j)  
                      for i, j in multi_val_dict_ex.items())): 
          print(*each_row, sep= '   ')

Out[]: 
names   ages   scores
jon   55   45
juan   66   100
sam   32   90


Sets

Sets are another datatype that allows you to store multiple data in one object. The rules for sets are as follows:
1. no duplicate items
2. do not have an index
3. are unordered
4. items within sets cannot be changed unless the set is converted to a list or some other datatype
5. sets can only have items added

Convert a list to a set (drops duplicates):


In[]: set_1 = set([1,2,2,1,5,6,9,9,8,7,7]); print(set_1)
Out[]: {1, 2, 5, 6, 7, 8, 9}
#alternatively
In[]: list_1 = ['the','the','dog','dog','pooped','pooped']
In[]: new_set = set(list_1); print(new_set)
Out[]: {'dog', 'the', 'pooped'}

Filter a set with comprehension:


In[]: str_list_2 = ['the','rain','in','spain','stays','mainly','in','the','plain']
In[]: str_set_2 = set(str_list_2); print(str_set_2)
Out[]: {'mainly', 'the', 'rain', 'plain', 'spain', 'in', 'stays'}

In[]: filter_list = ['spain','rain']

In[]: set_filtered_1 = {x for x in str_set_2 if x not in filter_list}; print(set_filtered_1)
Out[]: {'mainly', 'the', 'plain', 'in', 'stays'}

In[]: set_filtered_2 = {x for x in str_set_2 if x in filter_list}; print(set_filtered_2)
Out[]: {'spain', 'rain'}















Append data to a set with add():


In[]: ex_set = {'bad', 'birds', 'cats', 'good', 'zebra'}
In[]: ex_set.add('zebra') ; print(ex_set)
Out[]: {'good', 'birds', 'zebra', 'cats', 'bad'}

Set operations:


In[]: a = set('aabbccddddd') ; print(a)
Out[]: {'b', 'd', 'a', 'c'}
In[]: b = set('aabbczz') ; print(b)
Out[]: {'z', 'b', 'c', 'a'}

In[]: a - b  # letters in a but not in b
Out[]: {'d'}
    
In[]: a | b  # letters in a or b or both
Out[]: {'a', 'b', 'c', 'd', 'z'}

In[]: a & b  # letters in both a and b
Out[]: {'a', 'b', 'c'}

In[]: a ^ b  # letters in a or b but not both
Out[]: {'d', 'z'}


Tuples

Tuples are also used to store multiple items in one variable object. The rules for tuples:
1. odered (they have an index)
2. unchangeable (immutable)


A simple tuple:


In[]: ex_tup = ('this','is','a','tuple')
In[]: print(ex_tup[0]) ; ex_tup[3]
this
Out[]: 'tuple'

Convert a list of tuples to a dictionary:


In[]: lst_of_tup = [('year', 1999,1890,1776), ('name', 'Andrew','jimmy'), ('subject', 'calculus'), ('score', 80)]
In[]: d_from_tup = {t[0]: t[1:] for t in lst_of_tup} ; print(d_from_tup)
Out[]: {'year': (1999, 1890, 1776), 'name': ('Andrew', 'jimmy'), 'subject': ('calculus',), 'score': (80,)}











Tuple with nested lists:


Tuple of multiple lists:
In[]: ex_tup_lst = ([1, 2, 3], [10, 11, 12])


Loops and Conditionals

Loops are useful for doing something a known (definite iteration) or unknown (infinite iteration) number of times. Mostly you use a "for loop" if you know the number of times you need to repeat some process (iteration).

Loop basics:


'''
Think of a for loop like telling the 
interpreter to do somthing for every 
item in something else: 

for EachItem in Something: 
    DoTheseStatements
'''
#say "Something" in the above is a range() 
#range(5) here means DoTheseStatements 5 times. 
In[]: for EachItem in range(5): 
          print('dogs')
Out[]: 
dogs
dogs
dogs
dogs
dogs

'''
We can also access "EachItem" with the loop too. 
'''
In[]: for EachItem in range(5): 
          print(EachItem)
Out[]: 
0
1
2
3
4

'''
This can also be done to 
'''
In[]: 

Simple range for loop where range(lowerbound, upperbound, stepsize) :


In[]: for x in range(50,100,10): 
          print(x, end= ' ') 
Out[]: 50 60 70 80 90

For loop to access data in a list:


In[]: data = ['Algeria', 'Brazil','China','Dominican Republic']
In[]: for x in data: 
            print(x, end = ' ') 
Out[]: Algeria Brazil China Dominican Republic

Chained conditionals:


choice = 'z'

if choice == 'a':
    print("You chose 'a'.")
elif choice == 'b':
    print("You chose 'b'.")
elif choice == 'c':
    print("You chose 'c'.")
else:
    print("Invalid choice.")

A note on while loops:


'''
Knowing where to apply loops is important. 
Generally, a for loop may be used when you 
know the amount of iterations you will be executing. 
This is known as definite iteration. 

Otherwise if the amount of iterations is unknown you apply
a while loop. This is indefinite iteration. 
'''











Basic syntax of a conditional loop:


'''
If SomeCondition == True: 
     DoTheStatementsHere
Else: 
     DoTheStatementsHere
'''
In[]: x = 9
In[]: if x == 9: 
         print('x is equal to nine')
      else: 
         print('x is not equal to nine')

Out[]: 'x is equal to nine'

Simple nested for loop (the inner loop gets executed for each iteration of the outer loop):


In[]: Vars1 = ['A','B','C']
In[]: Vars2 = ['X','Y','Z']
In[]: for x in Vars1: 
            for y in Vars2: 
                print(x,y)
Out[]: 
A X
A Y
A Z
B X
B Y
B Z
C X
C Y
C Z

A nested conditional:


In[]: x = 69
In[]: if x > 10:
         print(" x is above ten.")
         if x > 20:
            print("And also above 20!")
            print('x is equal to', x,'.')
         else:
            print("but not above 20.")

Out[]:  'x is above ten.'
        'And also above 20!'
        'x is equal to 69'


Functions

Functions are one of the most useful tools anyone will use. At minimum you will use a function whenever you have code that you need to use over and over to accomplish something. This may be a little confusing at first because there are a handful of methods in which to write and call a function.

If you went through some of the sections above then you likely noticed that, in some code blocks, I used functions to accomplish something. Regardless I'm putting some more information on functions here.

Basic function that takes no arguments (parameters) syntax:


In[]: def SomeFunctionName():
          '''
          This section is where you put a doc string. 
          '''
          something = "something for the function to do"  #creates local string obj
          return something #something for the function to return

In[]: SomeFunctionName()    #calls the function
Out[]: 'something for the function to do'

A function that does something and takes one argument (parameter).


In[]: def funct_1(arg_1): 
          '''
          Pass an integer or float and this funct adds 5.
          '''
          arg_1 = arg_1 + 5
          return arg_1

In[]: funct_1(64)
Out[]: 69

Simple nested function:


In[]: def f1(num):
          #this is an outer funct
          print('Outer funct data that was passed: ', num)

          def f2():
              #this is the nested funct
              print('Inner funct data passed: ', num + 5)
    
          return f2()

In[]: f1(4)
Out[]: 
Outer funct data that was passed:  4
Inner funct data passed:  9

A note on nested functions:


'''
A function inside another function is a 'nested function'. 
Nested functions can access variables in the local scope of 
the outer function. 
'''

















Function that finds the quotient of two integers or floats (two arguments):


In[]: def funct_2(arg_x, arg_y): 
          '''
          Input two integers or floats; arg_y cannot be zero.
          '''
          quotient = arg_x / arg_y 
          return quotient

In[]: functExample(10,2) 
Out[]: 5.0 

Function that returns a modified global variable (if you can avoid using a global var then do so)


#This is actually an example of a really bad way to write a function. 
#Just don't declare globals in funct if you can avoid it.
In[]: x = 10  #assigns global var
In[]: def functExam2(): 
          global x   #declares a global var inside the local scope of the funct
          x = x - 9  #modifies global inside local scope funct
          return x   #returns a global x

In[]: functExam2() 
Out[]: 1 
In[]: x 
Out[]: 1 

Function used for assigning a variable:


In[]: def funct_1(arg_1): 
          '''
          Pass an integer or float and this funct adds 5.
          '''
          arg_1 = arg_1 + 5
          return arg_1

In[]: a_int_var = funct_1(10)
In[]: a_int_var
Out[]: 15

Function that returns shift differentials given an hourly wage.


In[]: def wage_funct(*argv):
          '''
          Function that returns wage differentials per shift per hourly wage.
          Pass a int or float houly wage and returns applied shift differentials. 
          '''    
          shift_dif = {'1st':1,'2nd':1.1,'3rd':1.25,'4th':1.35}
          applied_differential = []
          for k,v in shift_dif.items():
               for i2 in argv:
                    applied_differential.append(float(v)*i2)
          applied_differential = [round(i,2) for i in applied_differential]
          return applied_differential

In[]: hourly_wage = 15
In[]: wage_funct(hourly_wage)
Out[]: [15.0, 16.5, 18.75, 20.25]


Custom Exceptions

Custom exceptions can be very useful when you need to define your own errors.

A built in exception looks like this:


In[]: def add_3(num):
          return num + 3
In[]: add_3(60)
Out[]: 63

In[]: add_3('This is not a number')
Out[]: 
Traceback (most recent call last):

  File "<ipython-input-7-1e7035109e4c>", line 1, in <module>
    add_3("st")

  File "Path/to/SomeFile.txt", line 20, in add_3
    return num + 9

TypeError: can only concatenate str (not "int") to str
#this is basically telling you that the string cannot be added to an int 
#this is a raised exception









Here is a custom exception:


In[]: def add_3(num):
          try: 
             num = num + 9
             return num
          except TypeError:
             print('The argument "num" must be an integer or float... maybe try again')
             pass
In[]: add_9(6)
Out[]: 15

In[]: add_0('oops')
The argument "num" must be an integer or float... maybe try again


Class Objects

When you have a lot of data with the same attributes you can use a class object. Things like users or vehicles or whatever.

Class example and some methods:


In[]: class ex_class_2:
            '''
            Example class 2
            '''
            def __init__(self):
                self.attr_1 = []
                self.attr_2 = 10
                self.attr_3 = []
        
            def funct_1(self):
                return print('This class function returns a print statement')

#Call a class to an obj:
In[]: call_class = ex_class_2()    #call_class is the class obj

#Print all class attributes to dict: 
In[]: call_class.__dict__
Out[]: {'attr_1': [], 'attr_2': 10, 'attr_3': []}

#Check for attribute in class: 
In[]: print(hasattr(call_class,'attr_name_here'))
Out[]: False

#Set (add) attribute to class obj: 
In[]: setattr(call_class,'new_attr_added',[])
In[]: call_class.__dict__
Out[]: {'attr_1': [], 'attr_2': 10, 'attr_3': [], 'new_attr_added': []}

#Return value of attribute: 
In[]: print(getattr(call_class,'attr_2'))
Out[]: 10

#Delete an attribute: 
In[]: delattr(call_class,'attr_1')
In[]: call_class.__dict__
Out[]: {'attr_2': 10, 'attr_3': [], 'new_attr_added': []}










Another class example and methods:


##Class Example 2##
In[]: class ex_class_2:
            '''
            Example class 2
            '''
            def __init__(self):
                self.attr_1 = []      #creates an empty list for each instance of class
                self.attr_2 = []      #creates an empty list for each instance of class
                self.attr_3 = []      #creates an empty list for each instance of class
        
            def funct_1(self):
                return print('This class function returns a print statement')
#call class by assigning a variable
In[]: call_2 = ex_class_2()
#call class attribute 1,2,3 
In[]: call_2.attr_1
Out[]: []
In[]: call_2.attr_2
Out[]: []
In[]: call_2.attr_3
Out[]: []

#call the data type of class attribute 
In[]: type(call_2.attr_3)
Out[]: list

#append a number to class attribute that is a list
In[]: call_2.attr_3.append(4)
In[]: call_2.attr_3
Out[]: [4]

It can be useful to call the dir of a class:


In[]: class ex_class_2:
'''
Example class 2
'''
def __init__(self):
    self.attr_1 = []
    self.attr_2 = 10
    self.attr_3 = []

def funct_1(self):
    return print('This class function returns a print statement')
In[]: call_class = ex_class_2()    #call_class is the class obj
In[]: dir(call_class)
Out[]: 
['__class__',          #most of these are built-in class methods
'__delattr__',     
'__dict__',       
'__dir__',          
'__doc__',          
'__eq__',          
'__format__',         
'__ge__',        
'__getattribute__',       
'__gt__',       
'__hash__',      
'__init__',       
'__init_subclass__',    
'__le__',       
'__lt__',       
'__module__',  
'__ne__',          
'__new__',          
'__reduce__',          
'__reduce_ex__',          
'__repr__',          
'__setattr__',          
'__sizeof__',          
'__str__',          
'__subclasshook__',          
'__weakref__',          
'attr_1',          #class attribute
'attr_2',          #class attribute
'attr_3',          #class attribute
'funct_1']          #class function