Programação RPi Python 07: Noções básicas de Python (parte 2)

RPi Python Programming 07: Python Basics (Part 2)

In the previous tutorial , we learned about Python's syntax — that is, its identifiers, keywords, comments, statements, literals, and data types. We also learned that Python is object-oriented and that these objects can be mutable or immutable . Python objects that point to a group of values ​​are iterable, which in this case is called an iterator .

All of the data types covered in the previous tutorial (integers, floats, complex numbers, strings, raw strings) are data types for literals (real values ​​assigned to variables or attributes). Python also has data types for references that point to a group of ordered or unordered values ​​(items).

Furthermore, Python has the following data types:

Tuples – a sequence of objects. A sequence refers to an ordered group of items (objects), where indexes can access the items. The items in a tuple can be an arbitrary mutable or immutable object.

However, the tuple is immutable, which means that items in a tuple cannot be updated or changed once they are defined. This is why it is rarely recommended to use mutable items in a tuple unless they will never change or require updating. A tuple can be viewed as a group of constant values ​​or objects and can be used to define one or more constants.

A tuple is defined as a group of values ​​or objects separated by spaces or commas. The group can optionally be enclosed in parentheses (“(“). Commas are also optional, but it is recommended to use commas to separate items in the tuple, especially if the tuple is needed for a function.

Optionally, the last item in the tuple can also have a comma at the end. An empty tuple is defined by an empty pair of parentheses. The built-in function, tuple , can also be used to define a tuple. Any group of items separated by spaces or commas will default to a tuple. If a single string (or any iterable) is defined as a tuple, its characters (or items) can be used as tuple items.

Here are some valid examples of tuples:

tup1 = 'Id' 'Name' 'Address'
tup1 = 'Id', 'Name', 'Address'
tup1 = ('Id', 'Name', 'Address')
tup1 = ('Id', 'Name', 'Address',)
tup1 = #Empty Tuple
tup1 = ('Id', 123, 0b1111_1110, 2.5j5.4, True)
tup1 = ('Id', 123, 0b1111_1110, 2.5j5.4, True,)
tup1 = tuple('Id', 'Name', 'Address')
tup1 = tuple('Id' 'Name' 'Address')
tup1 = tuple('ABC') # is equivalent to tup = ('A', 'B', 'C')

Lists – sequences ordered as a group of mutable items. This means that items in a list can be changed or modified. Essentially, lists are a modifiable group of values ​​or objects where items in the group can be replaced, removed, or added. Items in a list can include any arbitrary mutable or immutable object.

A list is defined as a comma-separated group of values ​​and/or objects enclosed in square brackets (“(“). Optionally, the last item in the list can have a trailing comma. The built-in function, list can also be used to define a list .

An empty list is defined by a pair of empty brackets or by the list function with no arguments. If an iterable is used as the only item in a list, its items can be used as list items.

Here are some valid list examples:

list1 = ('Id', 'Name', 'Address')
list1 = ('Id', 'Name', 'Address',)
list1 = #EmptyList
list1 = list #EmptyList
list1 = ('Id', 123, 0b1111_1110, 2.5j5.4, True)
list1 = ('Id', 123, 0b1111_1110, 2.5j5.4, True,)
list1 = list('Id', 'Name', 'Address')
list1 = list('Id' 'Name' 'Address')
list1 = list('ABC') # is equivalent to list1 = ('A', 'B', 'C')

Sets – a changeable, unordered collection of unique items. Items in a set must be immutable and “hashable,” meaning that their value (the hash value) must never change. Immutable means that its data type should never change either. This is because a set can only contain immutable and hashable items, without any other set within it.

A set is defined by a group of values ​​and/or objects (immutable and hashable) separated by commas in square brackets (“{“). Optionally, the last item in the set can have a trailing comma. The built-in function, define , can also be used to define the set. An empty set is defined by an empty pair of square brackets or define function with no arguments.

Here are valid examples of sets:

set1 = {'Id', 'Name', 'Address'}
set1 = {'Id', 'Name', 'Address',}
set1 = {} #Empty set
set1 = set #Empty Set
set1 = {'Id', 123, 0b1111_1110, 2.5j5.4, True}
set1 = {'Id', 123, 0b1111_1110, 2.5j5.4, True,}
set1 = set('Id', 'Name', 'Address')
set1 = set('Id' 'Name' 'Address')

Frozen sets – immutable version of sets. They are defined by the frozen set function. An empty frozenset is defined by the frozenset function with no arguments.

Here are valid examples:

frset1 = frozenset('Id', 'Name', 'Address')
frset1 = frozenset('Id' 'Name' 'Address')
frset1 = frozenset #Empty Frozen Set

Dictionary – a mutable, unordered collection of values ​​paired with arbitrary keys. These keys are arbitrary, but must be hashable. Values ​​can include any arbitrary mutable or immutable objects.

The dictionary is similar to an unordered map or a hash table in other programming languages. It is defined as a group of commas, separated pairs of keys and values ​​enclosed in square brackets or used as arguments in the dict function.

The key-value pair, key and value are separated by a colon (“:”). In the saying function, key and value in a pair can be separated by an equal sign (“=”) or can be placed in a tuple. Optionally, the last item (key-value pair) of a dictionary can have a trailing comma.

An empty dictionary is defined by an empty pair of square brackets or by the dictated function with no arguments. Any item in a dictionary must be a key-value pair, even if the value is iterable.

Here are valid examples of dictionaries:

dict1 = {x:25, y:57, z:64}
dict1 = {x:25, y:57, z:64,}
dict1 = {'x':25, 'y':57, 'z':64,}
dict1 = {'x':25, 57:'y', z:8.9j6.5}
dictation1 = dictation (x = 25, y = 57, z = 64)
dict1 = dict((x, 25), (y, 57), (z, 64))
dict1 = dict # Empty dictionary
dict1 = {} # Empty dictionary

None – a null object in Python, which is equivalent to “no object” and has zero attributes or methods. A function returns None if it does not have a return statement.

Byte – immutable sequences (ordered collection), where only items can be bytes (integers in the range 0 to 255). Items in a Byte can be characters in a string, integers, None , or iterables.

Bytes can be defined by prefixing “b” before a string or integer or using a byte function. The Byte function also allows you to specify an encoding.

Here are some valid examples:

Val = b'Python'
Val = b'123'
Val = bytes('Python')
Val = bytes('Python', utd-8)
Val = bytes ((1, 2, 3, 4, 5))

Byte arrays – mutable sequences (ordered collection), containing only items (integers in the range 0 to 255). Items in a byte array can be characters from a string, integers, None , or iterables.

The only difference between Byte and Byte array types is that a byte is immutable whereas byte array is mutable. A byte array can be defined using the built-in bytearray function.

Here are some valid examples:

Val = bytearray(“Python”, 'utf-8')
Val = bytearray((1, 2, 3, 4, 5,))
Val = bytearray # empty bytearray

Range – a sequence (ordered collection) of integers between two integer values, such that each integer value (item) differs by an adjacent item through a step value. By default, adjacent values ​​are separated by a difference of “1”.

A range is refined using the integrated range function. The range function has the following syntax:

interval(start, stop, step)

  • If only one value is given as an argument in range , it will give a sequence of integers from 0 to the given argument.
  • If two values ​​are given as arguments, a sequence of integers between the two values ​​is given, which includes them.
  • If three values ​​are provided as arguments, the third value serves as the difference between each adjacent item.

Here are valid examples of a range:

range(9) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
range(5, 10) # 5, 6, 7, 8, 9, 10
range(2, 10, 2) #2, 4, 6, 8, 10

Boolean – this type can only have two values: True or False. Any non-zero value or non-empty container is considered True. Any value of zero, None , or Empty container, is considered false. True and False values ​​can also be denoted by “1” and “0”, respectively.

There are several other data types in Python that can have built-in classes (such as date and time) or user-defined classes. For example, any class that defines a data type by purpose or denotes a data structure can be considered a data type class.

There are six types of sequences (ordered collections) in Python:

  1. Strings
  2. Bytes
  3. Byte arrays
  4. Range
  5. Tuples
  6. Lists

Sequence indices must start at “0” (i.e., the first item in any sequence has an index/key of “0”). sets, frozen sets, and dictionaries are unordered collections in Python.

For clarity, see this table:

Collection name Nature of the Collection Nature of Items
String Immutable sequence (ordered collection) of ASCII or UTF (or other encoding) characters ASCII or UTF characters or characters corresponding to the applied encoding scheme
Byte Immutable sequence of bytes (0 to 255) Byte values ​​in the range 0 to 255 that can be integers, characters, or iterables
Byte Array Mutable sequence of bytes Byte values ​​in the range 0 to 255 that can be integers, characters, or iterables
Range Immutable sequence of integers Integers between the defined range
tuple Immutable sequence of objects Mutable or immutable arbitrary objects
List Mutable sequence of objects Mutable or immutable arbitrary objects
To define Mutable, unordered collection of objects Arbitrary immutable objects, duplicate objects not allowed
Frozen Set Immutable, unordered collection of objects Arbitrary immutable objects, duplicate objects not allowed
Dictionary Mutable, unordered collection of key-value pairs Arbitrary immutable keys and arbitrary mutable or immutable values

Variables, attributes and items
In Python – where everything is an object – data values ​​are also objects. Data values ​​are stored in memory. This means that whenever a new data value appears in the Python code (whether defined in code or at runtime), it is also stored somewhere in memory.

The code accesses these data values ​​through references. These references can be variables, attributes, methods or items of an object. Variables are references to data values ​​that can be used anywhere in the code.

Attributes are references to non-callable data values ​​associated with an object. Methods are references to “callable” data values ​​associated with an object. Being callable means that these data values ​​execute a group of instructions on their occurrence. In Python, instances of a class are called items. Therefore, items are references to instances of a class or objects.

Whenever a data value is assigned to a variable, attribute, method, or object, the process is called binding. The value of data remains independent of its connection. If a different data value is assigned to a reference, that data value is stored in a different memory location. As a result, the value of the previous data (if not linked to any other reference) becomes unlinked. Cleaning up unbound values ​​or data objects is called garbage collection.

There are no declarations in Python. References appear instantly and have no declared data type. How a reference (to a value/data object) is handled depends on the expression it appears in and how/if it is used to rebind another reference.

Data values ​​can be linked to a reference through an assignment or augmented assignment statements. In an assignment statement, an RHS expression is linked to an LHS target and they are separated by the assignment sign (“=”). The target can be any reference, such as a variable, an attribute, or an item.

A simple assignment statement looks like this:

var = Expression

This is an example of a simple task:

one = 1

You can assign a data value (or the result of an expression) to multiple targets at the same time:

var1 = var2 = var3 = Expression

This is an example of such a task:

a = b = c = 1

If the RHS expression is iterable or results in an iterable, it must be assigned to another iterable (of the same size) or to multiple variables (equal in number to the size of the iterable) as follows:

var1, var2, var3 = Iterable/Expression resulting in Iterable

Here is an example of such a statement:

a, b, c, = (1.1, 1.9, abc)

If a target reference has an asterisk (“*”) before it, it accepts a list of all values/data objects that have not been assigned, as follows:

var1, *var2, var3 = Iterable/Expression resulting in Iterable

Here is an example of such a task:

a, *b, c, = 1.1, 1.9, abc, 43, 2.5j4 # b = 1.9, abc, 43

In an augmented assignment, an augmented operator such as +=, -=, *=, **= is used for an assignment. The target reference must be created before use in such a statement. There can only be one target reference in an augmented assignment.

In augmented assignment, the data value previously assigned to a reference is modified and rebound to it. The previously assigned data value then becomes decoupled (from that specific reference).

Here is an example of an augmented task:

a += b # a = a + b

Data values ​​can be decoupled from references using a del statement. This is called decoupling. You can unlink multiple references in a single del statement by separating the target references with a comma. However, a del statement only detaches a value/data object from a reference and does not delete the value or data object. If the data value is not linked to any other reference, it is left for garbage collection.

Here are valid examples of del statements:

from it to
from a, b, c

Python Operators
Operators are used to manipulate data values ​​and references forming logical expressions. There can be one or more expressions within a logical statement. Python supports many operators.

These are the most common:

Arithmetic operators – include addition (+), subtraction (-), multiplication

, division (/), modulus (%), exponentiation (**) and floor division (//). The addition operator also serves as a concatenation operator with strings and other iterables. Similarly, the multiplication operator

can be used to copy a string or iterable for specified times using a multiplier. Assignment Operators –

include assign (=), add and assign (“+=”), subtract and assign (-=), multiply and assign (*=), divide and assign (/=), modulo and assign (%=), exponentiation and assign (**=) and floor division and assign (//==).

Comparison operators –

include equal (==), not equal to (!= or ), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). You can also chain comparisons as follows:

a < b >= c # a < beb >=c Logical operators –

include logical AND (and), logical OR (or), and logical NOT (not). Bitwise operators –

include binary AND (&), binary OR ( ), binary XOR (^), binary complement (~), binary left shift (<<), and binary right shift (>>). Membership operators – include in (in) and not in (not in). These operators are used to evaluate whether an item belongs to a sequence or collection. The in operator returns true if the LHS item is a member of the RHS sequence/collection. The not in operator returns true if the LHS item is not a member of the RHS sequence/collection. Identity operators – include is (is) and is not (is not). These operators are used to evaluate whether two references are identical. The two references can have the same identity if they point to the same data value, which can be stored in the same memory location (depending on the operating system and platform). What is

The (is) operator returns true if both references have the same identity. What is not

The (is not) operator returns true if both references do not have the same identity. Indexing operator –

The indexing operator ( ) is used with sequences to access a specific item from them. The indices in all sequences start with “0”, so the first item in any sequence has an index (or key) of 0. The indices of subsequent items increase by “1”. Therefore, x(2) allows you to access the third item of a sequence “x”. If a negative number is used as an index, this item will be accessed counting from the last item in the sequence and not the first.
Slicing operator –

The slicing operator ((:)) is used to access multiple items from a sequence like a list.  For example, x(1:3) allows you to access the second and third items of a sequence “x” as a list.  Remember that Python has six types of sequences: strings, bytes, byte arrays, range, tuple, and list.

Operator Precedence in Python

The following table summarizes operator precedence in Python, from lowest precedence to highest precedence:
Operator precedence in Python

Table showing precedence of operators in Python in ascending order Control Flow Statements

Python supports the following control flow statements:
If-elif-else –
a conditional statement that allows you to execute statements if the conditional expressions return true. It has this syntax:
if conditional_expression1:
statements)
elif conditional_expression2:
statements)
elif conditional_expression3:

statements) else conditional_expression: statements) In the if statement, the Elif and other

