With September having exactly 30 days, what better way to coordinate that with 30 Days of Python to learn python.
This is an amazing resource provided by GitHub user, Asabeneh, and highly recommend not only this challenge, but also the other self-learning challenges they provide on Asabeneh’s GitHub.
First off, what’s Python?
Python is a versatile, high-level programming language widely used for general-purpose programming. As an open-source, interpreted, and object-oriented language, Python has been popular since its first release on February 20, 1991. The latest version available is Python 3.
Fun Fact: Python was created by Dutch programmer Guido van Rossum. The language's name was inspired by the British sketch comedy series Monty Python's Flying Circus.
Why Python?
In the world of audiovisual archiving, where I often manage and organize digital files rather than film and video, the thought of manually renaming files, creating checksums, and performing quality control was inefficient, and tedious. Additionally, in my work with web archiving—a fully digital and online process—Python is one of the programming languages that can streamline and accelerate many of these tasks.
Python can significantly enhance audiovisual archiving processes, particularly through video quality control (QC) scripting. By leveraging Python, archivists can write, test, and deploy scripts that automate QC workflows for digital archive files derived from analog film, video, audio, and other time-based media formats. For example, Python scripts can be used to analyze video files for issues such as frame drops, audio sync problems, and color consistency.
These scripts can be integrated into larger QC workflows, ensuring that archived materials meet preservation standards and are accurately represented in digital formats. Python's flexibility allows for customized solutions tailored to the specific needs of different archival projects, streamlining the quality control process and improving the overall efficiency of audiovisual preservation.
Example of projects I plan to contribute to:
Browsetrix: https://github.com/webrecorder/browsertrix
pywb: https://github.com/webrecorder/pywb
Where to start with Python?
Before diving in, need to do environment Setup
Install Python
As I’m working on a Macbook Pro, the official Python site to download automatically gives me the MacOS version.
Confirm Python Installation
Typically, running the following command in the terminal will provide the version information: python --version.
python --version
Since I've installed Python 3, I need to use python3 --version to check the version.
python3 --version
Check out Python Shell
Quick tour inside the Python shell:
Write your code next to the prompt (symbol >>>)
To exit, exit()
To clear, CTRL + L
To comment, add a hashtag before your text (# Comment)
For help on built in functions: help(‘keywords’)
type()
print()
BEWARE OF ERRORS
SyntaxError: Occurs when the code violates Python's syntax rules. Like forgetting to close a parenthesis or using incorrect indentation.
NameError: Raised when trying to use a variable or function name that hasn’t been defined.
Example: print(message)
It’ll yell at you because it doesn’t know what you mean by message.
TypeError: Occurs when an operation is performed on an inappropriate type of data.
Example: len(5)
It’ll yell at you because length expects a sequence, not numbers.
ValueError: Raised when a function receives an argument of the correct type but an inappropriate value.
Example: int("Hello")
It’ll yell at you because a number can’t handle strings.
IndexError: Occurs when trying to access an index that is out of range for a sequence like a list or string.
Example: list_example = [1, 2, 3]; print(list_example[5])
It’ll yell at you because where are we getting the fifth index from!? It doesn’t exist.
KeyError: Raised when trying to access a dictionary key that doesn’t exist.
Example: dict_example = {"error_1": "KeyError"}; print(dict_example["error_2"])
It’ll yell at you because where’d we get error_2 from!? It doesn’t exist.
AttributeError: Happens when trying to access or call an attribute or method.
Basic Python
A Python script can be written in Python interactive shell or in the code editor.
A Python file has an extension .py.
Python indentation matters when coding! An indentation is a white space in a text (i.e. when you hit tab on your keyboard). BUT in Python it’s more than just a white space, it uses indentation to create blocks of codes.
How to make comments
# Single line comment
“““
Multiline comment uses quotes three times
“““
Data Types
Every language has data types. Python is no different.Number
Integer
Example: -1, 0, 1
Float
Example: 1.0, 0.0, 3.14
Complex
Example: 1 + j
What makes a number complex? When there is a combination of characters and numbers
String
Example: ‘use single quote’, “or double quote”, ‘both work’
Boolean
Example: True, False.
Yes, it must be capital
List
Example: ['Demon Slayer', 'Orange', 'Mango', 'Avocado']
Similar to an array in JavaScript, a Python list is an ordered collection of different data type items.
Dictionary
Example: { 'language':'Python', 'duration_days': 30 }
Similar to a hash in Ruby
Tuple
Example: (1," hello", True)
Tuples are ordered collections of different data types. It’s like a list BUT tuples can NOT be modified once they are created.
Set
Example: {'Asabeneh', 'Jujutsu Kaisen', 'Spy × Family', 'Mashle: Magic and Muscles ', 'One Piece'}
A set is a collection of data types similar to list and tuple. Unlike list and tuple, set is not an ordered collection of items.
Operations
Like in other languages, you can do basic operations with Python
print(2 + 3) # addition(+)
print(3 - 1) # subtraction(-)
print(2 * 3) # multiplication(*)
print(3 / 2) # division(/)
print(3 ** 2) # exponential(**)
print(3 % 2) # modulus(%)
print(3 // 2) # Floor division operator(//)
Exercises Day 1
I've detailed the technical aspects of my commits and exercises so make sure to follow along on my GitHub
Level 1
Do the basic operations using 3 and 4 as operands
Check the data types
Level 2
Create a python file helloworld.py
Print Level 1 exercises in the file
Level 3
Write an example for different Python data types such as Number(Integer, Float, Complex), String, Boolean, List, Tuple, Set and Dictionary.
Find an Euclidean distance between (2, 3) and (10, 8)