Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

IoT System LU 3. Python Programming Language

The document provides an overview of Python programming, covering its definition, uses, and fundamental concepts such as data types, variables, functions, and control structures. It highlights Python's versatility in various fields, including web development, data science, and machine learning, and introduces essential libraries like NumPy and Pandas for data manipulation. Additionally, it outlines the setup process for a Python environment and offers guidance on creating and using arrays.

Uploaded by

vodlouis1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IoT System LU 3. Python Programming Language

The document provides an overview of Python programming, covering its definition, uses, and fundamental concepts such as data types, variables, functions, and control structures. It highlights Python's versatility in various fields, including web development, data science, and machine learning, and introduces essential libraries like NumPy and Pandas for data manipulation. Additionally, it outlines the setup process for a Python environment and offers guidance on creating and using arrays.

Uploaded by

vodlouis1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Module name: IoT SYSTEMS

Module code: ETTIT701


Credits: 12
Competence: Develop IoT System
Prepared by: NIRERE Gaudence

ACADEMIC Year: 2024-2025


Python Programming Language
What is python?
0 High level, Interpreted, general-purpose programming language
0 Python is interpreted, meaning the code is executed line by line,
making it faster for development.
0 Python is a general-purpose language, meaning it can be used to
create a variety of different applications.
0 It was created by Guido van Rossum, and released in 1991
0 It is a great choice for beginners due to its readability and vast
learning resources.
0 Widely used among the data science community
Where can Python be used?
Why Python?
Python is versatile and used in various fields:
0 Web development (websites like YouTube and Instagram)
0 Data science (analyzing large datasets)
0 Machine learning (creating intelligent applications)
0 Automation (repetitive tasks)
0 Game development (creating fun and interactive games)
How to use Python
0 Install python from: https://www.python.org/downloads/
0 Create a file with the .py extension
0 Open a terminal and run python filename.py
0 Launch Jupyter notebook from anaconda.
0 Or we can simple use google colaboratory notebook:
https://colab.research.google.com/notebook
Setting up Python Environment Setup
0 Install Python : https://www.python.org/downloads/
0 Download and install the latest version of Python
Python programs
Python programs are built with fundamental concepts:
0 Data Types: Numbers (integers, floats), Text (strings), True/False
(booleans)
0 Variables: Naming and storing data
0 Operators: Performing calculations (+, -, *, /) and comparisons
(==, !=, <, >)
0 Functions: Reusable blocks of code
0 Loops: Repeating a block of code
0 Conditionals: Making decisions based on conditions (if
statements)
Data Types
0 In Python, data types define the kind of information a variable
can hold.
0 Python is loosely typed language. Therefore, no need to define
the datatype of variables
0 No need to declare variables before using them.
0 The program figures it out at runtime.
0 Python supports several data types, including integers, floats,
strings, booleans, lists, tuples, dictionaries, and sets.
0 These numeric data type, string and float
Python Data types
Data types Classes Description

Numeric Int, float, complex Hold numeric values

String Str Holds sequence of characters

Sequence List, tuple Holds collection of items

Mapping Dict Holds data in key-value pair form

Boolean Bool Holds either True or False


Data Types: Numbers (Numeric)
0 Integers (int): Represent whole numbers (positive, negative, or zero).
Example: 1,2,40
0 Floats (float): Represent decimal numbers.
Example: 1.2, 3.4 etc
0 Complex numbers: Represent numbers with a real and imaginary part
(a + bi)
Example: 12+j, 1+3j, etc
Data Types: String
0 Strings are sequence of characters (text).
0 It is written using single or double quotes.
Example:
Str=“Welcome to Python Programming language”
or
Str=‘Welcome to Python Programming language’
Data Types: Boolean (bool)
0 Boolean represent logical values: True or False.
0 Examples:
True (indicates something is correct),
False (indicates something is not correct)
String operations
0 Concatenation: ‘good’ + ‘morning’ = good morning
0 Repetition: ‘Hello’ * 2 = HelloHello
0 Slicing : str= ‘Florentine’
str[1:5] =
0 Indexing
0 Find():
0 Replace()
0 Strip()
0 Count()
Data Types: Lists
0A list is a sequence of mutable Python objects like floating number,
string, literals, etc.
0 Allows duplicate
0 The lists can be modified
0 Lists are defined using squarebracket [ ]
Example: MyList = [‘Banana’, ‘orange’, ‘Mango’]
Lists specific method:
0 append (value)
list=[1,‘a’,2.5] list.append(‘d’) [1,‘a’,2.5,’d’]

