Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

Data & Digital Scholarship Tutorials

Workshop Description

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. Introduction to Python, Jupyter Notebooks, and Google's Colab platrform
  2. Hands-on introduction to 12 fundamental Python features/commands: print, variable types, operators, input, Python indentation syntax, if conditions, reading text files, loops, lists, dictionaries, concatenations, and functions.

Python I Workshop

(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)

 

  • Data Visualization
  • Text Data Mining
  • Python Data Scripting
AND
Pick any One Category Below, Take at Least Two Workshops from That Category:

 

(Total of 2)

  • Research Data Management
  • Finding Secondary Data

 

* Workshops are offered every semester. No need to fit all 6 in one semester. Become a Data Scholar at your own pace.

* Becoming a Data Scholar is not mandatory. Take any workshop you like.

Head to Google Colaboratory

  • Sign in
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:

  • Site:
    • Light/Dark theme
  • Editor:
    • Indentation Length
    • Line Numbers

 

Covered in this section:

  • Print
  • Variables

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:

  • Variable types
  • Concatenations
  • Expressions
  • Input

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:

  • If condition

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:

  • Uploading to Colab
  • Reading and writing text files
  • Line break
  • Counting characters in a string
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')
print('(2) Document 2')
print('(3) Document 3')
doc=input('Please select document: ')
if doc=='1':
    f=open('document1.txt','r')
    contents=f.read()
    f.close()
    lines_count = contents.count('\n')+1
elif doc=='2':
    f=open('document2.txt','r')
    contents=f.read()
    f.close()
    lines_count = contents.count('\n')+1
elif doc=='3':
    f=open('document3.txt','r')
    contents=f.read()
    f.close()
    lines_count = contents.count('\n')+1
else:
    print('Try again')

first_guess=float(input('How many lines do you guess?'))
if first_guess == lines_count:
    print('Correct!')
else:
    if first_guess<lines_count:
        print('Too Low')
    else:
        print('Too High')
    second_guess=float(input('How many lines do you guess? '))
    if second_guess == lines_count:
        print('Correct!')
    else:
        print('Sorry, You Lose! ')

   
   

Covered in this Section:

  • Functions

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:

  • Lists
  • For Loops

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:

  • While Loops

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
guess = 0  # needed to intialize guess
attempts=0

while guess!=line_count:
    attempts=attempts+1
    guess=float(input('Guess? '))
    if guess<line_count:
        print('Too Low!')
    elif guess>line_count:
        print('Too High!')

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:

  • Use If statements
  • Use Inputs
  • Multiply: *
  • Divide: /

University Libraries

One Bear Place #97148
Waco, TX 76798-7148

(254) 710-6702