Python Fundamentals đ
Python interpreter is great, but why might we not want to write all our code in the interpreter? What happens every time you quit?
SOLUTION:
- Open your terminal directory in Visual Studio Code or your favorite code editor, and create a new file:
touch first_script.py
You should see this file in Visual Studio Code. Weâre going to write your first code script! Feel free to enter some of what you wrote for the exercise, or copy this below:
tools = [
{
'name':'HathiTrust',
'projects': ['Bookworm', 'Google Ngram Viewer']
},{
'name': 'Gephi',
'founded_year': 2008
}
]
- Now try running the file from your terminal Make sure you are not in the python interpreter. Type:
python3 first_script.py
What happens?
Because we arenât in the interpreter any more, unless we tell the computer to output a value it wonât show us any message (unless thereâs an error.)
One solution is the print method.
In first_script.py, add the line to the bottom of the script:
print(tools)
This time you should see the list of tools.
Now try moving that line to the top of first_script.py and running the script again.
Weâve hit a problem. Letâs break down what happened here. If the print statement was at the bottom of the script it worked, but if it was at the top it didnât.
Thatâs because the print statement within itâs parentheses references the variable tools
, but we havenât defined that variable yet.
Computers read code from top to bottom
So whatâs the print statement?
Print is a built-in Python method, which means it comes installed with Python and we can use it whenever we are writing Python.
Print is used to literally print out values to the terminal, and itâs one of many ways to find out the output of a Python script.
Built-In Methods
Print is one of many methods. Letâs take a look at a few more.
Len()
In first_script.py, move the print statement back to the bottom of the script. Then above the print statement type:
tools_numbers = len(tools)
print('how many DH tools do we have?', tools_numbers)
Then try running the code. You should first see our question followed by a number and then the list of projects.
The len method can return the length of any list, dictionary, or string in Python.
Type()
You might also want to know the type of your variable, whether itâs a data type or a data structure. In first_script.py, add to the bottom of the script:
print(type(tools), type(tools[0]))
Once you run the script you should see that we have both a list and a dictionary. With print statements you can add multiple items as long as they are separated by commas, and you can use built-in methods inside the print statement, instead of assigning them to a variable first.
Input()
You can also get input from the terminal using the input method. In first_script.py, type the following at the bottom of the script:
print('Enter your name:')
name = input()
print('Hello, ' + name)
What did we just do? First we printed out a prompt. Then we assigned the input method to a variable, and then we printed out hello concatenated with the value of name.
Youâve now used built-in methods before for both dictionaries and lists. But they exist for data types too.
Built-In Methods for Strings
Split()
The split method lets us split up a string and turn it into a list. In first_script.py, add the following:
definition_of_dh = 'DH is 1) using new digital tools to do humanistic research and 2) using humanistic methods to analyze new digital tools.'
print(definition_of_dh)
print(definition_of_dh.split(''))
Join()
The join method lets us take a list and join all the values together.
split_definition = definition_of_dh.split(' ')
joined_definition = '_'.join(split_definition)
print(joined_definition)
Replace()
The replace method lets us find a string and replace it with another string. You can also specify how many times you want to replace a string.
edited_definition_of_dh = definition_of_dh.replace('tools', 'people')
print(edited_definition_of_dh)
Upper()
andLower()
The upper and lower methods return the entire string capitalized or lowercased respectively.
print(definition_of_dh.upper(), definition_of_dh.lower())
You can find more information about string methods here https://www.w3schools.com/python/python_ref_string.asp
Inline Commenting
Comments are especially usefulânecessary!âfor collaboration. Python is open source and its community of millions of coders often share in its permissive approach to intellectual property. Python as a whole is a giant collaborative project of which you are now members.
When you write particularly complicated logic or whenever you write new classes or functions (more on this later!), you should write a comment to explain yourself.
edited_definition_of_dh = definition_of_dh.replace('tools', 'people')
print(edited_definition_of_dh)
Documentation
Python, as with virtually all other languages and complex codes, contains extensive documentation that covers all aspects of its use. This documentation is easily accessible via the Internet.
Letâs take a look at the specific documentation for strings:
Python 3 Docs: Built-in Types: Strings
Learning to read documentation is a critical skill for succeeding as a programmer. Happily, most of you, as graduate students, should already be literate.
The Zen of Python
Whatâs the deal with Python?
Type this into the Python interpreter:
import this
Hereâs one interpretation of Z of P.
Also, a DH answer: lots of DH projects are written in Python because of its simplicity and robust community and itâs especially popular in areas like text analysis and machine learning.