-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathrun_a_python_script.html
More file actions
80 lines (79 loc) · 4.59 KB
/
run_a_python_script.html
File metadata and controls
80 lines (79 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html>
<head>
<script src="/bjc-r/llab/loader.js"></script>
<title>Run a Python Script</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-176402054-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-176402054-1');
</script>
</head>
<body>
<h3>Running a Python File</h3>
<p>
Now it's time to seriously write some code. You may have noticed that when we were typing code directly into the Python prompt, after you hit enter, you can't go back and change any lines. Can you imagine writing larger programs like this? Any time you wanted to back and change something, you'd have to retype everything. "There has to be a better way."
</p>
<p>
And there is! We can create a <b>python file</b>, where we can write and edit as many lines of code as we want. Then, we can run the whole file through Python at once. This way, if we need to go back and edit something, we can just change specific lines in the file, without retyping anything.
</p>
<p>
To be able to do this, we need a program to edit the python files with. Document editors such as Microsoft Word or Pages won't work for us-- they're not designed to edit plain text like we'll be doing. The most simple and popular text editors are <a href="https://www.sublimetext.com/3">Sublime Text</a> and <a href="https://atom.io/">Atom</a>. The lab computers have Sublime Text installed.
</p>
<p>
Open a suitable text editor, and create a new file. Title it <code>lab1.py</code>, and <b>save it in the PythonLab1</b> folder that we made earlier.
</p>
<p>
You've created your first python file! Use the text editor to edit the new file so that it contains the following line:
</p>
<p><pre><code>
print("Hello, World!")
</code></pre></p>
<p class="alert alert-danger quoteRed">
Don't forget to save the file before you try to run it. Otherwise, when you run your program, you'll see the output of whatever you saved last.
</p>
<p>
Now, let's run the file with Python. Head back to your terminal, and use the commands you learned earlier to navigate to the <code>PythonLab1</code> directory we made earlier. Once you are there, use the <code>ls</code> command to verify that <code>lab1.py</code> is there!
</p>
<p><pre><code>
Alonzos-MacBook:PythonLab1 alonzo$$ ls
lab1.py
</code></pre></p>
<p>
To run your file with Python, you'll use the same Python command from earlier, but with an extra <b>argument</b>: your file's name. So, your command will either be <code>python3 lab1.py</code> or <code>python lab1.py</code>. Type the command and hit enter.
</p>
<p>
Congrats! You should see the words <code>Hello, World</code> printed to your terminal. You've just run your file with Python.
</p>
<h3>Interactive Mode</h3>
<p>
Did you notice that after the command <code>python3 lab1.py</code>, the prompt was back to something like:
</p>
<p><code>Alonzos-MacBook:PythonLab1 alonzo$$</code>?</p>
<p>
This means that even though we opened Python to run our program, Python exited when it was done. Sometimes, we'd like to stay in Python after we run a file!
</p>
<p>
To stay in Python after running a file, use the same command, but with <em>another</em> argument <b>-i</b>, like this: <code>python3 -i lab1.py</code>. Before you run your file with this new option, add the following line to <code>lab1.py</code> using the text editor:
</p>
<p><pre><code>
print("Hello, World!")
a = 5
</code></pre></p>
<p>
Don't forget to save. Now, return to the terminal and run the command <code>python3 -i lab1.py</code>.
</p>
<p>
Notice how "Hello, World!" is still printed to the terminal, but we are left with a Python prompt, <code>>>></code>! Get ready for this: type <code>a</code> and hit enter.
</p>
<p>
We get <code>5</code>. Python remembers everything from the file that it ran when we opened it! Imagine, we could define all sorts of useful variables and functions in a file, then play around with them in Python.
</p>
<p class="alert alert-info quoteBlue">
Now let's write some Python functions <code>>>></code>
</p>
</body>
</html>