To write and run Python code, I use Visual Studio Code (VS Code), I find it intuitive and you can install tons of useful extensions.
Since I want to draw a line through my journey that truly starts from zero,
I’m going to document everything step by step, even the most basic concepts.
If you think that I’m missing something just let me now, write me an email specifying what page or which part of the page can be adjusted 😂.
To start you have to:
- Download VS Code
- Install Python extension (search Python in the Extension tab on the left)
- Open a folder where you’ll save your Python files
- Create a new file and give it a name (you have to add “.py” at the end – test.py)
- Save the file on the just created folder.
- press Enter
💡 You can also use Google Colab to test code quickly but we’ll look at that later.
What now?
So, programming is just a way to give instruction to a computer using a language it can “understand”.
If I want a human to say “Hello” I just say:
Say “hello”!
With Python, we unfortunately can’t do the same thing.
It’s still simple though:
print("hello")
print is a predefined/standard function.
When you write this function, Python already knows what it’s supposed to do.
When you run (or execute) this line of code, a small area will appear, like a little screen, where you can see the result.
From now on, we’ll call this area the Terminal.
In this case the Terminal will show:"hello"
What do you need to know now?
Well, keep in mind that data have a type whose purpose is to let Python (or any other language) know how they need to be managed.
Let’s keep it simple… what’s 1, 2, 3, they are integers… for Python they are just int.
What about decimals? 1.8, 12.6, for Python they are float.
Last one, what is text for Python? text is str
There’s a way to know what type a data is if you’re unsure:
print(type("ciao"))
print(type(12))
print(type(1.5))
This will return:str
int
float
Just keep this in mind for now…
So, back to us…
When we write “print(----)” the space between parenthesis can be also used like a calculator
print(3 + 2)
The Terminal will show: 5
There’s more, we have Variables to which we can assign values.
To do so, we just need to define them and use them after.
In Python, to define a variable, you simply write the variable name, an “=”, and the value you want to assign.
The “=” is the “assignment operator”
There are many other operators but we’ll go through them one at a time.
Here’s an example:
a = 5
print(a)
Why do we have to use variables?
Let’s use an other example
print(10 + 3)
print(20 + 3)
print(30 + 3)
print(40 + 3)
Oh no…wait… I change my mind, I want to add to every number the number 4 instead of 3…
Well… Now you have to do it manually for every time you used number 3
OOOR you can just…
a = 3
print(10 + a)
print(20 + a)
print(30 + a)
print(40 + a)
You changed your mind, again?
no problem, just transform a=3 in a=4 and voilà , all your calculations are updated.
A good way to organize variables is to name them in a way you can easily recall what they serve for.
Imagine this:
You’re coding, you’ve got 20 variables, and you need to do calculations with them…
But all the variable names are things like lightbulb, chihuahua, television.
chihuahua=10
television=5
lightbulb= chihuahua+television
print(f"the sum is {lightbulb}")
Very, very bad move.
Make your life easier, just name things with some logic, please.
When you read your code a week later, you don’t want to play detective.
Trust me.
Look at the same code now:
# same code with logic
a=10
b=5
sum=a+b
print(f"the sum is {sum}")
# or
a=10
b=5
print(a+b)
Minimum effort, maximum result.
Ok, let’s explain ALL:
If you’ve been paying attention, you probably noticed that I wrote some weird stuff earlier.
Let’s break it down:
Besides naming variables logically, there is another way you can really make your life easier when coding:
Comment your code!
Whaat?
yes, comment it, I know you know what you wrote, and you don’t need explanations… of course… right? WRONG.
Look at this code:
n_s=random.randint(1,20)
a=None
while a!=n_s:
a=int(input("Choose"))
if a==n_s:
print("Good!")
break
elif a>n_s:
print("Lower")
elif a<n_s:
print("Higher")
else: pass
I voluntarily picked this code because, at this point… like… what the fuck is this?
BUT, even if this code is a next-level step, at least the person who wrote it, could’ve made it simpler to understand for whoever’s reading it… Do you agree?
Yes? Niiice!
DON’T BE THAT PERSON THEN!
Soon or later someone else will read your code, make both of your lives easier 😂.
To add a comment in your code, just use the # symbol.
Everything written after the # on that line will be ignored by Python when the program runs.
Comments are useful to explain what your code is doing. They don’t affect how the program works, but they help you (and anyone else reading your code) understand it better,especially if you come back to it after a few days or weeks.
Now, let’s review the same code using these tips:
import random # Import the random module to generate random numbers
# Generate a secret number between 1 and 20
secret_number = random.randint(1, 20)
# Initialize the user's guess variable
guess = None
# Keep asking the user until they guess the correct number
while guess != secret_number:
# Ask the user to input a number
guess = int(input("Choose a number: "))
# Check if the guess is correct
if guess == secret_number:
print("Well done!") # Congratulate the user
break # Exit the loop
elif guess > secret_number:
print("Too high") # Hint: the guess was too high
elif guess < secret_number:
print("Too low") # Hint: the guess was too low
else:
pass # This branch won't actually be reached, but included for completeness
Of course, for now, this code is still out of reach, but the comments at least show you what it’s all about
In conclusion, last but not least…
what’s that f in front of a string?
When you want to show something on the screen, you use the print() function.
But if you want to mix text and variables in the same sentence, you need to follow some special formatting rules and adding an f before the quotation marks it’s a way to not give a fuck about them…
The only thing to remember is that the variables need to be enclosed by curly brackets and you’re cool!
I think it’s enough for now, I will soon put exercises and solutions about this topic here!


Leave a comment