๐ Day 3 โ Python Essentials
Inputs, Conditions, Loops & Strings
I focused on practising the fundamentals of Python programming: inputs, conditions, loops, and string manipulation. Basically, the essential building block, not only for basics, but also for financial analysis tasks (e.g., validating inputs, iterating over datasets, etc.).
Main Goals:
- Generating numbers: learned how to handle user input (input()) and convert it into numeric types
- Using conditionals (if / elif / else)
- Loops like for and while cycles
- String methods, and patterns with them
Step by Step
๐ Exercise 1: Inputs & Arithmetic I asked the user for two integers (that I can type after the code runs), and applied basic operations (sum, difference, product, division):
```python
a = int(input("Primo Numero: "))
b = int(input("Secondo Numero: "))
risultati = {"somma": a+b, "differenza": a-b, "prodotto": a*b, "divisione": round(a/b, 2)}
print(risultati)
```
๐ Exercise 2: Conditions. Using if / elif / else, I built a simple grading system; a logic that can be useful in finance as well (e.g., categorizing risk scores like in a rating agency).
๐ Exercise 3: For Loop Printed numbers from 1 to n, reinforcing iteration basics.
๐ Exercise 4 โ While Loop Calculated the sum of integers from 1 to n with a while loop โ a great exercise in understanding loop termination conditions.
๐ Exercise 5 โ Strings Cleaned a user-input sentence: removed spaces, converted to lowercase, and replaced vowels with โ*โ. This simulates text preprocessing.
๐ Exercise 6 โ Palindrome Check Checked if a word reads the same backward โ practicing indexing and string slicing ([::-1]).
Challenges / Insights
- Remembering to convert input() from string to integer before doing math (int() casting).
- Ensuring loops terminate correctly (avoiding infinite loops).
- Understanding string immutability: every transformation creates a new string.
- Seen how simple conditions and loops could translate directly into financial logic (e.g., thresholds for buy/sell signals).
Code Snippet Final
```python
# Example: while loop sum
numero = int(input("Numero positivo: "))
contatore, somma = 1, 0
while contatore <= numero:
somma += contatore
contatore += 1
print(f"La somma da 1 a {numero} รจ: {somma}")
```
Next Step
๐ Combine these fundamentals with financial use cases โ applying conditions and loops to stock returns and building simple logic for portfolio signals.
Youโll find my projects here:
- ๐ GitHub Repository
- ๐ Google Colab