Python Data Types Explained
Python Data Types Explained
Python
ENG. MARIO DAVID ROSAS
Data types
No, we are not talking about everyone floating like in 'IT', this term
It refers to all real numbers that contain a decimal point.
Normally in the division operation of two integers
we can obtain a decimal quotient (the result); but due to
to the nature of Python described above, we do not
we would see the result in its decimal form, so that it does not
it brings us problems we will use this line of code ALWAYS THAT
LET'S NOT WANT TO FIGHT WITH DECIMALS IN DIVISION:
Exercise
Execute the following code and save it in your folder with the name
de: practica1_"grupo".py (example: practica1_3ro.py)
Expected result
Strings
There are many methods for the 'string' object just like several
attributes (characteristics) of it.
To see everything about strings, you can enter the following
league[Link] but the methods
and attributes that we will review would be the most used in the
simple programming.
String writing methods
Writing:
This is a string
This is also a string
A backslash (\) or '' will be used to escape (be able to paint) a quote:
Example: 'This is a string' This is also a 'string'
ProductThis is also a 'string'
To expand the string to two lines we will also use backslash:
This is a longer text
that takes up two lines We will not put the comment in the first line
Another way to create a multi-line string is by using "three quotes".
(single or double, it doesn't matter).
This is indeed
a very text
very long""" # Comments always on the last line
Common string methods
[Link]()
The method capitalize() returns a copy of the string with its
first letter in uppercase.
Example:
[Link](suffix)
The method endswith() returns True if the string ends with the
specified suffix.
Example:
[Link](sub)
Return the smallest index of the string where the substring was found
string. Returns -1 if the substring is not found.
Example:
[Link]()
It allows us to perform a string format operation. The
the string in which this method is called may contain text
literal or replacement fields delimited by braces { }. Each
replacement field contains the numerical index of a
positional argument or the name of a word argument
[Link] a copy of the string where each field of
replacement is replaced with the value of the argument string
corresponding.
Example:
[Link](sub)
The method indexes is very similar to the method find. With the difference of
that when the substring is not found, it throws an index
unValueError.
Example:
[Link]()
Returns true if all characters in the string are
alphanumeric and there is at least one character, otherwise it is false.
Example:
[Link]()
The method isdigit() returns True if all characters of the
the string consists of digits.
Example:
[Link]()
The method lower() returns a copy of the string with all its
lowercase characters.
Example:
[Link]()
The upper() method returns a copy of the string with all
his characters in uppercase.
Example:
lend()
It will count the length of our string (the total number of
characters including blank spaces
Example:
[Link]()
It will count the number of times a string repeats or
sequence of characters (sub) within it.
Example:
[Link]()
It will convert our string into a list, using as criteria
separator, the argument we introduce between the parentheses.
Example:
[Link]()
It is used to replace certain substrings with other substrings,
indicating in parentheses, firstly, the string to be replaced and
secondly, the new chain:
Example:
Exercise
practice2_"group".py
Create a program that reads a string from the keyboard and displays it
next information:
Print the first two characters.
Print the last three characters.
Print said string every two characters. E.g.: straight should print
rca
The said string in reverse. E.g.: hello world! should print
!hello world
Print the string in one direction and in the opposite direction. Eg: reflection prints
reflectionwolf.
Possible solution (There may be more)
Expected result
Booleans
A boolean variable is
a variable that only
you can take two possible
values: True (true) or
False.
In Python, any variable
(in general, any
object) can be considered
how a variable
Boolean. In general the
null or empty elements are
consider False and the rest
they are considered True.
Booleans
and: "y" logical. This operator results in True if and only if its
two operands are True
Booleans
A list is a sequence
ordered mutable elements.
The items in a list can be
objects of different types.
To specify a list you
they indicate the separate elements
for commas inside of
brackets.
To denote an empty list, one
they use two empty brackets.
Lists
append()
This method adds an element to the end of a list.
count()
This method receives an element as an argument, and counts the
number of times it appears in the list.
extend()
This method extends a list by adding an iterable to the end.
index()
This method takes an element as an argument and returns the
index of its first appearance in the list.
The sort() method accepts the reverse option, by default, with a value
False. If set to True, the sorting is done in ascending order.
inverse.
To iterate over two or more sequences at the same time, the values
they can be paired with the built-in functionzip().
Tuples
You can access the values of the dictionary using its key, you
present some examples below:
Dictionaries
fromkeys()
This method creates a new dictionary with keys based on a
data type sequence. The default value of value is the type
None.
In the previous example, initialize the values of each key to None,
but you can initialize a common default value for each key:
get()
This method returns the value based on a match of
search in a dictionary using a key, otherwise
return the objectNone.
has_key()
This method returns the value True if the dictionary is present
the key sent as an argument.
items()
This method returns a list of pairs of dictionaries (key,
value), as 2 tuples.
keys()
This method returns a list of the dictionary's keys:
pop()
This method specifically removes a key from the dictionary and
returns the corresponding value. Throws an exceptionKeyError yes
laclaveno is found.
popitem()
This method removes and returns a key-value pair.
dictionary as a 2 tuples. Throw an exceptionKeyError yes
the dictionary is empty.
update()
Este método actualiza un diccionario agregando los pares clave-
values in a second dictionary. This method does not return anything.
The update() method takes a dictionary or an iterable object of
key/value pairs (usually tuples). If update() is called without
pass parameters, the dictionary remains unchanged.
values(), ítems() y keys()
This method returns a list of the requested values in the
diccionario:
The cmp() function is used in Python to compare values and
keys of two dictionaries. If the function returns the value0if
both dictionaries are the same, return value1 if the first
the dictionary is greater than the second dictionary and returns the
value-1 if the first dictionary is less than the second
dictionary.
The function len() is the same built-in function in the interpreter.
Python but applied to the use of the dictionary type sequence.
The function del() is also the same as the interpreter and removes the item.
of the object passed by parentheses example of (dict[key]).