<jupyter_start><jupyter_text>Python Programming  Summer Workshop 2020 LSU HPC, LBRN & LONI  <NAME>   <EMAIL>  Topics Covered * [What is Python](intro) * [Python programming basics](data-type) * [Control structures and functions](control) * [Python modules](module) * [Numpy](numpy) * [Plotting](plot)  What is Python? * A general-purpose programming language (1980) by <NAME>     * https://gvanrossum.github.io * Intuitive and minimal coding * Objected-Oriented * Interpreted not compiled * Interactive * Dynamically typed, no type declarations, data type is tracked at runtime * Automatic memory management * Spacing defines blocks of code such as control structures and blocks    Why  Python? Advantages* Ease of programming * Minimal time to develop and maintain codes * Modular and object-oriented * Large standard and user-contributed librariea * Large user community  Disadvantages * Interpreted and therefore slower than compiled languages * Not great for 3D graphic applications requiring intensive computations    What is IPython? IPython: an interactive command shell for Python (2001) by <NAME>* Enhanced Read-Eval-Print Loop (REPL) environment* Command tab-completion, color-highlighted error messages..* Basic Linux shell integration (cp, ls, rm…)* Great for plotting!* More information click _[ipython.org](http://ipython.org)_  Jupyter Notebook * Web interface to Python, introduced in 2015 * Rich text, improved graphical capabilities* Integrate many existing web libraries for data visualization* Allow to create and share documents that contain live code, equations, visualizations and explanatory text. * Interface with over 40 languages, such as R, Julia and Scala* This colab.research.google.com notebook is a Jupyter notebook[back to top](top)  Variables * Variable type is dynamically determined from the value it is assigned, no data type is declared* Assign meaningful variable names* Some keywords are reserved such as ‘print’, ‘assert’,  ‘while’, ‘lambda’, for, if. * A variable is assigned a value using the ‘=’ operator, example:   x = 3 which is read as "the variable x is assigned the value of 3"* Reference of [keywords](https://docs.python.org/2.5/ref/keywords.html)  Operators * Arithmetic operators +, -, *, /, // (integer division for  floating point numbers), ’**’ power* Boolean operators and, or and not* Comparison operators >, = (greater or equal), <=* (less or equal), == equality **bold text** Built-in Data Types   * [Number](numbers)* [String](string)* [List](list)* [Tuple](tuple)* [Dictionary](dic)* [File](file)      [top](top)  Numbers<jupyter_code>x = 3
type(x)
y = 3.0
type (y)
z='hello'
type(z)
int(3.3)
complex(3)
import math
abs(-3.3)
math.sin(60)
math.sqrt(25)<jupyter_output><empty_output><jupyter_text>[top](top)    Strings<jupyter_code>my_str = 'Hello World'
print(my_str); len(my_str)
len(my_str); print(len(my_str))
#indexing starting at 0
print(my_str[0]); print(my_str[1]); print(my_str[10])
#string slicing
print(my_str[1:4]); print(my_str[:]); print(my_str[1:])
#string split
s1, s2 = my_str.split(); print(s1); print(s2)
print(s1, s2)
#multiple line spanning
multilines= """This is a multi-line
block of sample
 text"""
print(multilines)

#string formatting
text = "%d pigs in the barn, I’ll %s and %s" %(300,'huff','puff')
print(text)
total= "my share = %.5f, tip = %d" %(24.5, 5.5)
print(total)
#more string ops
my_str="Hello World"
print(my_str.upper())
print(my_str.isalpha())
print(my_str[0].isalpha())
a='12'
print(a.isdigit())<jupyter_output><empty_output><jupyter_text>[top](top)   Control Structures * if-else* for loops, while loops* break: jump out of the current loop* continue: jump to the top of next cycle within the loop* pass: do nothing  [top](top)<jupyter_code>x = 500 
if ( x >= 5 ):
  print(x,">= 5")
  print("Hi, greater than 5")
else:
    print(x, "< 5")
    

for num in range(3):
    print(num)
index=0
while (index < 3):
    print(index)
    index = index + 2<jupyter_output><empty_output><jupyter_text>Tip:Indentation: signifies code blocks<jupyter_code>#prime numbers
n = 2
while n < 10:
    prime = True
    for x in range(2,n):
        if n % x == 0:
            prime = False
            break
    if prime:
        print(n, 'is a prime #  ')
        #pass
        #else:
         #   n= n + 1
         #   continue
    n = n + 1
#Loops:  looping through a string
# 'for' and 'in' are new keywords
for x in "banana":
  print(x)
for x in range(6):
  print(x)
#range and increment by 3
for x in range(2, 30, 3):
  print(x)<jupyter_output><empty_output><jupyter_text>Lists* Collection of data [ ]* Often used to store homogeneous values    * e.g., Numbers, names with the same data type* Members are accessed as strings* Mutable: modify in place without creating a new object<jupyter_code>my_list = [1,2,9,4]
print(my_list)
#how to find out the data type?
type(my_list)
my_list2 = list(range(-4,10, 4))
print(my_list2)
# access list members by index 
# start at 0, end at n-1
print("1st element =", my_list[0])
print("last element =", my_list[3])
#Concatenate lists
print(my_list + my_list)
print(my_list *2)
print(my_list *3)
# iterate list elements
animals = ['dog','cat','horse']
for name in animals:
    print(name)
