Python
Es un lenguage interpretado, pensado (originalmente) para scripting.
Al igual que JavaScript y BASH, procesa todo en lote y llama a funciones especificamente si son convocadas de forma explícita.
- Comentarios
- Math Operators
- Augmented Assignment Operators
- Walrus Operator
- Concatenation and Replication
- Variables
- Condicionales
- Funciones comunes
Comentarios
Los comentarios son con "#"
Math Operators
From highest to lowest precedence:
Operators | Operation | Example |
---|---|---|
** | Exponent | 2 ** 3 = 8 |
% | Modulus/Remainder | 22 % 8 = 6 |
// | Integer division | 22 // 8 = 2 |
/ | Division | 22 / 8 = 2.75 |
* | Multiplication | 3 * 3 = 9 |
- | Subtraction | 5 - 2 = 3 |
+ | Addition | 2 + 2 = 4 |
>>> 2 + 3 * 6
# 20
>>> (2 + 3) * 6
# 30
>>> 2 ** 8
#256
>>> 23 // 7
# 3
>>> 23 % 7
# 2
>>> (5 - 1) * ((7 + 1) / (3 - 1))
# 16.0
Augmented Assignment Operators
Operator | Equivalent |
---|---|
var += 1 | var = var + 1 |
var -= 1 | var = var - 1 |
var *= 1 | var = var * 1 |
var /= 1 | var = var / 1 |
var //= 1 | var = var // 1 |
var %= 1 | var = var % 1 |
var **= 1 | var = var ** 1 |
>>> greeting = 'Hello'
>>> greeting += ' world!'
>>> greeting
# 'Hello world!'
>>> number = 1
>>> number += 1
>>> number
# 2
>>> my_list = ['item']
>>> my_list *= 3
>>> my_list
# ['item', 'item', 'item']
Walrus Operator
The Walrus Operator allows assignment of variables within an expression while returning the value of the variable
>>> print(my_var:="Hello World!")
# 'Hello world!'
>>> my_var="Yes"
>>> print(my_var)
# 'Yes'
>>> print(my_var:="Hello")
# 'Hello'
Concatenation and Replication
String concatenation
>>> 'Alice' 'Bob'
# 'AliceBob'
String replication:
>>> 'Alice' * 5
# 'AliceAliceAliceAliceAlice'
Variables
You can name a variable anything as long as it obeys the following rules:
It can be only one word.
>>> # good
>>> var = 'Hello'
>>> my_var_2 = 'Hello'
# It can’t begin with a number.
>>> 23_var = 'hello'
# Variable name starting with an underscore (_) are considered as “unuseful”.
>>> _spam = 'Hello'
Condicionales
if
if name == 'Debora':
print('Hi Debora!')
elif name == 'George':
print('Hi George!')
else:
print('Who are you?')
print('kid' if age < 18 else 'adult')
Si se necesita saber si una variable está vacia o no, no se debe usar "len([variable])" si no un simpĺe "if [variable]" y ya se evaluará por si mismo.
Funciones comunes
Imprimir en pantalla
print("[texto]", [variable], [modificador])
- El modificador "end=[letra]" permite añadir [letra] al final de cada palabra en la iteración del cilo "while", "for", etc.
- El modificador "sep=[letra]" permite añadir [letra] como separador de cada palabra en la iteración del cilo "while", "for", etc.
Imprimir variable sin texto previo
print(f'{[variable]}')
Tomar la entrada - input
Por defecto no llama al input directo, si no que esa variable debe ser impresa y ahí se ejecuta el input
[variable_store] = input("[texto a mostrar en el input]")
print(f'{[variable]}')
Largos - length
- Largo de caracteres
len('hello')
# 5
- Largo de una tupla
len(['cat', 3, 'dog'])
# 3