are optional. Because a conditional expression evaluates to True, the statements that precede it are executed and execution of the

if
statement ends there. However, a conditional expression does not need to explicitly return True or False. In Python, any expression or object implicitly evaluates to True or False as a conditional expression.
Therefore, it is recommended to use the following expression:
Fri:
If not x:
Instead of the following expressions:
If x == True:
If x == 1:
If x is true:
If bool(x):

If x == False: If x == 0: If x is false:

It is important to note that Python does not

support a switch statement.

As a statement –
repeats a statement or block of statements if a conditional expression evaluates to True. It keeps repeating until the conditional expression evaluates to False. This means that a statement must contain code that eventually makes the conditional expression False.

The while statement has this syntax: While conditional_expression: Statements) Statements that are executed repeatedly in a while statement are called the loop body. If while is used inside a function, and the body of the loop contains a return statement, the loop ends there – as the return statement ends execution of the function. Likewise, the break and carry on

statements can also affect the while statement. Declaration for input – allows you to execute a statement or block of statements while iterating through items in an iterable. A target reference accesses the items in the iterable. Iterable items can be used by the statement(s) or not. But the iterable should never be modified into an in

statement like the

while
When iterating through a list, set, or dictionary, items should not be added to or removed from them.

The for-in statement has this syntax: for target_reference in iterable: Declarations)

