## Python basics. ## Download Python from Python.org . ## It comes with IDLE. Open IDLE. ## An alternative is to get Anaconda, from https://www.anaconda.com/download-old , ## but you have to enter your email and and agree to receive communications and register, ## and they send you a link to download. ## First, from terminal, install packages. python3 -m pip install numpy python3 -m pip install faraway python3 -m pip install pandas python3 -m pip install scipy python3 -m pip install statsmodels python3 -m pip install matplotlib.pyplot python3 -m pip install seaborn ## Now do python3 or open Idle. ## If your download does not work, in chapter 0 of the book at https://automatetheboringstuff.com/2e/ ## it says how to figure out exactly whether you have a 64-bit or 32-bit OS. ## Once you have Python downloaded, you can go to terminal, and type python3 . ## In IDLE, I went to IDLE -> Preferences -> Keys and chose history-previous and history-next and set ## them to up arrow and down arrow. Good free introductions to Python are https://automatetheboringstuff.com/2e and https://realpython.com/python-statistics . Python is very inflexible when it comes to spacing and indentation. Python code runs a bit more stably and predictably in IDLE than in terminal. In IDLE you file -> open, then choose the file, and then select run -> run module. Also, it is unusual how you cannot end functions and for loops with brackets but instead need returns. 2+3 7*5 12 // 2.2 # just the integer part of the division. 12 % 2 # just the remainder. 4 ** 3 # exponents. the_world_is_flat = True if the_world_is_flat: print("Be careful not to fall off!") You need the indentation. += works as in C. x = 2 x += 1 ## 3. ## Python is great for text manipulation. ## single or doublequotes are used for strings. 'Alice' + 'Bob' x = "Alice" * 5 x = "rick" x[0] x[-1] ## last element "i" in x input() lets the user input a value. len() is the length, meaning number of characters in a string. str() converts a number into a string. ## But strings are immutable. You can't do x = "rick"; x[2] = "u". you have to instead do x = "rick"; newx = x[0:2] + "u" + x[3] # This program says hello and asks for my name. print('Hello, world!') print('What is your name?') myName = input() print('It is good to meet you, ' + myName) print('The length of your name is:') print(len(myName)) print('What is your age?') myAge = input() print('You will be ' + str(int(myAge) + 1) + ' in a year.') int(4.2) ## Just the integer part. There is also round() and float(). ## int('4') converts "4" into the integer 4, but int('4.2') doesn't. ## float() works nicely in both cases. float(4.2) float('4.2') Like in C, the comparison operators are ==, !=, >= etc. and, or, and not are logical. You don't need parentheses with if, in Python. if statement else: You have to have a colon and an indented block of code on the NEXT line. elif is only executed if the preceding thing was false. mynum = int(input()) if mynum < 5: print("Your number is small") elif mynum < 7: print("Your number is medium") elif mynum <= 8: print("Your number is large") else: print("Your number is huge") ## You can also do while loops or for loops, as with R. i = 1 while(i < 10): print("Your number is ", i,".\n") if i == 7: break i = i + 1 for i in range(10): print("Your number is",i,"\n") In a for loop, continue will pass to the next value of i. for i in range(10): if i == 5: continue print("Your number is",i,"\n") for i in range(10,3,-1): print("your number is ",i) import random, sys, os, math ## Modules print(random.randint(1, 10)) # This is a guess the number game. import random secretNumber = random.randint(1, 20) print('I am thinking of a number between 1 and 20.') # Ask the player to guess 6 times. for guessesTaken in range(1, 7): print('Take a guess.') guess = int(input()) if guess < secretNumber: print('Your guess is too low.') elif guess > secretNumber: print('Your guess is too high.') else: break # This condition is the correct guess! if guess == secretNumber: print('Good job! You guessed my number in ' + str(guessesTaken) + 'guesses!') else: print('Nope. The number I was thinking of was ' + str(secretNumber)) ## Functions. def mynumb(): print("What is your number?") i = input() print("Your number is ",i) mynumb() ## You need the return after the function for python to know it is over. ## You need () after def mynumb so it knows it is a function. If a function like print returns nothing, what it actually returns is the value None. Variables defined in functions are all local by default. To change a global variable within a function, do global x def f(): global y y = 3 ## y <<- 3 y = 2 f() y ## it will output 3. ## lists. x = [1,2,5] x[0] ## 1. x = [1,2,[10,11,12]] x[2][2] ## 12. x[-1] ## means the last element of x. x[0:2] ## means x[0] and x[1]. It does not include x[2]. This is called a slice. With lists, you can do + and *. [1, 2, 3] + ['A', 'B', 'C'] ## [1, 2, 3, 'A', 'B', 'C'] a = [1,2,8] * 4 ## [1,2,8,1,2,8,1,2,8,1,2,8] del a[2] ## removes the 3rd element of a. 4 in a 8 not in a a = [1,2,2] size, age, hair = a ## Methods. x = [1,2,1,5,6] x.index(5) ## first time it appears. x.append(5) ## makes it 1,2,1,5,6,5. x.insert(1,7) ## makes it 1,7,2,1,5,6,5. x.remove(1) ## removes the first instance. x.sort() x.reverse() ## reverses the order. ## These methods only work on lists. ## y = x.sort() doesn't work. Do x.sort(); y = x. Tuples. They are just like lists but have ( instead of [. And tuples are immutable. You can use tuple() or list() to convert an object to that type. LISTS ARE STORED AS REFERENCES! So if you do y = x, and x is a list, then y is actually a reference to the same list, and if you change y you will change x. This is not true with tuples though. x = [1,2,3,4,5] y = x y[2] = 12 x y x = (1,2,3,4,5) y = list(x) z = list(x) y[2] = 12 x y z ## still 1,2,3,4,5. ## More strings. x = "It\"s mine." ## \" = " inside quotes. \' = '. \t = tab. \n = newline. \\ = \. r makes something a raw string. Backslashes will appear. Useful for directories. x = r"Users\Desktop\computing" x ## this will have double slashes now. print(x) ## this will look correct. Multiline strings. Use """ to start and stop. Newlines, tabs, and even quotes are saved correctly! This is great. str(x) makes the number x into a string. ## % is useful. name = "rick"; age = 53 x = 'My name is %s. I am %s years old.' % (name, age) print(x) ## You can also use f before the quotes. x = f"My name is {name}. Next year I will be {age + 1}." x.upper() ## makes it all upper case! ## Basic statistical summaries. x = (1,5,3) mymean = sum(x)/len(x) ## or, use the statistics module import statistics statistics.mean(x) import statistics as st st.mean(x) ## weighted mean x = [1,2,5,50,5] w = [0.1, 0.2, 0.2, 0.3, 0.2] wmean = sum(w[i] * x[i] for i in range(len(x))) / sum(w) ## geometric mean statistics.geometric_mean(x) ## median statistics.median(x) ## Other summaries statistics.mode(x) statistics.variance(x) statistics.stdev(x) statistics.quantiles(x) ## Cor and regression in Python import scipy.stats x = (1,3,5) y = (1,7,2) r, p = scipy.stats.pearsonr(x, y) ## correlation and corresponding p-value scipy.stats.linregress(x, y) ## Visual summaries import numpy as np import matplotlib.pyplot as plt plt.style.use('ggplot') x = np.random.randn(200) ## 200 N(0,1) random variables. y = np.random.randn(200) z = np.random.randn(200) fig, ax = plt.subplots() z1 = ax.boxplot((x, y, z), vert=False, showmeans=True, meanline=True, labels=('x', 'y', 'z'), patch_artist=True, medianprops={'linewidth': 2, 'color': 'purple'}, meanprops={'linewidth': 2, 'color': 'red'}) plt.show() fig, ax = plt.subplots() hist, bin_edges = np.histogram(x, bins=10) ax.hist(x, bin_edges, cumulative=False) ax.set_xlabel('x') ax.set_ylabel('Frequency') plt.show() ## piechart x, y, z = 1, 3, 5 fig, ax = plt.subplots() ax.pie((x, y, z), labels=('x', 'y', 'z'), autopct='%1.1f%%') plt.show() ## Some simple things require packages that don't come with Python. ## Random normals import numpy as np x = np.random.randn(10) ## 10 N(0,1) random variables. x = [5,7,8,7,2,17,2,9,4,11,12,9,6] y = [99,86,87,88,111,86,103,87,94,78,77,85,86] plt.scatter(x, y) plt.show() ## For more, see Linear Models with Python by Julian Faraway. ## From the Preface, "If your sole objective is to do statistics, R is more attractive."