Fiot U 3
Fiot U 3
Python is one of the most dynamic and versatile programming languages available in the
industry today. Since its inception in the 1990s, Python has become hugely popular and even
today there are thousands who are learning this Object-Oriented Programming language. If you
are new to the world of programming, you have already heard the buzz it has created in recent
times because of the features of Python and must be wondering what makes this programming
language special.
Python is a general-purpose high level programming language and suitable for providing a solid
foundation to the reader in the area of cloud computing. The main characteristics of Python are:
2) Python supports more than one programming paradigms including object- oriented
programming and structured programming.
3) Interpreted Language.
4) Python is an interpreted language and does not require an explicit compilation step.
5) The Python interpreter executes the program source code directly, statement by statement, as a
Processor or scripting engine does.
6) Interactive Language
7) Python provides an interactive mode in which the user can submit commands at the Python
prompt and interact with the interpreter directly
Features of Python
As a programming language, the features of Python brought to the table are many. Some of the
Easy to Code: Python is a very developer-friendly language which means that anyone and
everyone can learn to code it in a couple of hours or days. As compared to other object-oriented
programming languages like Java, C, C++, and C#, Python is one of the easiest to learn. Open
Source and Free: Python is an open-source programming language which means that anyone can
create and contribute to its development. Python has an online forum where thousands of coders
gather daily to improve this language further. Along with this Python is free to download and use
in any operating system, be it Windows, Mac or Linux.
Support for GUI: GUI or Graphical User Interface is one of the key aspects of any programming
language because it has the ability to add flair to code and make the results more visual. Python
has support for a wide array of GUIs which can easily be imported to the interpreter, thus making
this one of the most favorite languages for developers.
Object-Oriented Approach: One of the key aspects of Python is its object-oriented approach.
This basically means that Python recognizes the concept of class and object encapsulation thus
allowing programs to be efficient in the long run.
Integrated by Nature: Python is an integrated language by nature. This means that the python
interpreter executes codes one line at a time. Unlike other object-oriented programming
languages, we don’t need to compile Python code thus making the debugging process much
easier and efficient. Another advantage of this is, that upon execution the Python code is
immediately converted into an intermediate form also known as byte-code which makes it easier
to execute and also saves runtime in the long run.
Highly Portable: Suppose you are running Python on Windows and you need to shift the same to
either a Mac or a Linux system, then you can easily achieve the same in Python without having
to worry about changing the code. This is not possible in other programming languages, thus
making Python one of the most portable languages available in the industry.
Highly Dynamic: As mentioned in an earlier paragraph, Python is one of the most dynamic
languages available in the industry today. What this basically means is that the type of a variable
is decided at the run time and not in advance. Due to the presence of this feature, we do not need
to specify the type of the variable during coding, thus saving time and increasing efficiency.
Extensive Array of Library: Out of the box, Python comes inbuilt with a large number of
libraries that can be imported at any instance and be used in a specific program. The presence of
libraries also makes sure that you don’t need to write all the code yourself and can import the
same from those that already exist in the libraries.
Support for Other Languages: Being coded in C, Python by default supports the execution of
code written in other programming languages such as Java, C, and C#, thus making it one of the
versatile in the industry.
Data types are the classification or categorization of data items. Data types represent a kind of
value which determines what operations can be performed on that data. Numeric, non-numeric
and Boolean (true/false) data are the most used data types. However, each programming
language has its own classification largely reflecting its programming philosophy.
In programming, data type is an important concept. Variables can store data of different types,
and different types can do different things. Python has the following data types built-in by
default, in these categories:
Numeric
A numeric value is any representation of data which has a numeric value. Python identifies three
types of numbers:
Float: Any real number with a floating point representation in which a fractional
Boolean
Data with one of two built-in values True or False. Notice that 'T' and 'F' are capital. True and
false are not valid booleans and Python will throw an error for them.
Sequence Type
A sequence is an ordered collection of similar or different data types. Python has the following
built-in sequence data types:
String: A string value is a collection of one or more characters put in single, double or
triple quotes.
List : A list object is an ordered collection of one or more data items, not necessarily of
Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily
Dictionary
3:"Ram", 4: "Farha"}
type() function
Python has an in-built function type() to ascertain the data type of a certain value. For example,
enter type(1234) in Python shell and it will return <class 'int'>, which means 1234 is an integer
value. Try and verify the data type of different values in Python shell, as shown below.
Data objects of the above types are stored in a computer's memory for processing. Some of these
values can be modified during processing, but contents of others can't be altered once they are
created in the memory. Number values, strings, and tuple are immutable, which means their
contents can't be altered after creation.
On the other hand, collection of items in a List or Dictionary object can be modified. It is
possible to add, delete, insert, and rearrange items in a list or dictionary. Hence, they are mutable
objects.
Python allows its users to create their own Data Structures enabling them to have full control
over their functionality. The most prominent Data Structures are Stack, Queue, Tree, Linked List
and so on which are also available to you in other programming languages. So now that you
know what are the types available to you, why don’t we move ahead to the Data Structures and
implement them using Python.
As the name suggests, these Data Structures are built-in with Python which makes programming
easier and helps programmers use them to obtain solutions faster. Let’s discuss each of them in
detail.
Lists
Lists are used to store data of different data types in a sequential manner. There are addresses
assigned to every element of the list, which is called as Index. The index value starts from 0 and
goes on until the last element called the positive index. There is also negative indexing which
starts from -1 enabling you to access elements from the last to first. Let us now understand lists
better with the help of an example program.
Creating a list
To create a list, you use the square brackets and add elements into it accordingly. If you do not
pass any elements inside the square brackets, you get an empty list as the output.
1
2
3
4
my_list = [] #create empty list
print(my_list)
my_list = [1, 2, 3, 'example', 3.132] #creating list with data
print(my_list)
Output:
[]
[1, 2, 3, ‘example’, 3.132]
Introduction to Raspberry Pi
Raspberry Pi is a low-cost mini-computer with the physical size of a credit card.
Raspberry Pi runs various flavors of Linux and can perform almost all tasks that a normal
desktop computer can do. Raspberry Pi also allows interfacing sensors and actuators
through the general purpose I/O pins. Since Raspberry Pi runs Linux operating system, it
supports Python "out of the box".
The Raspberry Pi was developed by the Raspberry Pi Foundation, a UK charity, with the
goal of encouraging the teaching of basic computer science, and Eben Upton is the
founder and inventor of the Raspberry Pi.
The Raspberry Pi 5, announced in September 2023, is a significantly more powerful
version of its predecessor, the Pi 4, featuring a 2.4 GHz quad-core 64-bit ARM Cortex
A76 CPU and a VideoCore VII GPU, with a new in-house designed I/O controller.
2) Temperature Sensing
CODE
import time
sensor = W1ThermSensor()
while True:
temperature = sensor.get_temperature()
time.sleep(1)
The code imports the w1thermsensor library, which simplifies interaction with
the DS18B20 sensor.
It initializes the sensor object.
The while True loop continuously reads the temperature using
sensor.get_temperature().
The temperature is printed to the console with two decimal places.
time.sleep(1) pauses the execution for 1 second before the next reading.