There can be more than one comma-separated target reference if the iterable is a dictionary or other data structure in which each item contains more than one data value. For example, the following
for

statement iterates through a dictionary: for key, value in d.items : print(key + ' : ' + value + '/n')

If a single statement has to be executed on each item of an iterable to obtain a new iterable, the

for

statement can be combined with an assignment statement as follows:

y = x+3 for x in interval(5)

In this statement, the execution of the expression can also be conditional, like this: y = x+3 for x in range(5) if x%2==0 Similar statements are possible with lists, sets and dictionaries. Break declaration –

used in a loop body to terminate the loop. It is often used with a

if
declaration. As the break statement is encountered, the loop ends.
Here are some valid examples of a break statement:
While x: #run loop if x is not zero or none
If x<10:
print (x)
x + = 1
other:
to break
While x: #run loop if x is not zero or none
If x>10:
to break

other: print (x) x + = 1 Continue statement –

used in a loop body to leave execution of statements after it only to the current iteration of the loop. It is often used with the

if
declaration.
Here is a valid example:

while x<=10: if x%2 == 0: continue print (x) Another statement – ​​used with an if , for and while statements. When used with the for or while it executes a statement or statement block after the natural termination of the loop (i.e., after all items of an iterable in the for

statement or when a conditional expression of a while statement evaluates to False). Declaration of approval –

