29. First lessons of Python

Hello there,


I recently started learning Python from the amazing Dr. Angela Yu : 100 Days of Code: The Complete Python Pro Bootcamp It seems that learning improves when I compare the thing that I am learning to something I have more experience with.


In this case, as a beginner in Python, I am able to notice that:


  • Python seems to be more readable. When I was learning JavaScript (still do!), many times I was confused by the syntax, all the curly braces and so on. I know, the code editors are doing a great job drawing your attention to possible syntax mistakes, but not to any underlying logic issues, which in my case come from not knowing yet the ins and the out of coding but also from the syntax requirements. This is a simple if statement in Python:



# Example of an if statement in Python 
 
age = 18 
if age >= 18: 

print("You are eligible to vote.")

....................................................JAVASCRIPT.....................................................

//Example of an if statement in JavaScript 

let age = 18; 
if (age >= 18) 

{ console.log("You are eligible to vote."); }


I find the whole braces thing a bit cumbersome, even if I understand what their role is.


  • The For Loop is also very easy to understand in Python > for the item in the list, do something while in JavaScript it's all jumbled together:


# Example of a for loop in Python 

fruits = ["apple", "banana", "cherry"] 

for fruit in fruits: 
  print(fruit)

....................................................JAVASCRIPT.....................................................

// Example of a for loop in JavaScript

let fruits = ["apple", "banana", "cherry"];


for (let fruit of fruits) {
    console.log(fruit);
}






  • Writing functions in Python feels like a cold bandage on a wound :


# Define a function to greet a user in Python

def greet(name):
    return f"Hello, {name}!"


# Call the function

message = greet("Alice")
print(message)

....................................................JAVASCRIPT.....................................................

// Define a function to greet a user in JavaScript 

function greet(name) {
    return `Hello, ${name}!`;
}


// Call the function
let message = greet("Alice");
console.log(message);




  • I find also find the Lists and their methods in Python easier to understand. In this example, you can see how much easier it is to generate a random choice in Python(random.choice), compared to JavaScript (fruits[Math.floor(Math.random() * fruits.length)];)


import random  # Import the random module for random operations


# Define a list
fruits = ["apple", "banana", "cherry", "date"]


# Accessing elements
print(fruits[0])  # Output: apple


# Adding an element
fruits.append("elderberry")
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']


# Removing an element
fruits.remove("banana")
print(fruits)  # Output: ['apple', 'cherry', 'date', 'elderberry']


# Get a random element
random_fruit = random.choice(fruits)
print(f"Random fruit: {random_fruit}")


# Sorting the list
fruits.sort()
print(fruits)  # Output: ['apple', 'cherry', 'date', 'elderberry']


....................................................JAVASCRIPT.....................................................

// Define an array
let fruits = ["apple", "banana", "cherry", "date"];


// Accessing elements
console.log(fruits[0]); // Output: apple


// Adding an element
fruits.push("elderberry");
console.log(fruits); // Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']


// Removing an element
fruits.splice(fruits.indexOf("banana"), 1);
console.log(fruits); // Output: ['apple', 'cherry', 'date', 'elderberry']


// Get a random element
let randomFruit = fruits[Math.floor(Math.random() * fruits.length)];
console.log(`Random fruit: ${randomFruit}`);


// Sorting the array
fruits.sort();
console.log(fruits); // Output: ['apple', 'cherry', 'date', 'elderberry']





The course I mentioned above it filled with little projects and challenges. I learned from my previous experiences that those challenges and exercises are gold for a learner and I do try to fulfill all of them, unless I get stuck.


I managed to make a game in Python, the poor Hangman who rarely gets to live. You can check it out here.


It turns out that deploying a Python application requires a lot of things to be set up. I had to use something called Flask. With the help of genius ChatGPT I learn that this is a micro-framework providing the necessary tools to handle requests, routing and templates allowing to develop applications and deploy them. I used PythonAnywhere to host my Hangman. One of the steps to configure the WSGI - Web Server Gateway Interface, which makes the communication with the web server possible. I know nothing about web servers, but the fact that I got to communicate with them via this WSGI is pretty cool! PythonAnywhere and this guy on YouTube helped me deploy, which in many cases before turned out to be the part I didn't like.


That's it for now.


Until next type, be curious and learn something every day :)