Skip to content

Commit c4d5176

Browse files
start writing file handling/packages lesson
1 parent 6bd7ec6 commit c4d5176

File tree

4 files changed

+169
-1
lines changed

4 files changed

+169
-1
lines changed

File_Handling_and_Packages.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "markdown",
6+
"source": [
7+
"# Packages and File Handling\n",
8+
"\n",
9+
"> _Why bother reinventing the wheel when someone else has already done the work for you? ~ unknown, c. 2019, in conversation_\n",
10+
"\n",
11+
"\n",
12+
"\n",
13+
"\n",
14+
"A large part of programming is using code that other people have written. However, that does not mean that you should blatantly copy and paste code you find on the internet into your own projects. This is bad practice for a number of reasons, not least of which is that you may be violating copyright or license agreements. You may also run into code written by malicious actors, and it is always bad practice to add code to your project that you do not understand. Instead of copying code, you should use packages and libraries that are designed to be used by others.\n",
15+
"\n",
16+
"Packages and libraries are collections of code that are designed to be used by others. These contain functions, classes, and modules that you can use in your own projects. They are very useful in saving time and effort, as you do not have to write code from scratch for things like data manipulation, web development, or machine learning. Instead, you can use packages that have already been written and tested by others.\n",
17+
"\n",
18+
"Have you ever woken up in the morning and said to yourself, \"Gee, I really want to write a new interface for GUI in Python today?\" No, you probably haven't unless you are crazy (if you have, mad respect tho). Instead, you probably want to use a package that has already been written for you, like Tkinter or PyQt. These packages provide a set of tools and functions that make it easy to create GUI applications in Python, which we'll be covering later (hopefully).\n",
19+
"\n",
20+
"\n",
21+
"## pip and the python package index (PyPI)\n",
22+
"\n",
23+
"pip is the package installer for python. You can use pip to install packages from the [Python Package Index](https://pypi.org) and other indexes. The Python Package Index is a repository of software for the Python programming language. It is the default package index for pip, and it contains thousands of packages that you can use in your own projects. As of 1:45 PM CST, August 24th, 2025, there are 670,699 packages avaliable on PyPI. (one of those is mine :]).\n",
24+
"\n",
25+
"### importing packages\n",
26+
"\n",
27+
"when you want to use a package in your project, you first need to install it using pip. You can do this by running the following command in your terminal or command prompt:\n",
28+
"\n",
29+
"```shell\n",
30+
"pip install package_name\n",
31+
"```\n",
32+
"if you are using a Jupyter notebook, you can run the same command by prefixing it with an exclamation mark:\n"
33+
],
34+
"id": "78388416954637ca"
35+
},
36+
{
37+
"cell_type": "code",
38+
"id": "initial_id",
39+
"metadata": {
40+
"collapsed": true,
41+
"ExecuteTime": {
42+
"end_time": "2025-08-24T18:57:01.809364Z",
43+
"start_time": "2025-08-24T18:57:01.218847Z"
44+
}
45+
},
46+
"source": "!pip install random\n",
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"Found existing installation: package_name 0.1\r\n",
53+
"Uninstalling package_name-0.1:\r\n",
54+
" Successfully uninstalled package_name-0.1\r\n"
55+
]
56+
}
57+
],
58+
"execution_count": 7
59+
},
60+
{
61+
"metadata": {},
62+
"cell_type": "markdown",
63+
"source": "We've just installed one of the most commonly used packages in Python, `random`. This package provides functions for generating random numbers and making random selections. You can now use the functions in the `random` package in your own projects by importing it at the top of your Python file or Jupyter notebook:",
64+
"id": "a12bc91b85fbf552"
65+
},
66+
{
67+
"metadata": {
68+
"ExecuteTime": {
69+
"end_time": "2025-08-24T18:58:35.268923Z",
70+
"start_time": "2025-08-24T18:58:35.262621Z"
71+
}
72+
},
73+
"cell_type": "code",
74+
"source": [
75+
"import random\n",
76+
"\n",
77+
"print(random.random()) # this will print a random float between 0 and 1\n",
78+
"print(random.randint(1, 10)) # this will print a random integer between 1 and 10 (inclusive)"
79+
],
80+
"id": "529368f0947c3d67",
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"0.8635314582930362\n",
87+
"3\n"
88+
]
89+
}
90+
],
91+
"execution_count": 18
92+
},
93+
{
94+
"metadata": {},
95+
"cell_type": "markdown",
96+
"source": [
97+
"## file handling\n",
98+
"\n",
99+
"file handling is an important part of programming, as it allows you to read and write data to files on your computer. this is crucial for maintaining data between program executions, as well as for storing and retrieving data in a structured way. You can open files in using the built-in `open()` function."
100+
],
101+
"id": "60bb2becfbbcce4a"
102+
},
103+
{
104+
"metadata": {
105+
"ExecuteTime": {
106+
"end_time": "2025-08-24T19:28:00.691470Z",
107+
"start_time": "2025-08-24T19:28:00.686432Z"
108+
}
109+
},
110+
"cell_type": "code",
111+
"source": [
112+
"with open(\"example.txt\", \"w\") as f:\n",
113+
" f.write(\"Hello, world!\") # try changing this and running again"
114+
],
115+
"id": "5cc57bf114ebb89d",
116+
"outputs": [],
117+
"execution_count": 19
118+
},
119+
{
120+
"metadata": {},
121+
"cell_type": "markdown",
122+
"source": [
123+
"Let's break it down. Get down on the floor and start dancing. (optional, but extra credit if you do)\n",
124+
"\n",
125+
"The `open()` function takes two arguments: the name of the file you want to open, and the mode in which you want to open it. In this case, we're opening a file called `example.txt` in write mode (`\"w\"`). If the file doesn't exist, it will be created. If it does exist, it will be overwritten. This is important to note, as you may accidentally overwrite important data if you're not careful.\n",
126+
"\n",
127+
"The `with` statement is used to create a context in which the file is open. This is important because it ensures that the file is properly closed when you're done with it, even if an error occurs. The `as f` part of the statement assigns the file object to the variable `f`, which you can then use to read from or write to the file.\n",
128+
"\n",
129+
"`.write()` is a method which allows you to write a string to a file that has been opened in a write mode. In this case, we're writing the string \"Hello, world!\" to the file.\n",
130+
"\n",
131+
"### other file modes\n",
132+
"\n",
133+
"There are several other modes you can use when opening a file:\n",
134+
"- `\"r\"`: read mode (default). This mode is used to read data from a file. If the file doesn't exist, an error will be raised. Good for reading in data.\n",
135+
"- `\"a\"`: append mode. This mode is used to add data to the end of a file. If the file doesn't exist, it will be created. Good for adding data to a file without overwriting existing data, like a log file or user data.\n",
136+
"-"
137+
],
138+
"id": "670fa24c371c3fbb"
139+
}
140+
],
141+
"metadata": {
142+
"kernelspec": {
143+
"display_name": "Python 3",
144+
"language": "python",
145+
"name": "python3"
146+
},
147+
"language_info": {
148+
"codemirror_mode": {
149+
"name": "ipython",
150+
"version": 2
151+
},
152+
"file_extension": ".py",
153+
"mimetype": "text/x-python",
154+
"name": "python",
155+
"nbconvert_exporter": "python",
156+
"pygments_lexer": "ipython2",
157+
"version": "2.7.6"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 5
162+
}

Functions_and_Classes.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"source": [
117117
"### practice exercise with recursion - sum of digits\n",
118118
"\n",
119-
"to practice writing recursive functions, write a function that takes in a non-zero positive integer and returns the sum of its digits. for example, if the number is 69420, 6 + 9 + 4 + 2 + 0 = 21, so the function should be returning 21. Here's a hint, try using logarithms or floor division `(a // b)` :3"
119+
"to practice writing recursive functions, write a function that takes in a non-zero positive integer and returns the sum of its digits. for example, if the number is 12345, 1+2+3+4+5 = 15, so the function should be returning 15. Here's a hint, try using logarithms or floor division `(a // b)`"
120120
]
121121
},
122122
{

Intro_To_Python.ipynb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"cell_type": "markdown",
66
"source": [
77
"# Introduction to Python!\n",
8+
"\n",
9+
"> _Python is an experiment in how much freedom programmers need. Too much freedom and nobody can read another's code; too little and expressiveness is endangered._ ~ Guido van Rossum, Creator of Python\n",
10+
"\n",
11+
"> _Python - why settle for snake oil when you can have the whole snake?_ ~ Mark Jackson\n",
12+
"\n",
813
"In today's lesson, we will be discussing the following questions: What is Python? How do I use it, and more importantly, why should I use it?\n",
914
"\n",
1015
"Python is a high level programming language, meaning that you are dealing more with concepts and ideas rather than manipulating the bits and bytes of a computer. This makes it an easy language for beginners, and when coupled with its intuitive syntax is a great language for people first learning how to code. But fear not, for if you are not a beginner, Python is popularly used in real-life professions, ranging from ML/Data Analysis to mathematics and developing desktop applications.\n",

example.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, world!

0 commit comments

Comments
 (0)