0 extend(list)
list=[1,‘a’,2.5] list.extend ([‘c’,’d’]) [1,‘a’,2.5,’c’,’d’]

0 Insert(index, value)
list=[1,‘a’,2.5] list.insert(2,’b’) [1,‘a’,’b’,2.5,]
0 Pop()
list=[1,‘a’,2.5] list.pop( ) [2.5]
Data Type: sets

0 A set is an unordered collection of unique elements.

0 Every element is unique (no duplicates) and must be immutable


(which cannot be changed)
0 Sets items are enclosed within curly braces { }
Sets specific methods
0 Creating set
mySet = {1,2,3} {1,2,3}

Having two sets: myS1={1,2,’c’}


myS2={1,’b’,’c’}
0 Union: myS1 | myS2 {1,2,’c’,’b’}
0 Intersection : myS1 & myS2 {1, ‘c’}
0 Difference: myS1 - myS2 {2}
Data type: Dictionaries
0 Dictionaries are perhaps the most flexible built-in data type in
python
0 Dictionaries items are also enclosed within curly braces { }
0 Key-value pairs used to store data collections
0 Dictionary doesn’t allow duplicate data
Dictionaries Examples
0 Creating empty dictionary myDict={}

0 Dictionary with integer keys myDict={1: ‘Eric], 2: ‘Marry’}

0 Dictionary with mixed keys myDict={Name: ‘Eric], 1: [2,4,6]}


Data Types: Tuples
0 A Tuple is a sequence of immutable Python objects like floating number,
string, ect. Tuples are sequences, just like Lists
0 Allows duplicate members.
0 The differences between tuples and lists are:
1. Tuples cannot be changed unlike lists
2. Tuples use parentheses/curve braces
Variables
0 Variable is small memory allocation
0 A variable is created the moment you first assign a value to it.
0 Variables do not need to be declared with any particular type
and can even change type after they have been set.
0 Example: X=5
Y=“Peter”
print(X)
print(Y)
Assigning a variable
0 Assigning a variable in programming refers to the process of associating a value
with a symbolic name (the variable name) so that the program can refer to and
manipulate that value using the given name.
0 It is essentially storing a piece of data in a memory location and giving it a label
for convenient reference.
To assign a variable, we need three components:
0 Variable name: We can decide what we would like to name our variable but keep
in mind it can be short and descriptive. We will learn about certain naming
conventions we must follow in a bit.
0 Assignment operator: The variable name will be followed by the assignment
operator. The assignment operator is an equal sign (=).
0 The value: The value we assign to a variable can be numerical or consist of
characters, known as strings. In this part of the lesson, we will only be focusing on
using numerical values.
Assignment operator image
Why we assign values to a variable
We assign values to variables for the following reasons:

0 To label and store information in memory.


0 This information can then be referenced later on and even be manipulated.
0 It makes it easier for a reader, and even ourselves, to understand our code.
Rules for Python variables:
0 A variable name must start with a letter or the underscore
character
0 A variable name cannot start with a number
0 A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
0 Variable names are case-sensitive (age, Age and AGE are three
different variables)
Functions
0 In Python, a function is a group of related statements that perform a
specific task.
0 A function is a block of organized, reusable sets of instructions that is
used to perform some related actions.
Built in functions
0 Python offers a variety of built-in functions that have been constructed
on our behalf; which we can quickly and easily utilise.
0 Follow the https://docs.python.org/3/library/functions.html to access a
list of all built-in Python functions.
0 Built-in functions come with a set of allowances and constraints that
must be obeyed in order for the function to perform properly.
0 These rules are listed in what we call the documentation of the function.
0 This is like the guideline for what the function is and how to use it. To
access the documentation of a function, we can use Python's help ( ).
Used Defined Function
0 Syntax: 0 Keyword def: marks the start of the function header.
def function_name(parameters) : 0 A function name to uniquely identify the function.
0 The Parameters (arguments) through which we pass values to
“”” docstrings ””” a function. They are optional.
statements 0 A colon (:) to mark the end of the function header.
return [expressions] 0 Optional documentation string (docstring) to describe what
the function does.
0 One or more valid python statements that make up the
function body. Statements must have the same indentation
level (usually 4 spaces).
0 An optional return statement to return a value from the
function. The return statement is used to exit a function and
go back to the place from where it was called.
User Defined Function cont’d

