Lazy loaded image
Python y Librerías para Ciencias de Datos
Lazy loaded imagePython Problems l
Palabras 537Tiempo de lectura 2 min
Jan 24, 2025
Jan 24, 2025
type
status
slug
summary
tags
category
icon
password

★ღ Squaring an Argument ★ღ✰

Write a function that takes an argument and returns the square of it.

★ Problem Breakdown ★

  • The function needs to take an argument and return the square.
  • The square of a number is the result of multiplying that number by itself. For example:
 

★ Flowchart ★

notion image

★ Code ★

  • We take the argument and multiply it by itself:

★ღ Even or Odd ★ღ✰

Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.

★ Problem Breakdown ★

  • The function needs to take an integer as input.
  • The function must check if the number is even or odd.
  • The function must return “Even” if the number is even or “Odd” if it’s odd.
💡
In python, we can use the % operator that calculates the remainder of a division.
Even and Odd numbers:
  • An even number is divisible by 2 without a remainder (2, 4, 6, ...).
  • An odd number leaves a remainder of 1 when divided by 2 (1, 3, 5, ...).

★ Flowchart ★

notion image

★ Code ★

  • We can use an if/else statement for checking whether a number is even or odd:
  • A more concise way to write this is using a ternary conditional:

★ღ Counting sheep ★ღ✰

Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present). For example:
🔍
Hint: Don't forget to check for bad values like null/undefined.

★ Problem Breakdown ★

  • The problem has an array that contains True or False values.
  • The function needs to count the number of sheep present in the array.
  • True means present.
💡
In python, we can use the count() method or we can iterate through each element in the array.

★ Flowchart ★

  • Flowchart iterating:
notion image
🔍
The .count() method functions for itself, so doesn’t require a flowchart.

★ Code ★

  • We call the .count() method, passing the argument “True” which counts the occurrences of the True value in the list:
  • First, we initialize a counter at 0, then iterate through the list, incrementing it (count += 1) for each True value.
💡
The reason we use if sheep instead of if sheep == True is that Python checks whether the value of sheep is 'truthy'. This is equivalent to writing if sheep == True, but it's shorter and cleaner.

 
上一篇
Iloc
下一篇
Tipos de Números y la Recta Numérica