0% found this document useful (0 votes)
2 views2 pages

Practice Python

The document provides an introductory tutorial on using Python for data processing, collection, condition checking, and visualization, specifically in the context of a course involving cyber physical systems. It outlines the use of lists, loops, and Numpy for handling and analyzing data, as well as the Matplotlib library for data visualization. Additionally, it includes a lab section with practical exercises to reinforce the concepts learned in the tutorial.

Uploaded by

bazmitch362
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Practice Python

The document provides an introductory tutorial on using Python for data processing, collection, condition checking, and visualization, specifically in the context of a course involving cyber physical systems. It outlines the use of lists, loops, and Numpy for handling and analyzing data, as well as the Matplotlib library for data visualization. Additionally, it includes a lab section with practical exercises to reinforce the concepts learned in the tutorial.

Uploaded by

bazmitch362
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week01: Python Practice

Tutorial:
During this course you will need to use Python for several different reasons, and it is best to
get familiar with it at the very beginning. Some of the applications of Python in this course
are:

• Data processing: in some of the labs you will be collecting data from the servers or
other components of a cyber physical system. You should be able to process the
data, this data is usually in form of a vector and collected in a period of time with
multiple elements (multiple dimensions). For example, you could be collecting the
system pressure, liquid-level in a tank, state of valves etc in a period of time (e.g. 10
minutes). Therefore, it is necessary to be able to collect, handle and manipulate
data. So, review the Python’s native variables that can hold more than a single value
(an array). These data types are:
o Lists: a list is defined with square brackets [1,2,3,4]
o Tuples: a tuple is defined with parentheses (1,2,3,4)
o Elements can be appended to the end of the lists using append() command
o To find maximum, minimum of the data Numpy’s min and max

• Collecting data: In some of the labs it is necessary to create a client to collect data
from server(s) or send data to it. In some scenarios it might be necessary to keep
collecting the data for a certain period or at least a certain number of data points.
This can be achieved by collecting the data in a loop (for or while). It’s necessary to
get familiar with using the loops to automate data collection for a period of time.
o for: repeats certain actions (for example requesting data) for a certain
number of times. As soon as it reaches a pre-determined number of
repetitions it will stop and exit the loop.
o while: repeats certain actions (for example requesting data) until the pre-
determined condition is changed or manually interrupted.
o Remember, to automate a number of processes you need to write them
inside the loop and in Python you achieve that by using indentations.

• Checking condition: To check the condition of a returned data, detect anomalies or


parameter state “if” statements or logical statements such as equal, smaller or
greater than are used (==, <, >). To process an array of data you can either right a
for loop to repeat the processing for each element, however, sometimes it might be
easier to first convert the data into a Numpy array and then use logical operators on
the data at once without using a loop.

• Data visualization: Graphs can be used to observe, understand and analyse the
data. Graphs make the process understanding the data easier and faster. Often you
would be turning multiple data points that has been collected over time into a graph
to observe how they change and the behaviour of the system. Matplotlib is one of
the libraries that handles data visualization in Python. Graphs can be created with
[Link] command. For more info you can visit
[Link]

Lab:
1. Write a for loop that repeats for exactly a 100 times. Each time that the loop repeats add 1
to a variable named “a”, the initial value of the a is 0 before the loop starts (a=0)
1.1. What is the value of the “a” at the end of the loop?
1.2. Print this value

2. After completing task 1, now create an empty list, called “b” outside the for loop you created
in task 1. Inside the for loop append the new value of the a to the end of the b.
2.1. What is the length of the b at the end of the loop?
2.2. What are the first 4 numbers in the list b?
2.3. What are the last 4 numbers in the list b?
2.4. Replace the value of the 50th element in the list with -1
2.5. Convert the list b into a numpy array (use [Link]() function) and call the array “c”
2.6. Find all the values in the c that are bigger than 70 using logical operation (>)
2.7. Try to repeat the same procedure in task 2.6 but this time try to do it with the list b (not
the array c)
2.8. What was the outcome of the task 2.7? You should have got an error unless you used a
for loop to process every data point one by one.
2.9. Replace the first 10 elements of b with value of 1 (you need to do slicing).

3. Create three random Numpy arrays call them x, y and z. One with 150 elements and the
other two with 150 elements.
3.1. Plot these values one by one in one graph to see them all.
3.2. Add a legend to the graph to be able to tell the graphs from each other.
3.3. Add title, and x and y labels to your graph

4. Create a function called myfunction, the function should take 1 numerical list as input and
such (example list could be L = [1,2,3,4,5,6,7,8]) and then multiply the values in the list by 2
and return it back a new list.
4.1. Do this task with the for loop in the function
4.2. Repeat this task with loop in the main body

You might also like