Difference between revisions of "IoT Coding basics"

From csn
Jump to navigation Jump to search
Line 109: Line 109:
 
== Upgrade the Python2 code ==
 
== Upgrade the Python2 code ==
  
Create an upgrade this code to python3
+
The following is a Simple python game, that reuses the random function from bash that we used earlier. The problem is that it was coded for Python 2. I want you to upgrade it so that it will work natively with Python 3. You will need to edit the shebang as well as the print statements
  
=== Averaging a value ===
+
<pre>
  
=== Exponentially Weighted Moving Averages ====
+
#!/usr/bin/python2
  
 +
#import sys
 +
 +
print "Seeking answers from the computerized 8 ball"
 +
if theanswer == 1:
 +
  print "Perhaps"
 +
   
 +
elif theanswer == 2:
 +
  print "Outlook good"
 +
   
 +
elif theanswer == 3:
 +
  print "Don't count on it"
 +
   
 +
elif theanswer == 4:
 +
  print "Hmmmmm"
 +
   
 +
elif theanswer == 5:
 +
  print "Focus and it will happen"
 +
   
 +
elif theanswer == 6:
 +
  print "It seems unclear"
 +
   
 +
elif theanswer == 7:
 +
  print "It is a certainty"
 +
   
 +
elif theanswer == 8:
 +
  print "Unlikely"
 +
 +
</pre>
  
 
== Glueing code together ===
 
== Glueing code together ===

Revision as of 02:30, 9 June 2020

In this lab we will learn about python and bash

Lab showing bash and python as well as execution times Provide some fix the code as well as complete the code examples. Dealing with numbers, printing them, division and decimals

Introducing Bash and Python

Hello World!

In this section, we will run Bash and Python hello world examples.

Bash

#!/bin/bash
# This is a bash comment 
echo "Hello Bash World!"

Python

#!/usr/bin/env python3
# This is a python comment
print("Hello Python World!")

Use your favourite text editor to create these two files hello_world.sh and hello_world.py. Make sure you provide them with execute permissions

chmod 777 hello_world.sh

And

chmod 777 hello_world.py

You can execute them with

./hello_world.sh

And

./hello_world.py

The contents of the code should be prety straightforward. The only link that may need explaining is the shebang first line. The shebang line allows the script to executed like a standalone executable without typing python, ruby, et cetera. It also immediately tells someone looking at the code, what language has been used and in the case of python it can specify the version of python that is used to execute it.

Variables

Modify your current code using the following code snippets

Bash

a="Hello bash using variables"
echo $a

Python

a="Hello Python using variables"
print(a)

Make sure you understand everything before moving on.

Basic calculations

Lets add some calculations to our previous code:

Bash

a=10
b=5
c=$((a+b))
echo $c

Python

a=20
b=10
c=a+b
print(c)

Run them both. Once you understand, I would like you to modify the examples so that you are familiar with subtraction, multiplication and division.

Modify the Code 1

Examine the following two code snippets written in Bash and Python.

Bash

#!/bin/bash
for ((i=0;i<10;i++)); 
do 
  echo $i
done

Python

#!/usr/bin/env python3
for x in range(10):
    print(x)

Modify each code snippet to print the square of each number in the sequence. So the output should look like:

0
1
4
9
16
25
36
49
64
81

Can you create an average of the numbers in the sequence? So the output should just be:

4.5 for python
4 for bash as it does not natively support decimal or floating-point numbers

Upgrade the Python2 code

The following is a Simple python game, that reuses the random function from bash that we used earlier. The problem is that it was coded for Python 2. I want you to upgrade it so that it will work natively with Python 3. You will need to edit the shebang as well as the print statements


#!/usr/bin/python2

#import sys

print "Seeking answers from the computerized 8 ball"
if theanswer == 1:
  print "Perhaps"
    
elif theanswer == 2:
  print "Outlook good"
    
elif theanswer == 3:
  print "Don't count on it"
    
elif theanswer == 4:
  print "Hmmmmm"
    
elif theanswer == 5:
  print "Focus and it will happen"
    
elif theanswer == 6:
   print "It seems unclear"
    
elif theanswer == 7:
   print "It is a certainty"
    
elif theanswer == 8:
   print "Unlikely"

Glueing code together =

Glueing code together

  • running Python from bash
  • running bash from python

Code resiliency

Reving processes

Virtual Terminals

Complete some common Fibonacci or squares examples

Modify the code 2