# enumarate
cars = ['Ford','Chevy','Toyota']
print(cars)
cars2 = enumerate(cars)
print(list(cars2))

for index, name in enumerate(cars):
    print(index+1, name)
# modify list
del my_list[0]; print(my_list)
my_list.append(99); print(my_list)
my_list.insert(1, 100); print(my_list)
#some list fun

my_list.sort(); print(my_list)
my_list.reverse(); print(my_list)
#more sorting
my_list = ['cats', 'SHEEP','DOGS','horses']
print(sorted(my_list, key=len))
print(sorted(my_list))
print(sorted(my_list, reverse=True))
sorted(my_list, key=len)
# sort by lowercase
sorted(my_list, key=str.lower)
#list comprehension [expr for var in list]
my_list=[2,5,9,1]
square = [x**2 for x in my_list]; print("square =", square)
large = [n for n in my_list if n >= 5]; print('large =',large)<jupyter_output><empty_output><jupyter_text>[top](top)   Tuples  * Why Tuple?  * Processed faster than lists  * Sequence of a Tuple is protected  * Sequence unpackingSuprising Trait:Python tuples are immutable, but their values may change. This may happen when a tuple holds a reference to any mutable object, such as a list. ... It's clear that dum and dee refer to objects that are equal, but not to the same object.( [ On immutable but changing tuples](http://radar.oreilly.com/2014/10/python-tuples-immutable-but-potentially-changing.html))[Tutorial just on Python Tuples](https://www.tutorialspoint.com/python/python_tuples.htm)<jupyter_code>#empty tuples
my_tup = ()
print(my_tup)
# you have to include a comma, even though there is only one value you have to include a comma, even though there is only one value
tup1 = (50,);
print(tup1)
my_tup = (2, 8, 3, 1)
print(my_tup)
a,b,c,d = my_tup
print(a, b, c, d)

print(my_tup[0])
#Spoiler:  look for error before running?
tup = ('physics', 'chemistry', 1997, 2000);
print(tup);
del tup;
print("After deleting tup : ");
print(tup);<jupyter_output><empty_output><jupyter_text>Immutable?  Not?<jupyter_code>dum = ('1861-10-23', ['poetry', 'pretend-fight'])
dee = ('1861-10-23', ['poetry', 'pretend-fight'])

print(dum == dee)

print(dum is dee)

id(dum), id(dee)
t_doom = dum
print(dum, t_doom)

print(t_doom == dum)
print(t_doom is dum)
id(dum), id(t_doom)
skills = t_doom[1]
print(skills)

skills.append('rap')
print(t_doom)

print(dum)


