Python and R Programming Basics Guide
Python and R Programming Basics Guide
Syllabus
INDEX
Concepts of Python Programming
Python
R Language and R Studio
API – Application Programming Interface
Metadata
Copyright Law No. 9,610, of February 19, 1998: It prohibits the total or partial reproduction of this material or its dissemination with
commercial purposes or not, in any means of communication, including the Internet, without the authorization of AlfaCon Public Competitions.
1
AlfaCon Public Competitions
makes an assignment of value, automatically the variable becomes of the type of the stored value, as
presented in the following examples:
Examples:
>>>a=10
>>>a
10
The variable becomes an integer type variable.
Strings can be concatenated (combined) with the + operator, and repeated with *.
2
AlfaCon Public Competitions
Examples:
>>> L = [3 , “abacate” , 9.7 , [5 , 6 , 3] , “Python” , (3 , ‘j’)]
>>> print(L[2])
9.7
>>> print(L[3])
[5,6,3]
>>>print(L[3][1])
6
FUNCTIONS FOR LIST MANIPULATION
The list is a mutable structure, meaning it can be modified. In the table below, there are
some functions used to manipulate lists
Function Description Example
L = [1, 2, 3, 4]
len Returns the size of the list.
len(L) 4
[10, 40, 30, 20]
min Return the smallest value from the list.
min(L) 10
L = [10, 40, 30, 20]
max Returns the highest value in the list.
max(L) 40
L = [10, 20, 30]
sum Return the sum of the elements in the list.
sum(L) 60
L = [1, 2, 3]
append Add a new value at the end of the list. [Link](100)
L [1, 2, 3, 100]
L = [1, 2, 3, 4]
remove Remove an element from the list, given its index. remove L[1]
L [1, 3, 4]
Tuple, just like List, is a sequential set of values, where each value is identified.
through an index The main difference between them is that tuples are immutable, or
be, your elements cannot be changed
Among the utilities of tuples, packing and unpacking operations stand out.
value mentoring
A tuple in Python is declared as follows:
Name_tuple = (value1, value2, ..., valueN)
Examples:
(1, 2, 3, 4, 5)
>>> print(T)
(1, 2, 3, 4, 5)
>>> print(T[3])
4
A dictionary is a set of values, in which each value is associated with an access key.
A dictionary in Python is declared as follows:
Nome_dicionario = { chave1 : valor1, chave2 : valor2, chave3 : valor3, chaveN : valorN}
Example:
>>>D={“arroz”: 17.30, “feijão”:12.50,”carne”:23.90,”alface”:3.40}
>>> print(D)
{“arroz”: 17.3, “carne”: 23.9, “alface”: 3.4, “feijão”: 12.5}
>>> print(D[“carne”])
Copyright Law No. 9.610, of February 19, 1998: Prohibits the total or partial reproduction of this material or disclosure with
commercial or non-commercial purposes, in any form of communication, including the Internet, without the authorization of AlfaCon Public Competitions.
3
AlfaCon Public Competitions
23.9
SOME OPERATORS
The Python language may present some operators different from the usual ones in other languages:
Mathematical Operators:
Addition + age + 1 = adding 1 to the age
Subtraction - age - 1 = subtracting 1 from the age
Multiplication * value * 10 = multiplying by 10
Division / amount / 10 = dividing the amount by 10
Remaining % number % 3 = giving the remainder of a number divided by 3
Power ** number ** 2 = raising the number to the second power
Relational Operators:
Greater >
Minor
<
Greater than or equal to >=
Less than or equal to <=
Same ==
Different !=
Logical Operators:
OU Or
E
And
Negation
Not
The use of # (hashtag) or ''' (triple quotes) indicates a comment.
CONDITIONAL AND REPETITION STRUCTURES IN PYTHON
In Python, just like in most programming languages, the program must be able to
make decisions based on values and results generated during its execution, that is, it must be capable of
decide whether a certain instruction should or should not be executed according to a conditionTo meet
in this type of situation, we can use special instructions called conditional structures
In addition to these, programming languages typically also support the so-called
loops, structures that allow instructions to be executed repeatedly until a
condition being met
Conditional structures with IF
It establishes a condition structure that allows evaluating an expression and, according to its result...
keep, perform a certain action
In the following code, we have an example of using the if statement, where we check if the variable age is
less than 20. If yes, we display a message on the screen, and if no, the code
will continue normally, disregarding line 3
As we can see, this structure is formed by the reserved word if, followed by a
condition and by two points (:) The lines below it form the block of instructions that will be executed
that condition is met. For this, they must be correctly indented, respecting the
Python specification: In this code, only the instruction in line 3 is executed, and that is why it is
more advanced If other lines needed to be executed in case the age is less than 20,
they should also be at the same indentation level as line 3
We saw earlier how to use 'if' to execute an action if a condition is met.
However, no specific behavior has been defined for the case where the condition does not apply.
be satisfied When this is necessary, we need to use the reserved word else Additionally-
If there is more than one alternative condition that needs to be checked, we should use elif.
Copyright Law No. 9,610, of February 19, 1998: Prohibits total or partial reproduction of this material or its disclosure
commercial or not, in any means of communication, including the Internet, without the authorization of AlfaCon Public Competitions.
4
AlfaCon Public Competitions
In some situations, it is common for the same instruction (or set of them) to need to be executed.
cut several times in a row. In these cases, we normally use a loop (or repetition loop),
which allows executing a block of code repeatedly, while a given condition is met
In Python, loops are coded using the for and while commands. The first allows us to
to iterate over the items in a collection and, for each of them, execute a block of code. As for while,
executes a set of instructions multiple times while a condition is met
In Listing 2, we have an example of using the for command.
The variable defined on line 1 is a list initialized with a sequence of values of the type
The for loop goes through all these elements, one by one, and in each case, assigns the value
from the item to the variable n, which is printed next. The result is then the printing of all names.
contained in the list, as we see in lines 5 to 7.
The while command, in turn, makes a set of instructions execute while
a condition is met When the result becomes false, the execution is interrupted, exiting
loop, and move to the next block
In the following code, we see an example of using the while loop, where we define the counter variable,
starting with 0, and while its value is less than 5, we execute the instructions in lines 3 and 4
Note that in line 4 we increment the counter variable, so that at some point
its value exceeds 5. When this is confirmed in line 2, the loop will be interrupted. If the
the stopping condition is never reached, the loop will run infinitely, causing problems in
program
Copyright Law No. 9.610, of February 19, 1998: Prohibits the total or partial reproduction of this material or its disclosure with
commercial or not, in any means of communication, including the Internet, without the authorization of AlfaCon Public Competitions.
5
AlfaCon Public Competitions
Control structures, conditionals, and loops are present in most programming languages.
programming represents a fundamental part of each of them. Therefore, it is very im-
It is important to understand the syntax and functioning of these structures.
Copyright Law No. 9,610, of February 19, 1998: Prohibits the total or partial reproduction of this material or disclosure without
for commercial purposes or not, in any means of communication, including the Internet, without authorization from AlfaCon Public Competitions.
6
AlfaCon Public Competitions
7
AlfaCon Public Competitions
→Plugins: currently it is one of the most used modes. When we talk about a WordPress blog.
example most of the resources are a result of plugin consumption (examples are the use of decaptcha,
email sending, efeed, antispam, SEO optimizer, statistics analyzer etc.)
WebAPI: basically, they are APIs used in web solutions. WebAPIs can be client-side or
server-side (executed on the front or in the back-end of a web application)
Metadata
The metadata technology emerged due to organizations needing to better understand the
data that they maintain. The metadata provides a concise description of the data.
data can be documents, collection of documents, graphs, tables, images, videos, among others
so many others
In databases, information about the data is just as important as the data.
Relational Database Management Systems - RDBMS also use metadata.
The tables in the database are used to store information. Similarly, a
RDBMS has several metatables that store descriptions of the tables.
In the Oracle DBMS, for example, the USER_TABLES table is a meta table that contains information
actions regarding tables created by users. Among this information, the following can be found:
table owner, table name, tablespace name (logical storage unit) for
which was defined, among others
Metadata play an important role in data management, as they provide information about the data.
are processed, updated and consulted. The information on how the data was created/derived
two, environment in which it lives and/or lived, changes made, among others, are obtained from metadata
Metadata provides the necessary features to understand data over time.
The date and time stamped on the photos also constitute the use of metadata, in addition to the location.
geographic action that modern cameras are capable of performing for each image
Exercises
Regarding languages, judge the following item.
Python is a high-level, object-oriented programming language that is difficult to read, as it does not
allows indentation of code lines
Sure Wrong ( )
Judge the following item regarding the Python language, version 3.1.
02. If, in any line of the Python script, the regular expression coding[=:] \s*([~\w ]+) matches
by a comment, this will be processed as a coding statement.
Sure ( ) Wrong
Copyright Law No. 9,610, of February 19, 1998: Prohibits total or partial reproduction of this material or disclosure of
commercial or not, in any means of communication, including the Internet, without the authorization of AlfaCon Public Contests.
8
AlfaCon Public Competitions
Sure ( ) Wrong ( )
With the introduction of HTML5, several new Javascript APIs (Application Programming ...
interfaces) were made available, considerably increasing the amount of resources
available for web page production
Sure Wrong ( )
[Link] a DBMS, the data dictionary manager is responsible for storing the metadata.
about the database structure
Sure ( ) Wrong ( )
Template
01 - Wrong
02 - Wrong
03 - Wrong
04 - Right
05 - Right
Copyright Law No. 9,610, of February 19, 1998: Prohibits the total or partial reproduction of this material or dissemination with
commercial or not, in any means of communication, including on the Internet, without the authorization of AlfaCon Public Competitions.
9