Category Archives: Programming

Programming is the process of designing, coding, debugging and maintaining the source code of programs

JDBC

JDBC

JDBC is a set of Java classes and interfaces for executing statements SQL

It is the CLI part of Java (Call-Level Interface)

It was jointly developed by JavaSoft, Sybase, Informix and IBM among others

JDBC allows the manipulation of any SQL database

You don't need to do a specific program to manipulate Oracle, Sybase, etc...

Our program can handle any database

Bringing together Java with JDBC, we get programs that can run on any platform, and they can manipulate any database

The classes and interfaces JDBC is located within the package java.sql

Process of working with JDBC

The JDBC worker process consists of the following steps:

  1. Connect to the database (using DriverManager)

  2. Issue SQL statements (using Statement, PreparedStatement, CallableStatement)

  3. Processing the results (ResultSet)

JDBC Driver

Each particular database implements the JDBC interface in a particular way, as do the helper and utility classes that are required for that database

That is why they are necessary drivers of databases

The same application will be able to connect to different databases simply by changing that driver (we should not confuse the database driver with the class Driver, because they are different things)

The interface Driver specifies the methods that any JDBC driver must implement, so drivers can be loaded in two ways:

  1. When you start the class Driver, the DriverManager consultation the property jdbc.drivers

    This property contains a list of drivers (classes) that must be loaded

    To do this, you must run a command like this (to use ODBC) on the command line:

  2. If you want to enter a new driver after the DriverManager is initialized we should use the method forname of the class Class

    To do this we must include in our code the following statement (to use ODBC):

    It is advisable to use of static in our application (because the driver will only load once, when loading the class)

Examples of drivers
Database Driver
ODBC sun.jdbc.odbc.JdbcOdbcDriver
Oracle oracle.jdbc.driver.OracleDriver
SQLServer com.microsoft.jdbc.sqlserver.SQLServerDriver
MySQL com.mysql.jdbcDriver

Tipos de driver:

  • Bridge JDBC-ODBC

    Translates JDBC to ODBC and then relays it to the ODBC driver of the machine

    Is the ODBC driver actually communicates with the database

    Is included in the JDK but does not include JDBC2

    Inconvenience:

    • It's useful for testing, but it's slow in production

    • ODBC driver required on client (lower portability)

  • Driver JDBC on a native driver of the database

    Relays JDBC the native driver installed on the machine

    The native driver is the one that actually communicates with the database

    Drawback:

    • need for native driver (lower portability)
  • Driver Java on network

    Translates JDBC calls into a network protocol independent of the platform that contact with the server

    The server translates these requests to the specific protocol of each database

    It uses a middleware on the network server that is capable of connecting customers with pure Java to many different databases

    Advantage:

    • is fast, platform-independent and requires no installation on the client

  • Driver pure Java and native protocol

    Translates the JDBC calls to the specific protocol of the database by directly contacting it

Get connections

Using the specific driver we will make the connections, but connection requests must be made using the DriverManager

Once the class Driver has been uploaded and registered, the DriverManager you can establish connections to the database by using these two steps:

  1. The method is called DriverManager.getConnection(url, user, pass); and you get an object of type Connection

  2. The DriverManager tests the registered drivers to see if you can establish the connection and if it wasn't possible, launch a SQLException

A single application can have multiple connections to the same database or multiple connections to other databases (up to the maximum allowed by the database)

The parameters supported by getConnection are the url (which is a subprotocol that uses the database to make the connection), the user (the username to be connected) and the pass (the password that the user uses to connect)

The parameter url has the following format:

The subprotocol is particular to each database, and uses the DriverManager to find the proper driver to handle it

The subname depends on the subprotocol specific

The driver will be responsible to interpret and will help you to locate the database

Our app won't work with the specific driver, but will work on the DriverManager

In this way applications can work with the object Connection without worrying about the type of database we're working with (no code modifications need to be made, just change the parameter url)

The connection should always be closed at the end, because otherwise resources are consumed unnecessarily, although connections can be reused, it is not advisable, it is better to always use a new connection

In the example we have used a skeleton of connection to Oracle

To use the connection we need to create an object of type Connection, which represents an open session with the database

The object provides a context with which to issue SQL statements and get results

The object Connection must be initialized to an initial null, so we can check if there was a connection because we'll include a block try to handle bugs, which will launch SQLException

To close the connection must be made using the method close(), within a finaly (which will also have its block try that spear SQLException)

The interface Statement

The connection allows us to create objects Statement by using the method createStatemen()

The objects Statement allow sql statements to be executed and results obtained (using the objects ResultSet)

The methods to execute SQL accept String (JDBC is CLI), which can be composed dynamically based on values contained in variables

To compose the String SQL, concatenate the different SQL fragments (static and variable)

There are three ways to execute SQL statements:

  1. By using the method executeQuery(<sql>) for queries using the statement SELECT that produces tuples as a result (returns an object of type ResultSet)

    • It is like a table that stores the result of the query

    • You have a number of query methods

    • It's like a cursor (in PL/SQL terminology)

  2. By using the method executeUpdate(<sql>) for updates by using the sentences INSERT, DELETE, UPDATE, as well as DDL commands (CREATE, DROP, ALTER TABLE, ADD) and PL/SQL blocks (between begin and end blocks)

    • Returns an integer that indicates the number of rows modified by the command

    • With DDL commands it returns 0

  3. By using the method execute(<sql>) that executes any SQL statement

    • If we used a QUERYReturn True

      • The ResultSet can be obtained by the method getResultSet()

    • If we used a UPDATEReturn False

      • The total number of modified rows can be obtained by using the method getUpdateCount()

After processing the result, the Statement by using the method close()

This method will also close the ResultSet associate, however, Sun recommended closing the ResultSet explicitly to avoid errors unwanted

The same Statement can be reused in the same connection to execute different statements

The interface ResultSet

The object ResultSet acts as a cursor within the results

The first time you read it points to the first result, but does not read it, so you have to read the first row moving forward using the method next()

The method next() returns True if you were able to move forward or False if you could not

To obtain the values of the ResultSet methods are used get that have the following format get<type>(<column>)

To name the columns we can either do it by name or by an index, which starts at 1, their numbering is given by the order in which it was entered in the SELECT

You have to pay attention to the dates, because they are kept as java.sql.Date, not as java.util.Date as you might expect

It should be close ResultSet although it is implicitly closes when you close or reuse the Statement that created it

When you have read a null SQL using one of the methods get<tipo>, it returns:

  • A value null Java for those methods that return Java objects (getString(), getBigDecimal(), getDate(), getTime(), getTimestamp(), getObject(), etc)

  • A value of 0 for those methods that return numeric types (getByte(), getShort(), getInt(), getLong(), getFloat(), getDouble() )

  • A value False for the method getBoolean()

To determine if a given value was null, you must first try to read the column and use the ResulSet wasNull() to know if it was null

Return True yes it was, False otherwise