print(dum == dee)<jupyter_output><empty_output><jupyter_text>[top](top)   Dictionaries* List of key-value pairs { }* Unordered collections of objects, not indexed* Store objects in a random order to provide faster lookup* Data type are heterogeneous, unlike list* Element are accessed by a keyword, not index* Elements are mutable * dict = {"key1”: value1,  “key2”: value2} [Tutorialspoint.com  on  Dictionaries](https://www.tutorialspoint.com/python/python_dictionary.htm)<jupyter_code>my_dict = {'cats': 4, 'dogs':2, 'sheep':3}
print(my_dict)
print(my_dict["cats"])
my_dict['horse'] = 3; print(sorted(my_dict))
print(my_dict.keys()); print(my_dict.values())
print(sorted(my_dict.values()))

for key, value in my_dict.items():
    print(key, value)<jupyter_output>cats 4
dogs 2
sheep 3
horse 3<jupyter_text>[top](top)   Files* file open, close, read, write* file_handle = open(“file_name”, ‘mode’)    * Modes:     * a: append    * r: read only (error if not existing)    * w: write only    * r+: read/write (error if not existing)    * w+: read/write    * b: binary* Python has a built-in garbage collector* Object memory space is auto reclaimed once a file is no longer in use<jupyter_code>if [ ! -f "data.txt" ]; then wget 'https:/! /raw.githubusercontent.com/lsuhpchelp/lbrnloniworkshop2018/master/day2_python/data.txt'; else echo 'file exists'; fi;
input = open('data.txt', 'r')
content = input.read()
input.close()
print(content)
#write to a file
output = open('write.txt','w')
output.write("Writing to a file")
output.close()
!ls -l
! cat write.txt<jupyter_output>total 12
-rw-r--r-- 1 root root  630 Jun  1 16:25 data.txt
drwxr-xr-x 1 root root 4096 May 27 16:27 sample_data
-rw-r--r-- 1 root root   17 Jun  1 16:33 write.txt
Writing to a file<jupyter_text>[top](top)    Functionsdef func_name(par1, par2,...): > body of code  [top](top)<jupyter_code>#addition
def my_add(x, y):
    return x+y

sum=my_add(3, 8); print(sum)
#return multiple values
def power(input):
    return input**2, input**3

square, cube = power(3)
print("square =",square, "cube =",cube)<jupyter_output>square = 9 cube = 27<jupyter_text>Python Module* A python script with python functions and statements* Import a module before having access to all the functions within* Most Python distributions come with plenty of build-in modules ***  Python Modules* math, sys, os..* NumPy, high performance in vector & matrix using vector computation * SciPy: based on Numpy, including many scientific algorithms.* padas* matplotlib, pyplot, pylabReference to [Python3.x standard libraries](https://docs.python.org/3/library/)<jupyter_code>#import a module
import math
math.sin(math.pi)
#get information of a module
dir(math)
#get information
help(math)
# get information of a module function 
help(range)<jupyter_output>Help on class range in module builtins:

class range(object)
 |  range(stop) -> range object
 |  range(start, stop[, step]) -> range object
 |  
 |  Return an object that produces a sequence of integers from start (inclusive)
 |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 |  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
 |  These are exactly the valid indices for a list of 4 elements.
 |  When step is given, it specifies the increment (or decrement).
 |  
 |  Methods defined here:
 |  
 |  __bool__(self, /)
 |      self != 0
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |[...]<jupyter_text>* Examples using os, commands modules * os: includes many functions interacting with the file system<jupyter_code>import os
!ls 
os.listdir()


def display_dir(dir):
      files = os.listdir()
      for fname in files:
        print(fname)
        print(os.path.abspath(os.path.join(dir, fname))) 

display_dir('/tmp')<jupyter_output>.config
/tmp/.config
data.txt
/tmp/data.txt
write.txt
/tmp/write.txt
sample_data
/tmp/sample_data<jupyter_text>[top](top)  Data processing<jupyter_code># Plot a bar chart
import matplotlib.pyplot as plt


xlabeles = ['January','February','March','April']
xticks = [i for i in range(len(xlabeles))]
y = [ 34, 17, 13, 69]
e = [5.2, 10., 1.5, 12.]
plt.errorbar(xlabeles, y, yerr=e, fmt='d', capsize=10)
plt.bar(xlabeles, y, 0.5, color='red' )
plt.title("Sale by Month")
plt.xlabel('Month')
plt.ylabel('Sale')
plt.xticks(xticks, xlabeles)

plt.show()<jupyter_output><empty_output><jupyter_text>[top](top)  Examples:   Please solve the following problems:  (Feel free to ask for help) Write a line of code to print the sum of 999 and 1600<jupyter_code>999 + 1600<jupyter_output><empty_output><jupyter_text>Write a line of code to print out your "Hello Name" where Name is your name.<jupyter_code>print("Hello Kathy")<jupyter_output>Hello Kathy<jupyter_text>Write a code snippet to calculate the total of the shopping spree on Rodeo Dr. You bought:1 pair of shoes:  24001𝑝𝑎𝑖𝑟𝑜𝑓𝑠𝑙𝑎𝑐𝑘𝑠: 1500ruby ring: 22000State and local tax is 22%.You want to print out the untaxed total, the amount of tax and the total of tax and goods.<jupyter_code>#my_dict = {'cats': 4, 'dogs':2, 'sheep':3}
a_dict = {'shoes': 2400, 'slacks':1500, 'ring' : 22000}
print(a_dict)

total = sum(a_dict.values())
print(total)

taxes = 0.22 * total
print(taxes)

grand_total = tax + total
print(grand_total)<jupyter_output>{'shoes': 2400, 'slacks': 1500, 'ring': 22000}
25900
5698.0
31598.0<jupyter_text>Using the code you wrote above make the tax calculation a function block and the shopping total a function block<jupyter_code>def taxes(a_dict):
  return sum(a_dict.values()) * 0.22

def shop(a_dict):
  return sum(a_dict.values())

shopping = shop(a_dict)

tax = taxes(a_dict)

grand_total = shopping + tax

print("Taxes are ", tax)
print("Total of shopping is ", shopping)
print("The grand total of today's spree is ", grand_total)<jupyter_output>Taxes are  5698.0
Total of shopping is  25900
The grand total of today's spree is  31598.0<jupyter_text>**Create a leap year finder based on the following algorithm:Leap years occur according to the following formula: a leap year is divisible by four, but not by one hundred, unless it is divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1993 and 1900 are not. The next leap year that falls on a century will be 2400.Use the following list to check and see if your code is correct.  The list of leap years from 1900 to 2400 inclusive is:1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020<jupyter_code>year = 1905

if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
    print("%d is a Leap Year" %year)
else:
    print("%d is Not the Leap Year" %year)<jupyter_output>1905 is Not the Leap Year<jupyter_text>Using the code in the prime number example in the Control Structures section fix the code so it only prints out each prime number once.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12792427315753888, 'fraction_numerical': 0.03488843813387424, 'mean_word_length': 4.7285050348567, 'pattern_counts': {'":': 1, '<': 76, '<?xml version=': 0, '>': 81, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '353592', 'license': 'CC0-1.0', 'n_tokens_mistral': 5574, 'n_tokens_neox': 5042, 'n_words': 1684, 'path': 'day1/intro_python.ipynb', 'repo_name': 'lsuhpchelp/lbrnloniworkshop2020', 'size': '14828'}