used in
if
statement if nothing should be done to a conditional expression. It has this syntax:
if conditional_expression:
Declarations)
elif conditional_expression:
Declarations)
elif conditional_expression:

Pass #Nothing to do if this condition is met other: Declarations) Exception Handling - Support for Python try , except , finally , other and with

instructions for exception handling. An exception can be raised in code explicitly by a
elevation

  • declaration. When an exception is raised implicitly or explicitly, normal code execution deviates until the exception is resolved.
  • Functions
  • Functions are blocks of instructions that can be called. In Python, functions are also objects. They can be:

Assigned to references (variables, attributes, and other objects) Included as an item in an iterable Used as an argument in other functions

A function can also return another function. It is defined using a
definition

instruction and has this syntax:

def function_name(parameters):

statements)

The function name is an identifier that can be used to make function calls. Parameters are references that must be provided to a function when it is called. Parameters (or arguments) are optional and if a function does not require the provision of references, there is no reason to include them in the function definition.

If there are multiple parameters, they must be separated by commas in parentheses in the function definition. There may be one or more instructions executed when calling the function. These instructions are called the function body.
It is possible to have optional parameters in a function that may or may not be supplied when the function is called. These optional parameters are specified by an assignment expression, where the optional parameter is assigned a default value.

A function with optional parameters has this syntax:

