Link Search Menu Expand Document

Basic Terminal Apps

Basic Terminal Apps

# Show a simple message
print('hello class')
hello class
import os

# Display a title bar.
print("**********************************************")
print("***  Hello to everyone in class today!     ***")
print("**********************************************")

# Display a bunch of output, representing a long-running program.
for week in range(1, 8):
    print(f"We will learn interesting things in Week {week:02}")
    print(f"We will learn interesting things in Week {week:02}")
**********************************************
***  Hello to everyone in class today!     ***
**********************************************
We will learn interesting things in Week 01
We will learn interesting things in Week 01
We will learn interesting things in Week 02
We will learn interesting things in Week 02
We will learn interesting things in Week 03
We will learn interesting things in Week 03
We will learn interesting things in Week 04
We will learn interesting things in Week 04
We will learn interesting things in Week 05
We will learn interesting things in Week 05
We will learn interesting things in Week 06
We will learn interesting things in Week 06
We will learn interesting things in Week 07
We will learn interesting things in Week 07

The sleep function

import os
from time import sleep

# Display a title bar.
print("**********************************************")
print("***  Hello to everyone in class today!     ***")
print("**********************************************")

# Display a bunch of output, representing a long-running program.
for week in range(1,8):
    sleep(1)
    print(f"We will learn interesting things in Week {week:02}")
**********************************************
***  Hello to everyone in class today!     ***
**********************************************
We will learn interesting things in Week 01
We will learn interesting things in Week 02
We will learn interesting things in Week 03
We will learn interesting things in Week 04
We will learn interesting things in Week 05
We will learn interesting things in Week 06
We will learn interesting things in Week 07

Accepting arguments

You can pass arguments into your application to dynamically change the behavior at run time.

See terminal_1.py

import sys
from time import sleep

adjective = sys.argv[1]
num_weeks = int(sys.argv[2])
    
print("**********************************************")
print("***  Hello to everyone in class today!     ***")
print("**********************************************")

for week in range(1, num_weeks + 1):
    sleep(1)
    print(f"We will learn {adjective} things in Week {week:02}")

Note:

  • sys.argv[1] is the 1st parameter passed in.
  • sys.argv[0] represents the path of the file being run
  • all arguments are strings by default, you can cast these to other data types
!python terminal_1.py fun 3
**********************************************
***  Hello to everyone in class today!     ***
**********************************************
We will learn fun things in Week 01
We will learn fun things in Week 02
We will learn fun things in Week 03

Here we slightly modify the code to process a list of arguments. Since all arguments are strings we’ll pass in a comma separated list of values.

See terminal_2.py

import sys
from time import sleep

adjectives = sys.argv[1]
    
print("**********************************************")
print("***  Hello to everyone in class today!     ***")
print("**********************************************")

for week, adjective in enumerate(adjectives.split(',')):
    sleep(1)
    print(f"We will learn {adjective} things in Week {(week + 1):02}")

Note:

  • adjectives is a string which we’ll split on commas to create a list
  • we use enumerate to keep track of the index we’re currently processing
  • since index is zero based, we’ll add 1 in the print statement to associate the index with the correct week
!python terminal_2.py "fun,interesting,exciting"
**********************************************
***  Hello to everyone in class today!     ***
**********************************************
We will learn fun things in Week 01
We will learn interesting things in Week 02
We will learn exciting things in Week 03