Run a Small Program

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Make sure Python is installed correctly on your machine
  • Start the interactive interpreter
  • Edit and run a small program

Files Created: art.py


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-132390{{"`Run a Small Program`"}} python/variables_data_types -.-> lab-132390{{"`Run a Small Program`"}} python/numeric_types -.-> lab-132390{{"`Run a Small Program`"}} python/type_conversion -.-> lab-132390{{"`Run a Small Program`"}} python/conditional_statements -.-> lab-132390{{"`Run a Small Program`"}} python/for_loops -.-> lab-132390{{"`Run a Small Program`"}} python/lists -.-> lab-132390{{"`Run a Small Program`"}} python/tuples -.-> lab-132390{{"`Run a Small Program`"}} python/function_definition -.-> lab-132390{{"`Run a Small Program`"}} python/importing_modules -.-> lab-132390{{"`Run a Small Program`"}} python/standard_libraries -.-> lab-132390{{"`Run a Small Program`"}} python/raising_exceptions -.-> lab-132390{{"`Run a Small Program`"}} python/math_random -.-> lab-132390{{"`Run a Small Program`"}} python/os_system -.-> lab-132390{{"`Run a Small Program`"}} python/python_shell -.-> lab-132390{{"`Run a Small Program`"}} python/build_in_functions -.-> lab-132390{{"`Run a Small Program`"}} end

Launch Python

Start Python3 on your machine. Make sure you can type simple statements such as the "hello world" program:

>>> print('Hello World')
Hello World
>>>

In much of this course, you'll want to make sure you can work from the interactive REPL like this. If you're working from a different environment such as IPython or Jupyter Notebooks, that's fine.

Some Generative Art

Create the following program and put it in a file called art.py:

## art.py

import sys
import random

chars = '\|/'

def draw(rows, columns):
    for r in range(rows):
        print(''.join(random.choice(chars) for _ in range(columns)))

if __name__ == '__main__':
    if len(sys.argv) != 3:
        raise SystemExit("Usage: art.py rows columns")
    draw(int(sys.argv[1]), int(sys.argv[2]))

Make sure you can run this program from the command line or a terminal.

python3 art.py 10 20

If you run the above command, you'll get a crash and traceback message. Go fix the problem and run the program again. You should get output like this:

python3 art.py 10 20
||||/\||//\//\|||\|\
///||\/||\//|\\|\\/\
|\////|//|||\//|/\||
|//\||\/|\///|\|\|/|
|/|//|/|/|\\/\/\||//
|\/\|\//\\//\|\||\\/
|||\\\\/\\\|/||||\/|
\\||\\\|\||||////\\|
//\//|/|\\|\//\|||\/
\\\|/\\|/|\\\|/|/\/|

Important Note

It is absolutely essential that you are able to edit, run, and debug ordinary Python programs for the rest of this course. The choice of editor, IDE, or operating system doesn't matter as long as you are able to experiment interactively and create normal Python source files that can execute from the command line.

Summary

Congratulations! You have completed the Run a Small Program lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like