Open main menu

CDOT Wiki β

Changes

OPS435 Online Lab1

4,489 bytes added, 21:39, 12 May 2020
Part II - Script (execution) mode: creating Python script
== Part II - Script (execution) mode: creating Python script ==
:* In this part we will start writing our very first python script. The scripts we are going to try will be very basic and it is use to help us practice the language syntax and explore the foundational coding skills, such as: :** sending text to the screen,:** storing value in an data object,=== Task 1 - Python script using builtin Functions === ====Creating the "Hello World" Python Script==== You will learn to create a simple python script in this section. This python script will just call the Python builtin function print() to send the text "hello world" to the screen. The "hello world" is 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 Python3 shell as well as to learn how to run the Python script from the bash shell. :'''Perform the following steps:'''  :#Create a new Python file in your ~/ops435/lab1 directory. Call it lab1a.py. The first Python code we will write is going to call the print() function. A function is code that has been defined somewhere. Functions can take arguments, use these arguments in some way, and then usually, but not always, return a result. The first function we will use is the "print()" functions, it's sole purpose is to send data to the screen.<br><br>:#Add the following line into your source code file:<source>print()</source>And run it from the command-line: <source>python3 ./lab1a.py</source>You will 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 include an argument ('hello world'):<br><source>print('hello world') </source>This time we should now see that the python function"print()" has sent something to the screen - the words 'hello world'. In Python a word or a bunch of characters like 'hello world' is called a 'string'. In the above example, a '''string''' was passed as an '''argument''' to the print '''function'''. These words are important for understanding and talking about different aspects of code.<br><br>:# 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><ol><li value="5" style="margin-left:25px;">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 # Any line that starts with a "#" is also known as a comment,# these lines are ignored by the python interpreter even if# they contain code. The very first line is called a Shebang line, # it is used to tell the system which interpreter to # use(python2, python3, bash, etc).  # Description: This program will output "hello world" to the screen print('Hello world')</source><li style="margin-left:25px;">Another way of running a Python program is executing it directly, e.g.: <source>./lab1a.py</source>Note that the file will need execute permissions even though you ran it just fine earlier. Why is that? <li style="margin-left:25px;">Download the check script and check your work. Enter the following commands from the bash shell.<source lang="bash"> cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls lab1a.py #confirm that you have the lab1a.py script in your directory ls CheckLab1.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab1a</source><li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1a.py". When the check script tells you everything is "ok", you may proceed to the next step.</ol>
= Investigation 4 - exploring Python's built-in data objects =
1,760
edits