0 The first string after the function header is called the docstring and is
short for documentation string. It is briefly used to explain what a
function does.
0 Although optional, documentation is a good programming practice,
always document your code.
How to call a function in python?
0 Once we have defined a function, we can call it from another function,
program or even the Python prompt.
0 To call a function we simply type the function name with appropriate
parameters.
How Function works in Python?
Why do we use functions?

0 Re-usability of code minimizes redundancy


0 Functions help break our program into smaller. As our
program grows larger and larger, functions make it more
organized and manageable.
0 Procedural decomposition makes things organized
Operators in Python
Arithmetic operators
Assignments operators
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Python Conditions
Python supports the usual logical conditions from mathematics:
0 Equals: a == b
0 Not Equals: a != b
0 Less than: a < b
0 Less than or equal to: a <= b
0 Greater than: a > b
0 Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if conditional
statements" and loops.
If conditional statement
0 Syntax:
if conditional expression:
Body of if

0 N.B: In Python, to set a block of code we need to use indentation


(whitespace)
Loops: for loop
0 for loop in Python is used for iterating Python sequence that is either
tuples, lists, sets, dictionaries or string.
Syntax:
for var in sequence/range(args):
Body of for loop
0 Here, var is the variable that takes the value of the item inside the
sequence on each iteration.
0 Loop continues until we reach the last item in the sequence.
0 The body of for loop is separated from the rest of the code using
indentation.
Loops: while loop
0 The while loop in Python is used to iterate over a block of code as long as
the test expression (condition) is true.

Syntax :
while test_expression:
Body of while

0 In the while loop, test expression is checked first.


0 The body of the loop is entered only if the test_expression evaluates to
True.
0 After one iteration, the test expression is checked again.
0 This process continues until the test_expression evaluates to False.
Mostely used Python Libraries
0 Python libraries are collections of modules that contain useful
codes and functions, eliminating the need to write them from
scratch.
0 There are huge amount of Python libraries that help in python
programming.
0 Among them: Numpy, pandas, Matplotlib, Scipy, requests
Python libraries
NumPy
0 : A fundamental library for scientific computing in Python. It provides
efficient data structures like arrays and matrices for numerical
operations and data analysis commonly used in IoT sensor data
processing.
Matplotlib:
0 A popular library for creating static, animated, and interactive
visualizations of data. It's useful for visualizing sensor data and creating
informative charts and graphs for IoT applications.
Pandas
0 Built on top of NumPy, Pandas offers high-performance, easy-to-use data
structures and data analysis tools. It's great for data cleaning,
manipulation, and analysis tasks in IoT.
SciPy:
0 A collection of algorithms and functions for scientific computing. It
extends the functionality of NumPy and provides advanced mathematical
algorithms relevant to IoT applications.
TensorFlow/Keras
0 Popular libraries for machine learning and deep learning.
0 These libraries are helpful for building intelligent systems that can learn
from sensor data and make data-driven decisions in IoT applications.
Arrays
0 An array is a fundamental data structure used in programming.
0 Arrays provide an efficient way to store and manage collections of similar
data items under a single variable name.
0 They offer an organized approach to data access and manipulation,
making them essential building blocks for various programming tasks.
Arrays

What are arrays? Benefits of arrays


0 Ordered collections of items of the 0 Organized data storage.
same data type. 0 Efficient access using indexing.
0 Accessed using an index 0 Easier data manipulation through
(position) within the array. looping.
0 Efficient for storing and managing
similar data.
Creating and using arrays in Python

0 Use the list() function to create an array.


0 Elements can be of the same data type (e.g., integers, strings).
0 Provide a list of values within the list() function.
0 Arrays can be empty initially.
0 Use the array name and index within square brackets []
0 Indexing starts from 0 (first element).
0 Use for loops to access each element.
0 Perform operations or retrieve data from each element.
Further readings
0 A guide to basic math operations in Python
0 Python documentation on built-in data types
0 Summary of Python data types
0 Tutorial on type conversion in Python
0 A description of how dictionaries work in Python
0 An introduction to lists in Python
0 Calculating mean, median, and mode in Python
0 A brief tutorial of For Loops

You might also like