|
| 1 | +# Python divide sus operadores en los siguientes grupos: |
| 2 | +# * Operadores Aritmeticos |
| 3 | +# * Operadores de Asignacion |
| 4 | +# * Operadores de Comparacion |
| 5 | +# * Operadores logicos |
| 6 | +# * Operadores de identidad |
| 7 | +# * Operadores de pertenencia |
| 8 | +# * Operadores a nivel de bit |
| 9 | + |
| 10 | +# OPERADORES ARITMETICOS |
| 11 | + |
| 12 | +# SUMA + |
| 13 | +x = 2 |
| 14 | +y = 3 |
| 15 | +z = x + y |
| 16 | +print(z) |
| 17 | + |
| 18 | +# RESTA - |
| 19 | +x = 7 |
| 20 | +y = 5 |
| 21 | +z = x - y |
| 22 | +print(z) |
| 23 | + |
| 24 | +# MULTIPLICACION * |
| 25 | +x = 5 |
| 26 | +y = 2 |
| 27 | +z = x * y |
| 28 | +print(z) |
| 29 | + |
| 30 | +# DIVISION / |
| 31 | +x = 10 |
| 32 | +y = 2 |
| 33 | +z = x / 2 |
| 34 | +print(z) |
| 35 | + |
| 36 | +# MODULUS % |
| 37 | + |
| 38 | +x = 3 |
| 39 | +y = 2 |
| 40 | +z = x % y |
| 41 | +print(z) |
| 42 | + |
| 43 | +# Exponenciación |
| 44 | + |
| 45 | +x = 2 |
| 46 | +y = 3 |
| 47 | +z = 2**3 |
| 48 | +print(z) |
| 49 | + |
| 50 | +# Floor division (en Python es una operación matemática |
| 51 | +# que devuelve el mayor número entero posible). |
| 52 | +x = 7 |
| 53 | +y = 2 |
| 54 | +z = x // y |
| 55 | +print(z) |
| 56 | + |
| 57 | +# OPERADORES DE ASIGNACION |
| 58 | + |
| 59 | +# Operador (=) |
| 60 | +x = 10 |
| 61 | +print(x) |
| 62 | + |
| 63 | +# Operador (+=) |
| 64 | +x = 10 |
| 65 | +x += 5 |
| 66 | +print(x) |
| 67 | + |
| 68 | +# Operador (-=) |
| 69 | +x = 10 |
| 70 | +x -= 5 |
| 71 | +print(x) |
| 72 | + |
| 73 | +# Operador (*=) |
| 74 | +x = 10 |
| 75 | +x *= 5 |
| 76 | +print(x) |
| 77 | + |
| 78 | +# Operador (/=) |
| 79 | +x = 10 |
| 80 | +x /= 5 |
| 81 | +print(x) |
| 82 | + |
| 83 | +# Operador (%=) |
| 84 | +x = 10 |
| 85 | +x %= 5 |
| 86 | +print(x) |
| 87 | + |
| 88 | +# Operador (//=) |
| 89 | +x = 10 |
| 90 | +x //= 5 |
| 91 | +print(x) |
| 92 | + |
| 93 | +# Operador (**=) |
| 94 | +x = 2 |
| 95 | +x **= 5 |
| 96 | +print(x) |
| 97 | + |
| 98 | +# Operador (&=) |
| 99 | +a = 12 # in binary: 1100 |
| 100 | +b = 10 # in binary: 1010 |
| 101 | +a &= b # now 'a' becomes 8, which is 1000 in binary |
| 102 | +print(a) |
| 103 | + |
| 104 | +# Operador (|=) |
| 105 | +a = 12 # in binary: 1100 |
| 106 | +b = 10 # in binary: 1010 |
| 107 | +a |= b # now 'a' becomes 14, which is 1110 in binary |
| 108 | +print(a) |
| 109 | + |
| 110 | +# Operador (^=) |
| 111 | +a = 12 # in binary: 1100 |
| 112 | +b = 10 # in binary: 1010 |
| 113 | +a ^= b # now 'a' becomes 6, which is 0110 in binary |
| 114 | +print(a) |
| 115 | + |
| 116 | +# Operador (>>=) |
| 117 | +a = 12 # in binary: 1100 |
| 118 | +a >>= 2 # now 'a' becomes 3, which is 0011 in binary |
| 119 | +print(a) |
| 120 | + |
| 121 | +# Operador (<<=) |
| 122 | +a = 3 # in binary: 0011 |
| 123 | +a <<= 2 # now 'a' becomes 12, which is 1100 in binary |
| 124 | +print(a) |
| 125 | + |
| 126 | +# OPERADORES DE COMPARACION |
| 127 | +# Operador igual (==) |
| 128 | +x = 5 |
| 129 | +y = 3 |
| 130 | +print(x == y) # Retorna falso porque x no es igual a y |
| 131 | + |
| 132 | +# Operador no igual (!=) |
| 133 | +x = 5 |
| 134 | +y = 3 |
| 135 | +print(x != y) # Retorna verdadero porque x no es igual a y |
| 136 | + |
| 137 | +# Operador mayor que (>) |
| 138 | +x = 5 |
| 139 | +y = 3 |
| 140 | +print(x > y) # Retorna verdadero porque x no es mayor a y |
| 141 | + |
| 142 | +# Operador menor que (>) |
| 143 | +x = 5 |
| 144 | +y = 3 |
| 145 | +print(x < y) # Retorna falso porque x no es menor a y |
| 146 | + |
| 147 | +# Operador mayor o iqual que (>=) |
| 148 | +x = 5 |
| 149 | +y = 3 |
| 150 | +print(x >= y) # Retorna verdadero porque x es mayor a y |
| 151 | + |
| 152 | +# Operador menor o iqual que (<=) |
| 153 | +x = 5 |
| 154 | +y = 3 |
| 155 | +print(x <= y) # Retorna falso porque x es mayor a y |
| 156 | + |
| 157 | +# OPERADORES LOGICOS |
| 158 | + |
| 159 | +# Operador AND |
| 160 | +x = 5 |
| 161 | +print(3 < x and x < 10) # Retorna verdadero porque x es mayor que 3 y |
| 162 | + # tambien es menor que 10 |
| 163 | + |
| 164 | +# Operador OR |
| 165 | +x = 5 |
| 166 | +print(3 < x or x < 2) # Retorna True porque cumple con alguna de las condiciones, |
| 167 | + # en este caso x es mayor que 3 |
| 168 | + |
| 169 | +# Operador NOT |
| 170 | +x = 5 |
| 171 | +print(not(3 < x and x < 10)) # Revierte el resultado, retorna falso si el resultado es verdadero |
| 172 | + # para nuestro caso retorna falso |
| 173 | + |
| 174 | +# OPERADORES DE IDENTIDAD |
| 175 | + |
| 176 | +# Operador IS |
| 177 | +x = ["apple", "banana"] |
| 178 | +y = ["apple", "banana"] |
| 179 | +z = x |
| 180 | +print(x is z) # Retorna verdadero porque z es el mismo objeto que x |
| 181 | + |
| 182 | +# Operador IS NOT |
| 183 | +x = ["apple", "banana"] |
| 184 | +y = ["orange", "banana"] |
| 185 | +print(x is y) # Retorna falso porque x no es el mismo objeto que y |
| 186 | + |
| 187 | +# OPERADORES DE PERTENENCIA |
| 188 | + |
| 189 | +# Operador IN |
| 190 | +x = ["apple", "banana"] # Devuelve verdadero si una secuencia con el valor |
| 191 | + # especificado está presente en el objeto. |
| 192 | +print("banana" in x) |
| 193 | + |
| 194 | +# Operador NOT IN |
| 195 | +x = ["apple", "banana"] # Retorna verdadero porque en la secuencia el |
| 196 | + # valor de "pineapple" no se encuentra en la lista |
| 197 | +print("pineapple" not in x) |
| 198 | + |
| 199 | +# OPERADORES A NIVEL DE BIT |
| 200 | + |
| 201 | +# Operador & (AND) |
| 202 | +print(6 & 3) |
| 203 | +""" |
| 204 | +The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0: |
| 205 | +
|
| 206 | +6 = 0000000000000110 |
| 207 | +3 = 0000000000000011 |
| 208 | +-------------------- |
| 209 | +2 = 0000000000000010 |
| 210 | +==================== |
| 211 | +""" |
| 212 | + |
| 213 | +# Operador | (OR) |
| 214 | +print(6 | 3) |
| 215 | +""" |
| 216 | +The | operator compares each bit and set it to 1 if one or both is 1, otherwise it is set to 0: |
| 217 | +
|
| 218 | +6 = 0000000000000110 |
| 219 | +3 = 0000000000000011 |
| 220 | +-------------------- |
| 221 | +7 = 0000000000000111 |
| 222 | +==================== |
| 223 | +""" |
| 224 | + |
| 225 | +# Operador | (XOR) |
| 226 | +print(6 ^ 3) |
| 227 | +""" |
| 228 | +The ^ operator compares each bit and set it to 1 if only one is 1, otherwise (if both are 1 or both are 0) it is set to 0: |
| 229 | +
|
| 230 | +6 = 0000000000000110 |
| 231 | +3 = 0000000000000011 |
| 232 | +-------------------- |
| 233 | +5 = 0000000000000101 |
| 234 | +==================== |
| 235 | +""" |
| 236 | + |
| 237 | +# Operador ~ (NOT) |
| 238 | +print(~3) |
| 239 | +""" |
| 240 | +The ~ operator inverts each bit (0 becomes 1 and 1 becomes 0). |
| 241 | +
|
| 242 | +Inverted 3 becomes -4: |
| 243 | + 3 = 0000000000000011 |
| 244 | +-4 = 1111111111111100 |
| 245 | +""" |
| 246 | + |
| 247 | +# Operador << (Zero fill left shift) |
| 248 | +print(3 << 2) |
| 249 | +""" |
| 250 | +The << operator inserts the specified number of 0's (in this case 2) from the right and let the same amount of leftmost bits fall off: |
| 251 | +
|
| 252 | +If you push 00 in from the left: |
| 253 | + 3 = 0000000000000011 |
| 254 | +becomes |
| 255 | +12 = 0000000000001100 |
| 256 | +""" |
| 257 | + |
| 258 | +# Operador >> (Signed right shift) |
| 259 | +print(8 >> 2) |
| 260 | +""" |
| 261 | +The >> operator moves each bit the specified number of times to the right. Empty holes at the left are filled with 0's. |
| 262 | +
|
| 263 | +If you move each bit 2 times to the right, 8 becomes 2: |
| 264 | + 8 = 0000000000001000 |
| 265 | +becomes |
| 266 | + 2 = 0000000000000010 |
| 267 | +""" |
| 268 | + |
| 269 | +# ESTRUCTURAS DE CONTROL |
| 270 | + |
| 271 | +# CONDICIONALES |
| 272 | +# IF |
| 273 | +x = 7 |
| 274 | +y = 5 |
| 275 | + |
| 276 | +if x > y: |
| 277 | + print("x es mayor que y") |
| 278 | + |
| 279 | +# ELIF |
| 280 | + |
| 281 | +x = 5 |
| 282 | +y = 7 |
| 283 | + |
| 284 | +if x > y: |
| 285 | + print("x es mayor que y") |
| 286 | +elif y > x: |
| 287 | + print("y es mayor que x") |
| 288 | + |
| 289 | +# ELSE |
| 290 | + |
| 291 | +x = 7 |
| 292 | +y = 7 |
| 293 | + |
| 294 | +if x > y: |
| 295 | + print("x es mayor que y") |
| 296 | +elif y > x: |
| 297 | + print("y es mayor que x") |
| 298 | +else: |
| 299 | + print("x es igual que y") |
| 300 | + |
| 301 | +# SHORT HAND IF |
| 302 | + |
| 303 | +x = 7 |
| 304 | +y = 5 |
| 305 | + |
| 306 | +if x > y: print("x es mayor que y") |
| 307 | + |
| 308 | +# SHORT HAND IF... ELSE |
| 309 | + |
| 310 | +x = 5 |
| 311 | +y = 7 |
| 312 | + |
| 313 | +print("x es mayor que y") if x > 7 else print("y es mayor que x") |
| 314 | + |
| 315 | +# MATCH CASE |
| 316 | + |
| 317 | +fruta = "manzana" |
| 318 | + |
| 319 | +match fruta: |
| 320 | + case "naranja": |
| 321 | + print("Es una naranja") |
| 322 | + case "platano": |
| 323 | + print("Es un platano") |
| 324 | + case "frutilla": |
| 325 | + print("Es una frutilla") |
| 326 | + case "manzana": |
| 327 | + print("Es una manzana") |
| 328 | + |
| 329 | +# ITERATIVAS |
| 330 | + |
| 331 | +# FOR |
| 332 | +frutas = ["manzana", "naranja", "platano"] |
| 333 | +for fruta in frutas: |
| 334 | + print(fruta) |
| 335 | + |
| 336 | +# WHILE |
| 337 | +i = 0 |
| 338 | +while i < 3: |
| 339 | + print("meow") |
| 340 | + i += 1 |
| 341 | + |
| 342 | +# EXCEPCIONES |
| 343 | + |
| 344 | +while True: |
| 345 | + try: |
| 346 | + user_input = int(input("Que es n?: ")) |
| 347 | + |
| 348 | + except ValueError: |
| 349 | + print("valor incorrecto, trate nuevamente") |
| 350 | + pass |
| 351 | + |
| 352 | + else: |
| 353 | + print("El valor de n es:", user_input) |
| 354 | + break |
| 355 | + |
| 356 | +# DIFICULTAD EXTRA (opcional): |
| 357 | +# Crea un programa que imprima por consola todos los números comprendidos |
| 358 | +# entre 10 y 55 (incluidos), pares, y que no son ni el 16 ni múltiplos de 3. |
| 359 | + |
| 360 | +for n in range(10, 56): |
| 361 | + if n % 2 == 0 and n % 3 != 0 and n != 16: |
| 362 | + print(n) |
| 363 | + |
0 commit comments