Skip to content

Commit 03cdde0

Browse files
authored
Merge pull request #341 from realpython/python-shebang
Python Shebang Q&A
2 parents 507db61 + 6427947 commit 03cdde0

File tree

13 files changed

+1110
-0
lines changed

13 files changed

+1110
-0
lines changed

python-shebang/README.md

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# Executing Python Scripts With a Shebang
2+
3+
This folder holds the code for the Real Python tutorial entitled [Executing Python Scripts With a Shebang](https://realpython.com/python-shebang/).
4+
5+
## Sample Scripts
6+
7+
### Python
8+
9+
Command:
10+
11+
```shell
12+
$ ./hello.py
13+
Hello, World!
14+
```
15+
16+
### Perl
17+
18+
Command:
19+
20+
```shell
21+
$ ./hello.pl
22+
Hello, World!
23+
```
24+
25+
### JavaScript
26+
27+
Command:
28+
29+
```shell
30+
$ ./hello.js
31+
Hello, World!
32+
```
33+
34+
### Java
35+
36+
Command:
37+
38+
```shell
39+
$ ./hello.j
40+
Hello, World!
41+
```
42+
43+
## Custom Interpreter
44+
45+
Install your custom interpreter of a popular [esoteric programming language](https://esolangs.org/wiki/Esoteric_programming_language) into a Python virtual environment:
46+
47+
```shell
48+
$ cd interpreter/
49+
$ python3 -m venv venv/
50+
$ source venv/bin/activate
51+
(venv) $ python -m pip install .
52+
```
53+
54+
Execute sample scripts through the esoteric language interpreter while using the virtual environment:
55+
56+
```shell
57+
(venv) $ cd scripts/
58+
```
59+
60+
### Towers of Hanoi
61+
62+
```shell
63+
(venv) $ ./hanoi.b
64+
65+
Towers of Hanoi in Brainf*ck
66+
Written by Clifford Wolf <http://www.clifford.at/bfcpu/>
67+
68+
69+
70+
71+
72+
73+
xXXXXXx
74+
xXXXXXXXXXXXXXXXXXXXXXXXXXx xXx
75+
xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx xXXXXXXXXXXXXXx
76+
xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx xXXXXXXXXXXXXXXXXXx
77+
----------------------------------- -----------------------------------
78+
79+
80+
81+
82+
83+
84+
85+
xXXXXXXXXXx
86+
xXXXXXXXXXXXXXXXXXXXXXx
87+
-----------------------------------
88+
```
89+
90+
91+
### Sierpiński Triangle
92+
93+
```shell
94+
(venv) $ ./sierpinski.b
95+
*
96+
* *
97+
* *
98+
* * * *
99+
* *
100+
* * * *
101+
* * * *
102+
* * * * * * * *
103+
* *
104+
* * * *
105+
* * * *
106+
* * * * * * * *
107+
* * * *
108+
* * * * * * * *
109+
* * * * * * * *
110+
* * * * * * * * * * * * * * * *
111+
* *
112+
* * * *
113+
* * * *
114+
* * * * * * * *
115+
* * * *
116+
* * * * * * * *
117+
* * * * * * * *
118+
* * * * * * * * * * * * * * * *
119+
* * * *
120+
* * * * * * * *
121+
* * * * * * * *
122+
* * * * * * * * * * * * * * * *
123+
* * * * * * * *
124+
* * * * * * * * * * * * * * * *
125+
* * * * * * * * * * * * * * * *
126+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
127+
```
128+
129+
130+
### Factorial
131+
132+
```shell
133+
(venv) $ ./factorial.b
134+
1
135+
1
136+
2
137+
6
138+
24
139+
120
140+
720
141+
5040
142+
40320
143+
362880
144+
3628800
145+
39916800
146+
479001600
147+
6227020800
148+
87178291200
149+
1307674368000
150+
20922789888000
151+
355687428096000
152+
6402373705728000
153+
121645100408832000
154+
2432902008176640000
155+
51090942171709440000
156+
1124000727777607680000
157+
25852016738884976640000
158+
620448401733239439360000
159+
15511210043330985984000000
160+
403291461126605635584000000
161+
10888869450418352160768000000
162+
^C
163+
```
164+
165+
### Fibonacci Sequence
166+
167+
```shell
168+
(venv) $ ./fib.b
169+
0
170+
1
171+
1
172+
2
173+
3
174+
5
175+
8
176+
13
177+
21
178+
34
179+
55
180+
89
181+
144
182+
233
183+
377
184+
610
185+
987
186+
1597
187+
2584
188+
4181
189+
6765
190+
10946
191+
17711
192+
28657
193+
46368
194+
75025
195+
121393
196+
196418
197+
^C
198+
```
199+
200+
### ROT-13
201+
202+
This is an interactive script that will expect you to provide input from your keyboard:
203+
204+
```shell
205+
(venv) $ ./rot13.b
206+
This text will be encrypted.
207+
Guvf grkg jvyy or rapelcgrq.
208+
```
209+
210+
### Hello World
211+
212+
```shell
213+
(venv) $ ./hello.b
214+
Hello World!
215+
```

python-shebang/hello.j

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env -S java --source 11
2+
3+
public class Hello {
4+
public static void main(String[] args) {
5+
System.out.println("Hello, World!");
6+
}
7+
}

python-shebang/hello.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
console.log("Hello, World!")

python-shebang/hello.pl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env perl
2+
3+
print("Hello, World!\n");

python-shebang/hello.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
3+
print("Hello, World!")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
requires = ["setuptools>=64.0.0", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "brainf"
7+
version = "1.0.0"
8+
9+
[project.scripts]
10+
brainf = "brainf:main"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env brainf
2+
3+
>>>>++>+[
4+
[
5+
>[>>]<[>+>]<<[>->>+<<<-]>+[
6+
[+>>[<<+>>-]>]+[-<<+<]>-[
7+
-[<+>>+<-]++++++[>++++++++<-]+>.[-]<<[
8+
>>>[[<<+>+>-]>>>]<<<<[[>+<-]<-<<]>-
9+
]>>>[
10+
<<-[<<+>>-]<+++++++++<[
11+
>[->+>]>>>[<<[<+>-]>>>+>>[-<]<[>]>+<]<<<<<<-
12+
]>[-]>+>>[<<<+>>>-]>>>
13+
]<<<+[-[+>>]<<<]>[<<<]>
14+
]>>>[<[>>>]<<<[[>>>+<<<-]<<<]>>>>>>>-[<]>>>[<<]<<[>+>]<]<<
15+
]++>>
16+
]<<++++++++.+
17+
]
18+
19+
This program computes the factorials (https://oeis.org/A000142). Because this
20+
sequence is infinitely long, this program doesn't terminate on its own; you will
21+
have to kill it. This program is much faster than my earlier factorial program.
22+
23+
[factorial2.b -- compute factorials
24+
(c) 2019 Daniel B. Cristofani
25+
http://brainfuck.org/]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env brainf
2+
3+
>++++++++++>+>+[
4+
[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
5+
[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
6+
[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
7+
]<<<
8+
]
9+
10+
This program doesn't terminate; you will have to kill it.
11+
Daniel B Cristofani (cristofdathevanetdotcom)
12+
http://www.hevanet.com/cristofd/brainfuck/

0 commit comments

Comments
 (0)