Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 1

1,405 bytes removed, 19:41, 16 January 2018
PART 1 - Common Introductory Python Functions
=== PART 1 - Common Introductory Python Functions ===
====Printing TextCreating the introductory "Hello World" Script====
:Let's start Python code editor You will learn to create a simple python script in this section. This python script will just print the text "hello world". The "hello world" an old traditional first program students usually are taught to create, which is based on the first programming example from the first C programming text co-written by Dennis Ritchie, the creator of the C programming language and start writing some Brian Kernighan. You will learn how to run the python script in the ipython3 shell as well as learn how to run the python codescript from the bash shell.
:'''Perform the following steps:'''
 :#Create a new python file in your ~/ops435/lab1 directory. Call it testlab1a.py or whatever makes sense for you - this will be a temporary file for trying simple Python programming language features. The first Python code we will write is going to call the print function. A function is code that has been defined in another location. Functions can take arguments, use these arguments in some way, and then usually return a result. The first function we will use is the "print()" functions, it's sole purpose is to output information to the screen.<br><br>
:#Add the following line into your source code file:<source>
print()
</source>And run it from the command-line in one of two ways:<source>./test.py</source> or <source>python3 ./test.py</source>Note that the file will need execute permissions for the first way of running it, but not for the second. Why is that? You will also notice that nothing is printed even though we called the "print()" function. This is because we didn't pass any arguments to it, lets try again.<br><br>
:# Modify your call to print() to inlcude an argument ('hello world'):<br><source>
print('hello world')
:# Note that there are similarities between the Python print() function and the Bash echo command, but Python is more picky than bash (which is a good thing). Try to run print without the brackets or without the quotes to see what happens.
<blockquote style="margin-left:35px;">{{Admon/tip|Reading errors|One of the things that makes a good programmer is debugging skills. The first and most important debugging technique is reading and understanding error messages. Try to understand what the errors are saying even if you think you already know what the problem is and already have some idea about how to fix it.}}</blockquote>
 
====Creating the introductory "Hello World" Script====
 
You will learn to create a simple python script in this section. This python script will just print the text "hello world". The "hello world" an old traditional first program students usually are taught to create, which is based on the first programming example from the first C programming text co-written by Dennis Ritchie, the creator of the C programming language and Brian Kernighan. You will learn how to run the python script in the ipython3 shell as well as learn how to run the python script from the bash shell.
 
:'''Perform the following steps:'''
 
:# Using a text editor, open a new text file called '''lab1a.py''':<source lang="bash">
vim ~/ops435/lab1/lab1a.py
</source>
:#Write the following code into our python file. Note the she-bang line at the top of the file to run this script in the python3 environment. You will need to add this she-bang line for all python scripts you create for this course.<source lang="python">
#!/usr/bin/env python3
print('Hello world')
</source>
:#Save the file and quit vim. We will now go over the process Another way of manually running this python scripta pythion program is executing it directly, e. Both in the Bash shell and in the IPython shellg.:#Now lets try running the script directly from the IPython shell by issuing the follwoing commands:<source>run ~/ops435/lab1./lab1atest.py</source>Your python script should have run, if you have any errors you should check Note that you typed the script in exactly. Be careful of extra spaces, symbols, letters, or lowercase/uppercase differences.<br><br>:# Issue the following command to exit your ipython session:<source>exit</source>:#Now, from the Bash shell we file will give need execute permissions even though you ran it the correct Linux permissions in order to run your newly-created python scriptjust fine earlier. This Why is just showing the multiple ways you can use this python script. You are not required to have IPython running on a system, however hopefully we can use IPython's powerful features to our advantage.:# Run your python script by issuing the following commands:<source lang="bash">chmod 755 ~/ops435/lab1/lab1a.pypython3 ~/ops435/lab1/lab1a.py</source>that?
:#Download the check script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab1/