The interface PreparedStatement

Every time you launch a Statement the database must interpret it and calculate a query plan

But when using PreparedStatement, can be executed multiple times, getting an increase in performance by having the query already analyzed and optimized

Not all databases support PreparedStatement, you have to read the documentation of it to know if they can be used

The objects PreparedStatement derived from the Statement obtained from the connection, using the prepareStatement(<sql>);

A PreparedStatement to launch precompiled SQL statements, you can parameterize one or more entries using the ?, whose values may be changed in different executions of the statement

Parameterize entries is useful when we do not know the values of certain SQL data types in the target database,

The parameterized value gets the correct value from the database driver, so we won't have to worry about the types

Before the SQL statement can be executed we will need to assign a value to the input parameters

To assign values use the methods set<type>(column, value); being the type compatible with the parameter

To run methods are used execute of Statement

In the example we have used a PreparedStatement for Oracle

Python

Python

Python is a widely used programming language currently in Data Science and Big Data related environments

Its use in libraries created by Google and other tech giants has made it one of the most widely used languages

Within programming languages, its features are:

  • Interpreted

    The compilation process is the translation process of the programming language that we are using (e.g. C++) to machine code that the computer can run

    In addition, this process creates an executable file that can be used on other computers

    There are programming languages that do not use a compiling process but an interpreter, which adds meaning to scheduled statements

    Python is an interpreted language

  • Dynamic typing

    Typing a programming language is the language's way of managing variables and their types

    Usually, in a programming language you can create variables of different types (whole number, actual number, character, lists...)

    There are languages in which before we can use a variable we have to define it and assign it a specific type of variable

    In a non-formal language it would be something like: I want an entire variable called Brothers and Assigns the home variable the value 3

    However, in Python you don't need to make this mapping, because it's a dynamic typed language, and you generally don't need to define a variable before you can use it

    In Python we would write directly The variable brothers has a value of 2 and it would be the interpreter who would see that we want to use a variable that does not exist, with the name Brothers and assign the value 2

  • Multi paradigm

    There are several ways to program; with object oriented programming as functional programming

    Python admits both types

  • Multi platform

    There are Python interpreters for multiple platforms, such as MS Windows, Mac OS or Linux

Both the Python interpreter and its extensive standard library are freely available in the form of source code and executable file for major platforms from the Python

Behind Python there is a large community that provides extensive support while constantly developing and updating a centralized library array in a repository called Pypi

There is also a code of good programming practices in Python as well as a style guide, so that the code is easy to read and understand

Installing and using Python modes

Python can be installed on our computer from the Python page (recommended to install the latest version of Python)

However, it is more convenient to install using a distribution such as Anaconda, which downloads the interpreter, different editors and the most used packages and libraries

To install this distribution we would have to:

  1. Go to the downloads page Anaconda and download the package that fits your desired operating system and Python version

    It is recommended to install the one corresponding to the most current version of Python

  2. Run the package and let it install completely on the system
  3. After installation it would be advisable to update the packages

    To do this open a terminal and run:

    It will display a list of libraries to be updated

    If you agree, accept and start downloading and installing the libraries

  4. In the case of wanting to install a specific library, in the terminal we will run:

    You will install the latest version of the library present in the Anaconda repository that corresponds to our operating system and our version of Python

In case it is not present in the repositories, we can optionally install the libraries with pip

To do this open a terminal and run:

For more information, see the Anaconda documentation

The fastest way to use Python is by using command line

Once we have an interpreter installed on our computer, we would open a terminal and write

This opens a console on which we can write and run Python code

Each line we executed would perform the requested operation

It's a very quick way to test code but it's very impractical, as we lose the reuse of executed code

That is, if we wanted to run it again, we would have to write the code line by line again

The most common thing is to use an editor as a Spyder (installs next to Anaconda) the Jupyter notebooks, which is the one I'm going to use for the examples

Jupyter Notebook

Jupyter Notebook (formerly IPython Notebook) is an open source web application that lets you create and share documents that contain live code, equations, visualizations, and narrative text

In addition, it is a widely used application in the field of Data Science to create and share documents that include: data cleansing and transformation, numerical simulation, statistical modeling, data visualization, machine learning and much more

It allows you to edit and run notebook documents through any web browser of your choice; and can run on a local desktop that does not require Internet access or can be installed on a remote server and accessed over the Internet

We can also execute Jupyter Notebook without any installation from the project's website

Or install it using pip, open a terminal and run:

Use Jupyter Notebook

Once we have it installed, we will open a terminal and run:

This will print some information about the notebook server in our terminal, including the web application URL (by default, http://localhost:8888)

In case you need to know the path where we have Jupyter installed, we can run in the terminal:

In case you need to know the version of Python that we have installed, we can run in the terminal:

Before running this one-line command, it is recommended that we verify that our Anaconda PATH directory has been successfully added to the Environment Variables, and if not, we should locate our Anaconda directory/file path and add it to the Environment Variables, or if you could not get an error:

jupyter is not recognized as an internal or external command

If the launch has worked properly, our default browser will open with the Notebook open on port 8888

Once inside the Notebook, we will see a button called New which, when clicked, allows us to select a Python kernel (the options depend on what is installed on our local server) from the drop-down menu

It is recommended to select the latest version of Python that appears in the drop-down, unless you need to use another version for compatibility reasons

Once selected and accepted, we will open our first notebook to write our programs in Python

Our first Python script

Let's write a small sample script to show how simple it is to run a Python script from Jupyter

Python has a defined storage system and variable management

In variables we can save information that we want to use later in the code with a name that is easy for us to remember

In our example, we want to calculate the area and volume of a sphere

The equations we need are:

  • A = 4\pi \cdot r^2 for the area
  • V = \frac{4}{3}\pi \cdot r^3 for the volume

Specifically, we want to calculate these values for a 7 radio sphere

Let's define three variables:

  • r

    It contains the radius value, in this case 7

  • A

    Contains the value of the calculated area

  • V

    Contains the value of the calculated volume

What advantages do we get by defining variables?

If we change our minds and instead of a radio 7 we want it to be 6, we would have to change the number of two sites (which in the case of a program in production could be hundreds)

Or we want the user to indicate the value, so we can't know a priori

To write it in Python we will use a cell of the notebook to which we will assign it in the drop-down menu (we will see it to the left of the icon that looks like a keyboard) the Code value

Once inside the cell, we will use it as a text editor by typing:

To run it we can use the run button or by leaving the uppercase key and then pressing enter

The result of the variables will remain in memory, but we won't be able to see it

Now let's use the print function (similar to the C++ function) to be able to see the result of our Python script

In order to use the constant Pi, the math module has been imported with the statement import math and then we've used it with math.pi

As you can see, to define a variable we write the name with which we want to baptize it, the sign and the expression that we want to be assigned to that variable

Comments on Python

Python supports Unix Shell commentary

Operators in Python

Operators in Python

Python has arithmetic, relational, logical, bit by bit, assignment, membership, and identity operators

Arithmetic operators

Python supplies basic operations with the only additions of the remaining operators, whole quotient and exponentiation

Arithmetic operators
Operator Operation Description
+ Addition Add two operands
Subtraction

Subtract the value of the right operand from the left operand

Used on a single operand, it changes the sign

* Multiplication Multiplication of two operands
/ Division Divide the operand from the left to the right (the result is always a float)
% Rest Gets the rest of dividing the operand on the left by the one on the right
// Integer quotient Gets the integer quotient of dividing the operand from the left to the right operand
** Exponentiation The result is the left operand raised to the power of the right operand

Note In Python, using the + operator applied to strings concatenates both strings into a single

Relational operators

Are used typically in the conditional expression

The relational operators return boolean values

The operands can be numerical or strings

Relational operators
Operator Operation Description
> Greater than

True if the operand on the left is strictly greater than that on the right

False otherwise

< Less than

True if the operand on the left is strictly less than the one on the right

False otherwise

> = Greater than or equal to

True whether the operand on the left is greater than or equal to that of the right

False otherwise

< = Less than or equal to

True if the operand on the left is less than or equal to that of the right

False otherwise

! = Other than that

True if the operands are different

False otherwise

= = Like

True if the operand on the left is the same as the one on the right

False otherwise

Objects of different types, except numeric types, are never compared the same

The operator == it is always defined, but for some types of objects (for example, class objects) it is equivalent to is

Non identical instances of a class are typically compared as non equal unless the class defines the method __eq__()

Instances of a class cannot be sorted with respect to other instances of the same class or other object types, unless the class defines the methods __lt__() and __gt__()


Logical operators

The logical operands is related to the relational as they are normally the operands used are the result of expressions in relational

The resulting values are boolean

Logical operators
Operator Description
AND True if both are True
OR True if one of them is True
NOT It was True moves on to False and vice versa

Bitwise operators

The way of working of these operators is to convert to the binary operands and then operate with them bitwise

Bitwise operators
Operator Operation Description
& AND Change the bit to 1 if both were 1
| OR Change the bit to 1 if one of them was 1
^ XOR Change the bit to 1 if one of them was 1, but not both
~ NOT

Change the bit to 1 if it was 0

Change the bit to 0 if it was 1

<< Propagation to the left Shift the value to the left by entering zeros, if it goes out of range, values are lost
>> Spread to the right Moves the value to the right entering by the left, the sign bit and eliminating the values that are out by the right

Note Propagation operators take two operands: the first is the variable to propagate and the second is the number of positions to propagate

Assignment operators

The assignment is also an operator that returns the variable modified

The mapping operator in Python is =

The assignment operators shown below are but abbreviations that make expressions more comfortable and simple, even if they are sometimes more unreadable

Assignment operators
Operator Expression Equivalence
=

A = B = C

D = ‘Text’

A = C

B = C

D = ‘Text’

+ = A + = 4 A = A + 4
– = A – = 3 * B A = A – (3 * B)
* = A * = 2 A = A * 2
** = A ** = 2 A = A ** 2
/ = A / = 35 + B A = A / (35 + B)
% = A % = B A = A % B
// = A // = B A = A // B
>> = A >> = 1 A = A >> 1
<< = A << = B A = A << B
& = A & = (C + = 3) C = C +3
A = A & C
^ = A ^ = 2 A = A ^ 2
| = A | = C A = A | C

Membership operators

Membership operators are used to check whether a value or variable is in a sequence (list, tuple, dict, set or str)

Membership operators
Operator Expression Equivalence
in Included

Returns True if the value is in a sequence

False otherwise

not in Not included

Returns True if the value is not in a sequence

False otherwise

Identity operators

These operators will be used to check whether or not two variables are the same object

Membership operators
Operator Expression Equivalence
is Equal objects

Returns True if both operands refer to the same object

False otherwise

is not Different objects

Returns True if both operands do not refer to the same object

False otherwise

Preference

The operator precedence will determine the order in which they are running in a given expression

Using parentheses will check that the operations are carried out according to us we want to

In Python, the preference for operators from highest to lowest is as follows:

Preference
Parenthesis ( )
Exponentiation **
Unaries ~ +
Mul / Div / Rest / Div integer * / % //
Addition / Subtract +
Spread << >>
Bit by bit &
Bit by bit ^ |
Relational < < = > > =
Equality = = !=
Assignment = + = – = * = * * = / = // = % = & = | = ^ = >> = << =
Identity is is not
Membership in not in

Data types in Python

Data types in Python

Types in Python are a representation of data because it does not require variables to declare their type, because all types are automatically converted

We can use the function type to know the type of a variable

Type boolean

The Boolean type simply distinguishes between two states, a success or enabled state, true value, True, and a failure or deactivated state, false value, False

Both states are not case sensitive

Typically, the result of operators that return a Boolean value are passed to the control structures

Numeric types

In Python there are 4 different types of numeric variables:

  • int

    integer with fixed precision

  • long

    integer if the size of an int is exceeded

  • float

    double precision floating point number

  • complex

    complex number (real part + j imaginary part)

It is the interpreter who decides the type of variable according to what we define

However, we may want to force it ourselves through a cast

Type string

A string is a string of characters delimited by quotation marks

A string literal can be specified in two different ways:

  • single quote

    are specified between the characters opening and closing

    To specify a literal single quotation mark, you must escape with a backslash (\). To specify a literal backslash, it is duplicated (\\)

    All other instances of backslashes will be treated as a literal backslash: this means that other escape sequences that could be used, such as \r o \n, will be displayed literally as specified, instead of having any other special meaning

  • double quotes

    are specified between the characters " opening and " closing

    Python will interpret the following escape sequences as special characters:

    Code Description
    \n line feed (LF or 0x0A (10) in ASCII)
    \r carriage return (CR or 0x0D (13) in ASCII)
    \t horizontal tab (HT or 0x09 (9) in ASCII)
    \\ backslash
    \" double quotes

Methods

Let's look at some of the most everyday methods for strings

Formatting methods

They affect the formatting of the text contained in the string

  • capitalize()

    Make a copy of the string with the first letter in uppercase

  • lower()

    Returns a copy of the string in lowercase

  • upper()

    Returns a copy of the string in uppercase

  • swapcase()

    Returns a copy of the string in which they are case-sensitive and vice versa

  • title()

    Returns a copy of the string in which the first letter of each word is uppercase and the other lowercase letters

  • center(length, 'fill character')

    Returns a copy of the input string centered on the specified length and between the specified fill character

  • ljust(length, 'fill character'): Returns a copy of the left aligned string

  • rjust(length, 'fill character')

    Returns a copy of the left/right aligned string

  • zfill(length)

    Returns a copy of the string filled with zeros to the left until it reaches the indicated length

Search methods

They allow us to search within a certain string to check whether a substring is contained in the string or how many times a substring appears

  • count (‘substring’)

    Returns an integer representing the number of occurrences of the substring within the string

  • find(‘substring’)

    Returns an integer representing the position at which the substring begins within the string

Validation methods

They always give us back a boolean, that is, True or False

They serve to check if a chain meets a certain condition

  • startswith(‘substring’)

    It tells us if a string starts with a certain string

  • endswith(‘substring’)

    It tells us if a string ends up by a certain string

  • isalnum()

    It tells us if a substring is alphanumeric

  • isalpha()

    It tells us if a string is alphabetical

  • isdigit()

    It tells us if a string is numeric

  • islower()

    It tells us if a string contains only lowercase

  • isupper()

    It tells us if a string contains only capital letters

  • isspace()

    It tells us if a string contains only blanks

  • istitle()

    It tells us if a string is formatted as a title

Substitution methods

They allow us to replace text to unify the format of different strings that have come to us in different ways

  • format(arguments)

    We may have a chain that is prepared to receive a certain amount of arguments

    Calling this function and entering the necessary arguments returns a copy of the already formatted string

  • replace('searched substring', 'substring to put')

    Searches within the string for a substring and replaces it with the indicated string

  • strip()

    Removes the character we specify from the right and left of the string

    By default it looks for the space character

  • lstrip()

    Removes characters to the left of a string

    If we enter a substring as an argument, look for that substring and the combinations of the substring characters from the left until it finds no more

  • rstrip()

    Removes characters to the right of a string

    If we enter a substring as an argument, look for that substring and the combinations of the substring characters from the right until it finds no more

Methods of union and division

They allow us to replace text to unify different strings that have come to us by different ways or to separate a string into several

  • join(iterable)

    It receives as an argument an iterable and returns as a result the elements of the iterable joined by the character of the string on which it is applied

  • partition('separator')

    Returns a 3 elements tuple

    The first element is to the left of the 'separator', the second element is the separator, and the third element is to the right of the separator

  • split('separator')

    Returns a list of all items found when splitting the string by the separator

  • splitlines()

    Returns a list where each item is a fraction of the string divided into lines

Lists

They are defined by putting the contents of the list in square brackets, separating each of the elements by a comma

Each item in the list can contain items of different types

The lists are mutable, that is, their elements can be modified

In Python, items in a list are numbered from 0 to length – 1

To access the item in a list, the name of the list is put and then the index that we want to access (if we put the index with a negative sign will start at the end of the list)

To access a range within a list we have different options:

  • Take items from the start: lista[:a]
  • Take items from position to (included) to end: lista[a:]
  • Take items from a to b (not including b): lista[a:b]

The start of the range always includes the element in that position, but not the end of the range

Methods

Let's look at some of the methods for most everyday lists

  • append()

    adds an item to the bottom of the list

  • insert()

    is used to insert an item into the assigned index

  • pop()

    deletes and returns the value at the position of the assigned index

  • reverse()

    reorders the list in reverse

  • sort()

    rearrange the list in ascending order

We can find all the methods in your documentation

Tuples

Tuples are similar to lists, defined with parentheses instead of brackets

They have the peculiarity of being immutable

As you can see, items are accessed in the same way as with lists

Dictionaries

Dictionaries define a one to one relationship between key value between {} and they're mutable

They are defined by placing a comma separated list of pairs key:value

Once defined, we can access the value associated with a key by searching by key

In addition, we can search whether or not a certain key exists in our dictionary

Dictionaries are a type of collection in Python that, unlike lists, are indexed by words and not by numbers

Values can be of any type

We can think of a dictionary as an unordered set of pairs key:value with the requirement that the keys have to be unique

Methods

Let's look at some of the methods for more everyday dictionaries

  • keys()

    returns us an iterable containing the dictionary keys

  • values()

    returns us an iterable containing the dictionary values

  • items()

    returns us an iterable with the pairs key:value of the dictionary

Objects

Objects consist of a set of values, properties, and a set of methods applied to those values

It is these methods that allow us to modify the state of that object, that is, the value of its properties

In Python, objects have the following characteristics:

  • Everything is an object, including types and classes
  • Allows multiple inheritance
  • There are no private methods or attributes
  • Attributes can be modified directly
  • Allows monkey patching

    Classes are created at run time but can be modified after they are created

  • Allows duck typing

    The dynamic typing style of data in which the current set of methods and properties determines the semantic validity, rather than inheritance of a class

  • Allows operator overload
  • Allows the creation of new data types

Let's create a academic sample object where you have the properties: Name, Surname, Age and Identity card

We'll create the object instance by mapping it to a variable as follows:

It is possible to query the class of an object with the function type, but you can also query through its special attribute __class__

[/tp”]

A su vez las clases tienen un atributo especial __name__ que nos devuelve su nombre en forma de cadena sin adornos:

You can initialize all variables to have the kind we want, we will use the class constructor passing the arguments we need

The constructor is a method that is called automatically when you create an object and is defined with the name __init__ and through the reserved word self we can reference to the current object

If a constructor exists, there must also be a destructor that is called when deleting the object to handle cleanup tasks such as emptying the memory

All objects are automatically deleted from memory at the end of the program, although to remove them automatically we have the special method __del__

It is not advisable to overwrite this method because it is handled automatically, but it is interesting to know that it is possible

The method __str__ is the one that returns the representation of an object in the form of a string

It is called automatically is when we print a variable per screen with the function print

By default the objects print their class and a memory address, but we can change it by overwriting their behavior

For example, for the university object we overwrite the method __str__ to show us the name, age, surname and dni

An interesting special method is the one that returns the length

It is usually linked to collections, but nothing prevents it from defining it in a class and not redefining it, because by default it does not exist in objects even if it is the one that is executed when passed to the function __len__

In our example of the university object it doesn't make much sense to count its length, but I include it to show its use

The properties of an object can be described by other objects or even collections of them

The definition of methods associated with an object can be specified in the object definition

For example, for the university object we overwrite the method __str__ to show the student's first name, age, surname, dni and mean student's notes

To access the properties or methods we will use the operator .

Inheritance

Inheritance is the ability of one class to inherit attributes and methods from another, something that allows us to reuse code

To see its usefulness, let's develop an example by modifying the university object

We will start from a base class that will be the father (superclass) of other daughters who inherited from it (subclasses)

We start from a class Person that will contain all the common attributes and two child classes Student and Teacher who inherit from it and have their own attributes or methods

To inherit the attributes and methods of one class in another, you just have to pass it in parentheses during its definition

It is possible to use the behavior of a superclass without defining anything in the subclass, although in our example we will give them behavior

Thanks to the flexibility of Python we can handle objects of different classes massively in a very simple way

Let's start by creating a list with our two People from different subclasses

One of the few flaws in Python is that if we modify the values within an object, these changes will be reflected outside the same

Just as in collections, objects are passed to functions by reference

It affects when making copies, creating instead an access to the object instead of a new one with its previous values

To make a copy from its values we can use the function copy of the module with the same name

The function copy can also be used to copy collections

Multiple inheritance allows a subclass to inherit from multiple superclasses

This involves a problem, which is that if multiple superclasses have the same attributes or methods, the subclass can only inherit from one of them

In addition to superclasses you can also inherit multiple subclasses

In these cases Python will prioritize the leftmost classes at the time of subclass declaration

We've used the reserved word pass to instruct the interpreter to ignore the body code and use the one in the class object or the superclasses (subclases) we tell you to

Null value

In Python variables can be assigned a value that indicates the empty value, this value is the value NULL

A variable is considered None if:

  • has been assigned the constant None
  • has not passed an attribute to a function

The constant None is insensitive to case sensitive

Control structures in Python

Control structures in Python

For control structures, Python possesses the control statements typical of high-level languages

Variable statement

Variables in Python are not assigned a predefined type

In Python the type of variables depends on the value they contain at all times

Therefore it performs an automatic conversion of types

Python recognizes the following types of values:

  • Numbers
  • Boolean values

    true and false

  • Strings
  • Lists

    data type that associates values with a numeric value by putting the contents of the list in square brackets, separating each of the elements by a comma

  • Tuples

    data type that associates values with a numeric value by putting the contents of the list in parentheses, separating each of the elements by a comma

    They have the peculiarity of being immutable

  • Dictionaries

    type of data that associates a value with a key and allows you to search for those keys

  • Objects

    Created by the programmer or pre-defined by the language

As it is a language with dynamic typing, it is not necessary to tell the interpreter its type of variable, but it is he who decides the type that will assign

The names with which we can name variables in Python follow several rules:

  • Names always start by letter
  • Within the name you can use any letter or number within the utf-8 encoding
  • Python reserved words, which are used in your code syntax, cannot be used

    For more information on these names, see the Python documentation

Note that Python is case sensitive, so two variables with the same name but containing an uppercase letter will be understood as two distinct variables

For example Variable is different of variable

There are also certain conventions to reduce errors and improve readability

To avoid errors it is recommended not to use certain characters:

  • Do not use neither l (l lowercase) neither I (i uppercase), because depending on the type of font used they may be confused with each other or the number 1
  • Do not use Or (o uppercase) because depending on the type of font used it may lead to confusion with the number 0

In order to promote readability, the following suggestions are made:

  • variablename is unreadable, because it's all together and can make it difficult to read
  • variableName is a little more readable, when applying uppercase letter when starting word
  • variable_name is the one that brings the best readability, when applying underscore at word start, which is the most recommended way

The if sentence

The if sentence has the form:

If the condition is true, instruction 1 will be executed; otherwise it is executed if there is instructution 2

The use of the sentence else is optional, so brackets have been used in the definition

If omitted, the instruction block will only be considered when the condition is true

An instruction block is a set of tab statements after the statement if, when jumping from line after the symbol :

They are mandatory as they tell the interpreter that the instruction block belongs to the sentence if

If omitted and the condition was true, the following instruction will be executed

Otherwise, the subsequent instruction will be executed, independent of the condition state

In this way, the omission of tabulations after the sentence if will allow us to write everything on a single line after the symbol :

The elif sentence

We can also use sentences if anities by sentencing elif

Sentences elif work just like a sentence if

But they will only be executed in case the condition of the sentence if was false

We can optionally add a statement else in the end but will only be executed if all the above conditions were false

The while sentence

The while sentence has the form

If the condition is met, the sentence or instruction block is executed and the process is repeated until the condition is no longer met

The for sentence

This loop has a syntax very similar to the for-each Java

Iterates a variable var over all the properties of an object obj that is passed to it

Thus, for each value of var is is executed, the judgments of the loop

Therefore, the loop will have as many iterations as the object's properties, and in each iteration the variable will have the value of the corresponding object's property with that iteration

Its syntax is:

Iterators

In Python there are different structures that are sets of elements, they are called collections

These types of structures are iterable, that is, they can be traversed element by element

Some variable types that are iterable are:

  • Character string (str)
  • List (list)
  • Tuple (tuple)
  • Dictionary (dict)

Its use is useful for traversing collection items such as lists, tuples, or objects

In addition, many times we want to repeat a loop a certain number of times

The function of this can be useful for this range(n)

This function generates an iterable ranging from 0 to n – 1

The break sentence

The break sentence can be placed within a loop or anities loops

When you execute the break sentence exits the loop more internal

To all effects, the break sentence acts as a jump to the sentence following the loop in which it runs

The continue sentence

The continue sentence does not leave the loop but causes the next iteration to run

In the loop while the execution of the continue causes the program flow to jump to the condition

In the for loop the continue execution causes the increment expression to run, and then continue normally with the condition

That is, running the continue prevents the rest of the loop body from running

Functions in Python

Functions in Python

To define functions in Python we have the instruction def

After this reserved word goes the function name along with a list of arguments delimited by parentheses and separated by commas, ending with the symbol :

Defining a function does not execute the body of the function

It will only be executed when the function is called

Its execution binds the function name with the current local namespace to an objecto function (a wrapper around the executable code for the function)

This object function contains a reference to the global local namespace as the global namespace to be used when the function is called

Optionally you can add an expression that will be returned by the function

The expression can be a valid Python type preceded by the statement return

The DOCSTRING is a descriptive text of the function enclosed in three double quotes of opening and closing, before the first statement

It's optional, so if we don't want to add a description, we can omit it

But it is recommended to use it so that when we review after a while our code, we remember what our function did

Arguments

The argument step is optional and in some cases there may be functions that lack them

Type of arguments

Supports types str, int, float, complex,bool, list, tuple, dic and class

Arguments with value by position

When we send arguments to a function, they are received in order in the defined parameters

Value arguments by name

It is possible to evade the order of the parameters if we indicate during the call the value that each parameter has from its name

No arguments

When calling a function which has a few parameters defined, if you do not pass the arguments correctly it will cause an exception TypeError

Default value arguments

Sometimes we need an argument to have a value, to avoid an error (which would cause the exception to be thrown TypeError), even if the user of the function does not enter it

Note that default arguments become optional and that all arguments to the left of the first default value are required

The example used the null value (None) to express that no arguments had been passed

But we could use any type we need as the default

Indeterminate arguments

Sometimes we won't know in advance how many elements we need to send to a function

For these cases we can use the indeterminate parameters by position or by name

Arguments by position

As an argument we will use a tuple type preceding it with the symbol *

In this way all the arguments will be received by position, being able to navigate them comfortably with a for

Arguments by name

As an argument we will use a type to dictate it before the symbols **

In this way all the arguments will be received by the name of the dictionary key, being able to navigate them comfortably with a for

Arguments by position and name

Sometimes you may need to use both types of parameters simultaneously in a function, so you must create both dynamic collections

First the indeterminate arguments by value and then by name

All examples have used the names args and kwargs but they're not mandatory

Many frameworks and libraries use them so it's a good practice to call them that, by convention

The pass statement

It's a null operation, when we execute it, nothing seems to happen

It is useful as a container when a statement is required syntactically, but you don't need to execute code

The return statement

The statement return it is the one that allows you to return the result of a function

Supports types str, int, float, complex,bool, list, tuple, dic and class

The statement return optional, but if omitted, by default it will return the null value (None)

Multiple return

An interesting feature of the functions in Python, is the possibility to return multiple values separated by commas

Functions are predefined by the language

Language defined functions are sorted by modules

A module is a set of classes and functions stored in a file that can be used later

We can create our own modules and import them into the new programs we make

However, it is advisable to reuse existing modules

To import a module we will use the statement import

In this way we will be driving a namespace, similar to how C++ does (using namespaces) or Java

It has the following syntax

To call a specific function of the module we will use number.funcion() or alias.funcion in case we've given the module an alias

If you don't want to use all the functions of the module but if you do, we'll use the statement from

It has the following syntax

Sys module

This module is responsible for providing variables and functionalities related to the interpreter

The variables of the sys module are:

  • sys.executable

    Returns the absolute directory of the Python interpreter executable binary file

  • sys.platform

    Returns the platform on which the interpreter is running

  • sys.version

    Return the Python version number with additional information

The most prominent methods of the sys module are as follows:

  • sys.exit()

    Force the interpreter out

  • sys.getdefaultencoding()

    Returns the default character encoding

  • sys.getfilesystemencoding()

    Returns the character encoding used to convert unicode file names to system file names

  • sys.getsizeof()

    Returns the size of the passed object as a parameter

os module

This module allows us to access OS dependent features

Above all, those that give us information about the environment of the same and allow us to manipulate the directory structure

Some of the most commonly used methods are:

  • os.getcwd()

    It tells us what the current directory is

  • os.chdir(route)

    Change working directory to last path as an argument

  • os.chroot()

    Switch to the root directory

  • os.mkdir(directory)

    Creates the passed directory as an argument

  • os.rmdir(directory)

    Deletes the passed directory as an argument

  • os.remove(file)

    Delete the file passed as an argument

  • os.rename(current, new)

    Renames the current file by the name of the new file, passed as arguments

math module

This module provides us with mathematical functions and constants

We can find all the functions and attributes in your documentation

Some of the most commonly used variables are:

  • math.pi

    Represents the value of the variable \pi (Pi number)

  • math.e

    Represents the value of variable e (Euler number or Napier constant)

  • math.tau

    Represents the value of the variable \tau = 2\cdot\pi (constant Tau, proposed by Bob Palais, Peter Harremoes, Hermann Laurent, Fred Hoyle, Michael Hartl, Nicolas Atanes, among others)

Some of the most commonly used methods are:

  • math.ceil(x)

    Returns the integer less than or equal to x

  • math.floor(x)

    Returns the largest integer less than or equal to x

  • math.exp(x)

    Returns the number e raised to the power x

  • math.log2(x)

    Returns the base 2 logarithm of x

  • math.sin(x)

    Returns the sine of x radians

  • math.cos(x)

    Returns the cosine of x radians

  • math.tan(x)

    Returns the tangent of x radians

  • math.degrees(x)

    Converts the x-angle of radians to degrees

Random module

The random module is used for everything related to random numbers

Some of the most commonly used methods are:

  • shuffle(col)

    Randomly reorders items in a collection

  • sample(col, k)

    Take n items without a collection replacement

  • random()

    Generates a random number in the range [0, 1)

  • uniform(a, b)

    Generates a random number in the range [a, b]

  • randint(a, b)

    Generates an integer random number in the range [a, b]

  • gauss(mean, sigma)

    Generates a random number following a gaussian distribution with given mean and sigma

NumPy module

Numpy provides us with a lot of mathematical functions and the type of variable NumPy array

Usually the NumPy package is usually imported with the alias np

This module allows us to work with mathematical structures in a very comfortable way (product of matrices, random numbers, operations on matrices...)

It is a module that is not installed by default

To do this open a terminal and run:

We can find all the functions and attributes in your documentation

Some of the most commonly used methods are:

  • random.rand()

    It returns random values to us with the desired shape

  • empty()

    Creates an empty array with the right shape

  • zeros()

    Creates an array of zeros with the right shape

  • dot()

    Calculates the matrix product between two arrays

  • sum()

    Calculates the sum of the elements of an array

Pandas module

The package pandas is widely used in the Data Scientists

It's one of the packages we should focus on to learn about data analysis

Through pandas we can get acquainted with our data set by cleaning it, transforming and analyzing it

For example, with pandas we can read a CSV file from our computer, pass it to a DataFrame (a table, in essence) and do things like:

  • Calculate statistics and answer questions about the data such as getting the maximum, minimum and average of each column, knowing how correlated columns A and B are, or knowing the statistical distribution of column C
  • Clean up data doing things like removing missing values or filtering rows and columns according to some criteria
  • Visualize the data with the help of matplotlib
  • Save the data already cleaned back in a CSV or database
  • Pandas is based on NumPy, so we should have that module pre-installed

    Jupyter Notebooks it's an app that complements very well with Pandas, since we can run the code by cells instead of completely, which is useful if we work with large datasets

    The primary components of Pandas are series and DataFrame, the first type being a column and the second being a table

    Usually the pandas package is usually imported with the alias np

    It is a module that is not installed by default

    To do this open a terminal and run:

    We can find all the functions and attributes in your documentation

    Now let's create a simple example to show the use of pandas using a series that will contain the members of the Spanish football team that won the 2010 World Cup

    To create the series we use the method pd.Series you will receive a list of player names and a list with the number of players

    Then we'll show it by screen

    Now we're not going to explicitly point indexes on it, then it will generate the indexes automatically starting from the zero value

    Now let's introduce a dictionary into the series instead of a list and add one more player

    Now let's create a simple example to show the use of pandas using a DataFrame that will contain the members of the Spanish football team that won the 2010 World Cup

    To create the DataFrame we use the method pd.DataFrame you will receive a dictionary with player data, a list with the column name and a list with the number of players

    We'll add one more player using the method loc dataFrame object

    Then we'll show it by screen

    Display functions

    print

    The function print serves to send the output of your arguments to the browser in a format

    Operador %

    The operator % can also be used for string format

    Interprets the left argument much like a style format string of the function printf C++, which applies to the right argument

    In Python, there is no function printf but it has been endowed with its functionality print

    For this purpose, the operator % is overloaded for strings to show the format of the string

    It is often called string module operator (or, sometimes, even, module)

    It has the format:

    To format a text you have to enter the following formatting characters preceded by the symbol %:

    Format characters for text
    Character Description
    c

    The argument is treated as a string value and collects a single character

    d

    The argument is treated as a value of type int and presented as an integer

    e

    The argument is treated with scientific notation (for example 1.2e+2)

    E

    As %e but uses the uppercase letter (for example 1.2E+2)

    f

    The argument is treated as a float value and presented as a floating point number (considering the locale)

    F

    The argument is treated as a float value and presented as a floating point number (without considering the locale)

    g

    Like %e and %f

    G

    Like %E and %f

    o

    The argument is treated as an integer value and presented as an octal number

    s

    The argument is treated as a string value and collects a string

    u

    The argument is treated as a value of type int and presented as an unsigned decimal number

    x

    The argument is treated as an integer value and presented as a hexadecimal number

    X

    The argument is treated as an integer value and presented as a hexadecimal number in uppercase

    Format method

    The method format added to string types in Python 2.6

    Allows the use of characters {} to mark where a variable will be replaced and uses detailed formatting policies used to format

    This method allows us to concatenate elements within an output through the positional format

    Brackets and characters within them (called format fields) are replaced with objects passed to the format method

    A number can be used in parentheses to refer to the position of the argument passed to the format method

    Keep in mind that the first argument starts at zero

    A key identifier can also be used when the argument is passed by name

    This option will be useful when working with dictionaries or when we want to reduce the code something

    Optionally you can put the symbol : after the number or name, and specify the type of the object:

    Format characters for text
    Character Description
    s

    The argument is treated as a string value and collects a string

    d

    The argument is treated as a value of type int and presented as a decimal number (signed)

    f

    The argument is treated as a float value and presented as a floating point number (considering the locale)

    Higher order functions

    In mathematics and computer science, higher order functions are functions that meet at least one of the following conditions:

    • Take one or more functions as input
    • Return a function as output

    Lambda functions

    Lambda functions are anonymous functions, that is, unnamed functions

    They are only used where they were created (although they can be named and used later)

    They are usually used in conjunction with map, filter and reduced functions

    They are usually used to create functions that are passed as arguments to other functions

    In Python the syntax of a lamdba function is:

    Where the expression can be any operation we would do when making an assignment

    map function

    It allows us to apply a function on each of the elements of an iterable

    It returns a map object, which we can easily transform into a list

    filter function

    Allows us to filter items in a collection (for example, a list)

    It is a widely used function in the treatment of collections

    reduce function

    Take a function and a collection by reducing it to a single value

    It would be equivalent to an aggregation function

    It belongs to the module functools, so it must be imported before it can use reduce

    The function we pass must take at least two parameters and an optional one

    The first is the aggregate parameter that accumulates in each iteration over the items in the collection

    The second element corresponds to the step of the iteration

    The third is in case an initial value is needed

Python exceptions

Python exceptions

If an operation cannot be completed due to an error, the program must:

  • return to a stable state and allow other operations

  • try to save your work and finish

This task is difficult because usually the code that detects the error is not the one that can perform such tasks this is why you should inform that you can handle it

The most common solution are the error codes

Python offers another way to deal with errors under exceptional conditions: the exceptions

An exceptional condition is one that prevents the continuation of an operation

It's not known how to handle it, but you can't continue

In Python, an exception is thrown for someone who knows how to handle it to handle it in a higher context

In the example the open function checks whether the file readme.txt existed in the system and if it doesn't exist the exception is thrown FileNotFoundError who is untreated at the moment

When the interpreter detects the throw of the exception, the current method ends and launches an object that provides information about the error that occurred

Normally each type of error will be handled individually

Handling of the exceptions

Once detected the error makes it necessary to indicate who is in charge of treating it

An exception handler has the following format:

try block

The block try delimits the group of operations that may produce exceptions

except block

The block except it is the place to which control is passed if any of the operations throws an exception

The example used a block try with his block except to handle the errors in the previous example

If any of the operations in the block throw an exception, the block is interrupted try and runs the except

At the end of this, it is normally continued

If no exception is thrown, the block except is ignored

Multiple exceptions

There is also the possibility to use an exception name to handle specific errors

Some of which are already recognized by the system, such as the exception FileNotFoundError, which is launched when a file does not exist in the system

The example used a block try with several blocks except to handle several exceptions

A block try can have several except associated

We've added the sentence as to the block except after naming it to make it the object of exception we've called and

As you can see, after turning it into an object, we can use it as if it were a class

When an exception does not correspond to any catch spreads backwards in the sequence of invocations until a catch adequate

In the example is managed the exceptions of the methods f1 and f2 and if you don't find any, it continues the execution of the code in the normal way outside of the block try

The sentence has been used raise followed by the name of the type of exception to propagate it and treat it elsewhere

In this case we have treated it within the f1 function itself in its exception blocks

Although spreading it could have been treated outside of it elsewhere in the script

A block has also been used else that allows the code to continue execution in case there are no exceptions before continuing outside the block try

else block

Allows code execution to continue in case there are no exceptions before continuing outside the block try

The block else can be used to exit a loop if the statement is added at the end of a loop break

It is optional and if it is not used, it will continue the execution of the code in the block finally and finally outside the block try

Finally block

There may be occasions in which you want to perform some operation if exceptions occur as if not

Such operations can be placed within a block finally

If there is no exception, the block will be executed try, then the block else and finally the finally

Like the block else, is optional and if it is not used, it will continue to execute the code outside the block try

Rules of use

  1. Standard

    • If an exception can be handle should not be spread

    • It is more convenient to use methods that do not produce errors

  2. Standard

    • Do not use exceptions to avoid a query

      • Not to abuse them

  3. Standard

    • To separate the error handling logic

      All together

      Separate

  4. Standard

    • Do not ignore an exception already that we leave the code with errors without control

Web Page

Web page

A website, or electronic page, digital page, or cyberpage is a document or electronic information capable of containing text, sound, video, digital images, programs, access to other websites through hypertext links, images, etc

Broadcast in HTML or XHTML formats, it also includes other resources called cascading style sheets (CSS) and scripts

A web page can be stored on a local computer or on a remote web server

A web server can restrict access only to private networks, such as a corporate intranet, or it can publish pages to the World Wide Web (WWW)

Access to web pages is done by a transfer from servers, using hypertext transfer protocol (HTTP) or Secure Hypertext Transfer Protocol (HTTPS), being accessible from a browser

Html

Html

The HTML (hypertext markup language) describes the content of a web page, including text, images, videos, etc

The specifications on the functioning of the web makes the W3C (World Wide Web Consortium). On its website we can find all the documentation about HTML and other network standards

For the editing of web pages we can use any text editor

In windows Notepad or Wordpad, in linux vi, joe, mcedit, etc

In them we will take advantage of the capabilities of each one to copy and paste. There are many web editors that make it easy for us to insert HTML code

Some freeware, other shareware and other commercial ones like Adobe Dreamweaver with which you get more professional pages because they have predefined templates that speed up our work

For advanced developers we have Sublime Text, although it may be unfriendly to other developers

Among the friendliest we have Brackets, which allows you to view the HTML code automatically

And Atom, as another alternative, which is now becoming fashionable

We have to take into account the type of encoding that we use with this language, because if when writing our website we use an encoding other than that of the web browser, we can get a page with strange or illegible characters

The most commonly used option is UTF-8 encoding, which is the standard for the Internet and Linux systems

Windows and MacOS systems use other types of encoding so we will have to match the encoding in the document header, with the one that will be displayed in the web browser

Standards

HTML 1.0

Tim Berners-Lee in 1991 describes 18 elements that include the initial and relatively simple HTML design. Thirteen of these elements still exist in HTML 4.4

Berners-Lee considered HTML an extension of SGML, but was not formally recognized as such until publication in mid-1993, by the IETF (Internet Engineering Task Force), of a first proposition for an HTML specification: Berners-Lee and Dan Connolly's Hypertext Markup Language draft, which included an SGML Document Type Definition to define grammar

The draft expired at six months, but was notable for its recognition of the Mosaic browser's own label used to insert images without line change, reflecting IETF's philosophy of successfully basing standards on prototypes. Similarly, Dave Raggett HTML+'s (Hypertext Markup Format) competing draft from late 1993 suggested standardizing features already implemented, such as tables

HTML 2.0

The official version for HTML was until a few years ago HTML 2.0, called HTML+, began to develop in late 1993. It was originally designed to be a superset of HTML that would gradually evolve from the previous HTML format

The first formal HTML+ specification was therefore given the version number 2.0 to distinguish it from previous unofficial proposals. HTML+ work continued, but never became a standard, despite being the basis formally more similar to the common aspect of current specifications

It fulfilled its function perfectly, but many HTML users wanted to have greater control over their documents, both in the formatting of the text and in the appearance of the page

HTML 3.0

Meanwhile, Netscape, which was at the time the undisputed leader of browsers, introduced with each new version tags and attributes not covered by the official standard, creating discontent among some developers

Due to their wide dissemination and acceptance, other browsers tried to copy these innovations, but because Netscape did not fully specify its new tags, the results were no different from those desired. Which caused a lot of confusion and numerous problems, when developers used these elements and found that it didn't work the way they wanted in other browsers

The draft HTML 3.0 standard was proposed by the newly formed W3C in March 1995. Many new capabilities were introduced with him; for example, facilities for creating tables, making text flow around figures, and displaying complex mathematical elements. Although it was designed to support HTML 2.0, it was too complex to be implemented with the technology of the time, and when the draft standard expired in September 1995, it was abandoned due to the lack of support from web browser manufacturers

HTML 3.2

HTML 3.1 never officially came to be proposed, and the next standard was HTML 3.2, which abandoned most of the new features of HTML 3.0 and, in return, adopted many elements initially developed by Netscape and Mosaic web browsers. The possibility of working with mathematical formulas that had been proposed in HTML 3.0 became integrated into a different standard called MathML

The need for a new standard that was accepted by all was becoming increasingly evident. Another committee was formed, the W3C, supported by major software vendors (including IBM, Microsoft, Novell, Netscape, Sun, etc.). The new standard was developed throughout 1996 by the W3C under the nickname Wilbur, and finally, in January 1997 it was approved as HTML 3.2

HTML 3.2 was fully compatible with the above standard, but incorporates many of the innovations of commercial browsers (Netscape and Internet Explorer mainly), such as tables, applets, text bordering images, among others, but not all, such as frames

HTML 4.0

In July 1997, the first public draft of the official HTML 4.0 standard (which has the code name Cougar) was introduced. It incorporates specifications on tables, frames, scripts, style sheets, accessibility by different means, and internationalization (including the use of Unicode, instead of Latin-1)

In 1997, HTML 4.0 (which has the key name Cougar) was published as a W3C recommendation. HTML 4.0 adopted many specific elements initially developed for a particular web browser, but at the same time began cleaning up the HTML by pointing out some of them as deprecated (disapproved)

HTML 4.0 implements features such as XForms 1.0 that do not need to implement navigation engines that were incompatible with some HTML web pages. It incorporates specifications on tables, frames, scripts, style sheets, accessibility by different means, and internationalization (including the use of Unicode, instead of Latin-1)

HTML 5.0

HTML5 specifies two syntax variants for HTML: a "classic", HTML (text/html), known as HTML5, and an XHTML variant known as XHTML5 syntax that should be served with XML syntax (application/xhtml+xml). This is the first time HTML and XHTML have been developed in parallel

In 2004 the W3C reopened the debate on the evolution of HTML, and the basis for the HTML5 version was released. However, this work was rejected by W3C members and preference would be given to the development of XML

Apple, Mozilla and Opera announced their interest in continuing to work on the project under the name WHATWG, which is based on compatibility with previous technologies

In 2006, W3C became interested in html5 development, and in 2007 it joined the WHATWG working group to unify the project

The final version of the fifth revision of the standard was published in October 2014

Because it is not recognized in old browser versions for its new tags, it is recommended that the common user update their browser to the newer version, in order to enjoy the full potential provided by HTML5

Format of an Html page

Format of an HTML page

Its essential principle is the use of labels. The labels are reserved words of the language, enclosed in < >

Types of labels

There are three types of tags:

  • Paired
    when a start and end tag appears, the blocks contained between start and close are elements, which in turn may contain other elements

  • No match
    when the label represents the insertion of an element, these tags end with a forward slash

  • With final optional
    are the labels paired in the which it is not mandatory to use the end tag

The labels can include attributes modifying its behavior, specifying some unique characteristic of the element or including additional information

The attributes are specified as pairs attribute='value', separated by spaces within the home label

Values are not required to be enclosed in quotation marks, but it is recommended

Single or double quotation marks can be used interchangeably, as long as they match their opening and closing

The example has used the id attribute, which is generic for any tag and which will be useful to us when using CSS and Javascript, because it allows us to uniquely distinguish the element to which that particular tag belongs

There are more generic attributes, such as class, title or style, all very useful when we want to format or improve accessibility to our elements

When we introduce a label that does not exist, it will be ignored, we must pay attention to this issue because it makes it difficult to detect errors

The labels can be typed in uppercase or in lowercase

Line breaks and extra white space are not considered. The latter allows you to indent your code to make it more readable

Comments within the HTML

Sometimes it is very useful to write comments in the HTML document about the code we write, they can serve to remind us later what was done, and that we do not want it to be seen on screen

This is achieved by enclosing such comments between these two symbols:

Comments can be used anywhere in the HTML document, but will not be displayed

Special characters

There are certain characters that a browser is not able to recognize in spite of using the correct encoding

Let's imagine that we want to write the text x > y within our HTML, however, the character > will not be recognized by the browser or will show us one wrong

This happens with a set of characters that are special and that the browser will recognize by using the particular label of this character with the following format &label;

In the example above, the label for the > is gt, starting with & and ending with ;

On the following website we can consult the complete list of the 256 universal characters HTML