def function_name(parameter, optional_parameter = expression):
statements)
Additionally, this is a valid example of a function with optional parameters:

def makeList(x, y= ): y.append(x) return

A function can return any data or object value using a

turn back

instruction, which can only be used in the body of a function. If the function does not have a return statement, an expression, a data value, or an object after a return keyword, the function returns no object.

Parameters that are not optional are called positional parameters. It is possible to supply any arbitrary number of positional parameters to a function with a prefixed asterisk
or double asterisk (**) before a parameter.
By prefixing an asterisk

to a parameter, it accepts any arbitrary number of positional parameters and collects them into a tuple. By prefixing a double asterisk

to a parameter, it accepts any arbitrary number of positional parameters and collects them in the dictionary.

Here is a valid example of accepting an arbitrary number of positional parameters in a function:

def add(*num)
return sum (num)
s = add(15, 20, 15) # s = 50

References given to a function when called are known as arguments. Arguments can be provided as positional or named arguments. If data values/objects are used as arguments without being assigned to parameter references, they will be accepted into the function call by the position of their appearance in the function signature. (Note: Function signature is the collection of parameters bound to a function).

These are called positional arguments. If data values/objects are used as objects by explicitly assigning them to parameter references, the position of their appearance in a function call does not matter. They are called named arguments or keywords.
Here is a valid example of the positional arguments:
def f(a, b, c):

return a = b = c

f(12, 17, 21) # returns 50

This is a valid example of named arguments (keyword):

def f(a, b, c):

return a = b = c
f(b = 12, a = 17, c = 21) # returns 50
In Python it is possible to define a function inside another function and this is called. The function defined inside another function is called a nested function and the function in which another function is defined is called an outer function. A nested function can use the parameters and references of an outer function. This nested function is called a closure.
A function can also return the values/objects at different times. For example, you may have income or income from statements. Such functions are called generators. When a generator is called, it returns the value/object or result of an expression in the first yield statement and saves all local references in its memory.
When the function is called again, it starts executing from the statements after the first yield statement and returns the value/object or expression result after another yield statement. The yield of a statement can be used if the expression containing it is an iterator.

Here is a valid example of a generating function:

def f(a, b, c):
produce a
yield a + b
yield a + b + c
for x in f(2, 5, 7): print(x) # returns 2, 7, 14

The generator uses the yield of instructions:

def f(a, b, c):

interval yield (a)
interval yield (b)
interval yield (c)

for x in f(1, 2, 3): print(x) # returns 0, 1, 0, 1, 2, 0, 1, 2, 3
Python also supports recursive functions, which are those that call themselves within the body itself.

Here is a valid example of a recursive function in Python: def factorial(n): if n == 1: return 1

else return n * factorial (n-1)

Scope of references
In Python, references can have local or global scope. This is called a namespace. References that bind to a function or other objects are local to that function or object. These references can only be used within the function or object to which they are linked.
References that appear outside of functions or other objects have global scope. These references can be used anywhere in the code. A reference can be made global by prefixing a
global

keyword. This is a valid example: a = 1

deff(b):

global breturn a + bIn the next tutorial, we will learn more about byte arrays, lists, sets, and dictionaries. While string, byte, range and tuple are immutable sequences, byte array and list are just mutable sequences in Python. Sets and collections are most often used in unordered collections.

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.