Enroll in the Data Scholar Canvas course here!
Data Scripting: Introduction to Python Jupyter Notebooks Using Google Colab
This workshop will cover the fundamentals of Python scripting using Jupyter Notebooks on Google's Colaboratory platform.
Participants will work through a series of exercises in the following:
(1) Take Workshops, (2) Pass Quizzes, (3) Become a Data Scholar
Interested in becoming a Data Scholar?
Takes only six workshops! |
Pick any Two Categories Below, Take at Least Two Workshops from Each of Those Categories: (Total of 4)
|
|
Pick any One Category Below, Take at Least Two Workshops from That Category:
(Total of 2)
|
* Becoming a Data Scholar is not mandatory. Take any workshop you like.
Head to Google Colaboratory
|
https://colab.research.google.com |
Click New Python 3 Notebook
If you do not have this popup, click File/New Python 3 Notebook |
|
Rename the Notebook to Howdy, World | |
In the first cell, type: print('Hello, World')
Then run the snippet |
|
Create a new Text snippet above our code snippet | |
Double-click the new text snippet. Jupyter Notebooks use a syntax called Markdown (here is a good guide)
Let's use Header 2 and type Learning Python Then a new line and type the date in bold.
Click in the code snippet to run the markdown |
|
Click Tools/Settings
Features to note:
|
Covered in this section:
Create a new text snippet below and using H2 format, type Programmer Practice
Then create a new code snippet blow that |
|
Note the difference between printing a variable in quotes as a string and a variable not in quotes. | |
Incorporate the following additions to number our lines |
Covered in this section:
Create a new Text snippet below
In H2, type Number Adder
Then create a Code snippet below |
|
Add 5 and 2 together | |
Change x and y to accept inputs from the user.
Run the snippet, but beware for any issues! |
|
Adjust so that x and y are numeric values | |
QUIZ: Adjust so that the values are divided. |
Covered in this section:
Create an entirely new Python 3 Notebook |
|
Name the Notebook Document Size Guesser
# My First Script Josh Been, 11/15/2019 |
|
Print three document choices and input for a selection | |
if: elif: elif: else: |
print('(1) Document 1') print('(2) Document 2') print('(3) Document 3') doc=input('Please select document: ') if doc=='1': print('Doc 1') elif doc=='2': print('Doc 2') elif doc=='3': print('Doc 3') else: print('Try again') |
Covered in this Section:
Download & Unzip Documents | https://researchguides.baylor.edu/ld.php?content_id=51097654 |
Upload to Colab - Click the little tab on the left - Click Files and upload the three files |
|
Read a File
# open read connection - change to 'w' to write - change to 'a' to append f=open('filename','r') # read file and pass to variable all_content=f.read() # write: f.write(string) # close read connection f.close() |
In a new code cell, read and print the contents of document 2
|
Count the number of line breaks.
Don't forget to add +1 to include the last line! |
|
In previous code cell (with the condition statement), replace the print(Doc #) with the four lines needed to read the file and count the number of lines.
If you would like, the next tab has code you can copy/paste |
|
Implement if and input statements to give a user two guesses
Try on your own! OR Copy/paste the code to the right |
print('(1) Document 1') first_guess=float(input('How many lines do you guess?')) |
Covered in this Section:
A function is a reusable piece of code that can repeatedly accept variables and return variables.
To define a function, use: def function_name(variable_name): do something with variable_name return a_variable
To call a function, use: function_name(variable_name) |
Let's create a function that accepts a number, and returns the number +1 |
Adjust the function to accept two numbers and return the product (multiply) of these two numbers. | |
Covered in this Section:
A List is a simple list of values. Lists are denoted using the square brackets.
Items can be added to lists using the append command. |
copy/paste: my_list=['red', 'orange', 'yellow', 'green', 'blue', 'indigo'] |
A list indexes items beginning with 0 and can be called using its index. | |
Items in a list can be iterated, or looped through using the for loop. |
Covered in this Section:
Different from for loops, while loops will run indefinitely while the expression is true.
Careful! - It is possible to create infinite loops using while |
|
We can now easily add unlimited attempts to our document line size guesser |
line_count = 1000 while guess!=line_count: print('You guessed it in', attempts, 'guesses!') |
Create a notebook that will allow users to enter two numbers, decide whether the numbers should be added, subtracted, multiplied, or divided, and then print the solution.
--------------------
Hints:
Copyright © Baylor® University. All rights reserved.
Report It | Title IX | Mental Health Resources | Anonymous Reporting | Legal Disclosures