These are some Python tutorials by Boyan Tabakov designed to get a basic grasp of Python code.
Below is some links to basic tutorials.
Return to Python Online Interpreter
Hello World is the most basic program that is often done when coding. This helps you get a basic grasp of Python and it's workings. It also has nothing to do with printing, so do not worry, we will not be wasting any ink.
Hover over the editor below and click the play button to execute code. Remember this, as this will be required when coding in this interactive environment.
As you can see, below is outputted "Hello World!". This is how code will be outputted for the rest of these tutorials. This indicates that our code works and that Python can execute it.
Now, let's look at how our code is structured.
print("Hello World!")
We can see that after the word "print", we have 2 brackets and inside them is the contents of "Hello World!". This shows us the basic structure of a command in Python, which is
function(argument)
The word "function" above is a placeholder for any function that you might want, such as print, input, open, etc. Then, immediately we have 2 brackets which enclose something. That something is an argument. An argument is essentially some parameters for a function that give it additional information on how it should operate, just like we saw in our Hello World function. We can see that print accepts an argument because when we put in text in quotes, it shows up. Generally, most functions have arguments and they can help you a lot.
Activity: Try it yourself by editing the above in the editor to say "Hello Boyan".
Next up we will learn about loops. Loops in Python tell a function (or functions) how many times to be executed. They are very important as instead of repeating code like this:
We can have code repeat 5 times like this:
These loops are called "for" loops. Below is their basic structure:
for i in range(number of times to execute):
[indent of 2 spaces]functions(argument)
As you can see above, the code has 2 indents, which are 2 spaces or 1 press of the "tab" key on your keyboard. These are important as they tell Python where your code is. So indent of 2 spaces means that you add 2 spaces.
The loop that you created has a range function, and any number that you put inside it is how many times your code will cycle through. So, if it is 5 times, the functions inside it will be executed 5 times.
Activity: Print Hello World and Hello Boyan 7 times using the for loop above.
You can imagine variables like a box, (π¦) which store only 1 type of information. For example in a box (π¦), if you put grapes (π), you cannot put bananas (π), but you can still keep adding grapes (π).
In Python, this is the same but with strings(pieces of text), integers(all numbers without a dot, so like 1 is an integer but 1.5 is not), float(same as integer, but vice versa), bool(boolean, which is True or False), etc. What I just mentioned are data types. These data types can only be contained in 1 variable. Mixing up the two will give you an error. For example, you cannot add a string with an integer. Just like you cannot add grapes(π) to pizza(π).
Above is a data type of string that is inside a variable called "myvariable". This variable contains 1 data type. And it is defined with an equals to sign. Inside the variable is "Hello World!".
Activity: Change the variable to say: "Hello Boyan!"
As you can see above, we have 2 numbers inside 2 variables. Hover over the editor and click the play button. You will see that the 2 numbers are added and we get a sum of 3.
Activity: Change the numbers above so that they add up to 4
Hmm. These 2 variables won't add up to 3? Why is that? See if you can figure out the problem and correct it.
Hint: Take a look at data types above.
The answer was: the variable data types are not the same! As we said above, we cannot combine 2 data types! What you could have come up with, is:
1. Removing the quotes, which correctly adds up the numbers to 3.
2. Added quotes on the first number, so it now becomes "12".
For anyone who solved the question like the first answer, and are wondering why the second answer prints out 12: here is why. In the 2 variables, we are telling Python that 1 and 2 are pieces of text and not numbers, so Python3 thinks that these are simply pieces of text and when you add 2 pieces of text, they join together, so if you were to add "Hello " + "World", that would make "Hello World". Try it out below.
A float, is a number with a dot, so a number and a fraction, example: 1.4; 4.4; 93.3; etc. Now, let's see if you can make the numbers below equal to 5:
Now that our number is a whole number, how do we get rid of the .0? We can do that, by changing the data type when we add the numbers up to int(integer). See below:
We can even convert a string to a integer, which can solve our problem with this activity!
But, some data types cannot be converted, like converting a piece of text to a number:
This topic goes into the general data types and how they should be used. This section is more of a cheat sheet, so it can be used to figure out data types/use them in later activities.
Data type: string
Conver to string function: str(content)
Data type: float
Convert to float function: float(content)
Data type: integer
Convert to integer function: int(content)
Data type: Boolean
Convert to boolean function: bool(content)
If statements in Python are there to determine whether something should happen. Below is their general structure:
if (condition 1) is equal to(==)/not equal to(!=)/greater than(>)/less than(<)/greater than or equal to(>=)/less than or equal to (<=) (condition 2) and/or (more conditions):
[indent of 2 spaces]then do something
Looking at the code above, can you determine which statements will be triggered?
Answer: "myvariable1 is less than or equal to myvariable2" and "myvariable1 is less than myvariable2"
Activity: Make the 2 numbers in the variables equal and see what happens!
Activity: Now that you know the general structure of an If Statement, Let's see if you can make one to compare the 2 numbers in the variables, and then if myvariable1 is bigger than myvariable2, make it print: Integers are worse than floats!
Answer below:
You can now write a program below for a Code Jumbo activity. Here is a description:
Below is the activity. 2 variables will be given. You will have to write a program that adds 2 variables, prints the result that is formatted like "Addition result: (your adding result which is rounded)" and then compares them using an If Statement, and whichever variable is larger than the other one, it gets printed as "(this variable) is larger than (the other variable)!"
This is an example of where we have to meet 2 conditions to execute the code in the if statement. Below is an example of an or statement:
So, let's discuss both and alternate methods of reaching both:
To explain it simply, when we have a statement like this below:
We have to meet the 2 conditions. myvariable1 has to be less than 2 AND myvariable2 has to be greater than 10 to execute the code.
As you can see above, they are not equal so they will not execute.
Activity: Make the 2 numbers meet the conditions and see what happens!
Answer:
Take a look at the following code below:
... and now run the code
You can see that we get the printed statement. How so?
This is because we are no longer using an AND parameter. We are now using an OR parameter. Here is how it works:
If myvariable1 is less than 2 OR myvariable2 is greater than 10, then do the indented code.
So we only have to meet 1 condition in order to execute code!
I will keep this short, but if for whatever reason you do not want to use AND or OR parameters, we can simply stack IF statements or use elif, which is useful. Look at the code below:
Let's say that we want to mimic the same code as above but without using AND. Let's look at stacking IF statements:
Here is how interpreting process would go:
1. Set the 2 variables to their respective data
2. Is myvariable1 less than 2? Yes, executing inside the indented code....
3. Is myvariable2 greater than 10? Yes, executing inside the indented code....
4. Print The conditions are met!
Now, let's take a look at OR statements using elif.
Let's see how we can mimic the OR parameter without using it.
Else if stands for else if. Here is how he interpreting process would go:
1. Set the 2 variables to their respective data.
2. Is myvariable1 less than 2? No, so now go to elif.
3. Is myvariable2 greater than 10? Yes, execute the code inside.
4. Print The conditions are met!
So essentially we are just telling Python to go to the other conditions if one does not work.
An else statement goes together with an IF statement, just like an elif statement. Both require an IF.
Let's understand the else statement
An important part of coding is comments. It is important to have them as they can tell you what certain functions do. It is important to comment your code because if multiple people are working on your code, they need to know what to do and what to make out of your code, and also if you are especially working on a school/college coding project. Here is how to make basic comments in Python code:
# This is a comment.
You can see that we add a comment by using a hashtag (#) and then putting text in front of it. Matter of fact: anything that is infront of a hashtag will not be interpreted as code! This also means that you can disable parts of code, which we will do in following activities.
Activity: Make a comment below to signify that the 2 numbers in the variables are floats.
Answer:
Activity: Let's comment on this Code Jumbo Activity. Make a comment in the beginning of the text that tells us that this is code that is for the Code Jumbo activity.
Answer:
Activity: Let's comment out the print function that prints "Hello World 6" on this code.
Answer:
Activity: Hmm. I wonder why my code is not working? Can you help me fix it?
Answer:
While Loops are similar to for loops, but there is a key difference:
For loops will keep executing until a certain number of executions is reached, and;
While loops will keep executing until a certain condition is met.
Let's see an example:
Let's explore the logic:
we set myvariable1 to 0; we set myvariable2 to 10; we enter a while loop, where:
1. check if myvariable1 is less than myvariable2(10)
if yes, then: add 1 to myvariable1; then print myvariable 1; then go to 1 and repeat until myvariable1 is greater than 10, in which case the loop breaks and we exit.
Now, here is the structure of a basic while loop:
while (condition is met):
do something
Activity: Make a while loop to add myvariable1 to myvariable2 and then myvariable2 to myvariable1 until both are greater than 300, and then print the output of both variables.
Answer:
So essentially, while loops are if statements that start executing code and will loop until the condition is met. They are also my favorite type of loops.
Modules are a great way to extend Python's functionality without requiring much code. This section will be quite fun. We will explore a couple of modules, and in the end, we will build a Python project that contains modules.
Let's start off simple.
Modules are always declared at the beginning of code, and that is usually so that you can catch any missing modules, or as they are also called, dependencies.
Let's take a look at a basic example of a Python module: the random integer module.
First, import the module using import random.
So, here is what we did now: we imported the random module, using the import function. Infact, this is how we will import modules from now. We will use import (module name)
Now, let's take the randint function and let's make use of it:
So, the syntax of random.randint is: random.randint(first number, second number)
So, essentially we are telling random.randint to randomize from this number to this number and give us an output.
This also means that we can embed this in functions! Look below:
We are now using the module in a for loop, that prints "I've got no idea how many times this will execute, anyone wanna bet?" an unknown number of times!
Activity: Make a dice roll program using the random module.
Answer:
Have you ever wanted to hear a cheesy software joke? Yes? Then this module is for you.
Let's see how we can integrate it!
As you can see, integration is very simple! All we have to do is import the module, and call pyjokes.get_joke(), and we get a software joke!
This module gives us a random English word! It's quite indicatively archchaplain! (Generated by the module)
So, how do we import it and use?
Simple. Just:
Use these 2 lines, and call a new word with r.get_random_word()
Has your teacher ever made you sing example text? Mine certainly has.
Now, we can generate some of it using Python. Check it out!
Even a paragraph!
We can't forget the dads and their cheesy jokes!
Not the texture pack, just ASCII art.
So, in summary:
Modules | Import methods | Usage function |
---|---|---|
Dadjoke | from dadjokes import Dadjoke joke = Dadjoke |
print(joke.joke) |
Programmer Art | import art | print(art.text2art("Hello Boyan!!")) |
PyJokes module | import pyjokes | print(pyjokes.get_joke()) |
Random Words | from random_word import RandomWords r = RandomWords() |
print(r.get_random_word()) |
Lorem Ipsum | import lorem | lorem.sentence(); lorem.paragraph() |
Random | import random | random.randint(0, 10) |
Answer: