Strings in Python

In this blog, I am going to give you more information about string sin Python. Strings could be pretty simple things, just some letters after each other wrapped by apostrophes. I already explained the basic information about strings in my blog about the basic types of Python. You can always consult that information back.

In this blog, I am going to focus on how to manipulate string, Because creating is already discussed in the blog that I mentioned above.
A first way to modify a string is to isolate a couple of indexes. The first letter of your string has always the index 0, the second letter 1, and so on. Even spaces, commas and sign in your string gets an index number. It works exactly the same as isolating individual elements out of a list or tuple. So you create your string and gives it a name and then you put in square brackets behind the name the indexes you want to isolate. The string only always keeps his original shape. It is not possible to delete or change elements out of a string in the same way as with a list.

There is also a way to out the whole string in capitals, upper(), in lower case, lower() or removing the spaces at the beginning and ending, strip().

Another modification that you can do with a string is replacing letters with other letters, replace() or split the string in two parts, split(“,”). In the split in my example, I put a comma between the apostrophes, so it will divide the string at the point of the commas. But you can also for example put the c between the apostrophes and then the string would be separated at the c.

The last thing that I want to explain is that you can check if certain characters are present in a string. You can do that with x = “Tec” in txt, that would give the outcome x=true.

You can also easily combine two strings:
string = ‘ Tec, de, Monterrey! ‘
string2=’hallo’
string3= string+string2
print(string3)


outcome: Tec, de, Moterrey! hallo

Hopefully you enjoyed reading this blog again!

List and Tuples in Python

In this blog, I am going to give you some more information about lists and tuples. Hopefully you fully understand what both are and when to use them after this blog. So let’s get started.

Lists

The first thing that I am going to explain is a list. A list is very easy to make in Python. First you need to define your list, and that is just possible with giving it a name and then ‘=’. After that you open the list with a square bracket and you type the content of your list, divided by commas. The last step is to close the list with a square bracket. If the content of your list is just words or letters for example, you have to put it between apostrophes. If the content of your list is already defined, it is enough to only separate it with commas. See below for my example:

A list has a couple of important characteristics of whom I am going to explain a couple. The first one is that a list is always ordered. If you change the order of the elements of the list, it is a different list according to Python.

A list can also contain any kind of elements. You can use, as seen here above, letters or defined numbers, but you can even use functions and numbers, Besides that, you can make the list as long as you want and even repeat the same element.

Another thing is that you can always access individual elements from a list. Every element in a list has an index number, the place where it stands in the list. So in the example above here, b has the index 0, c has the index 1, end so on. You can call these elements individually by typing the name of your list followed by the index number in square brackets:

Thalast one that I am going to discuss here is the fact that a list is mutable. Most of the data types in Python are not mutable, but the list is one of the easiest that is. That a list is mutable means that you can always delete, add or changes individual elements in the list.
In the example below here, I first changed index number 2 in the list to 5, so d–>5 (list[2]=5). After that I deleted index number 4 of the list (del list[4]). The last thing that I did was deleting index number 2 until 4 (5,e,f) and placing in that spot 88,99,100,200 (list[2:4]=[88,99,100,200]).

For more information about characteristics of list, you can consult this site: https://realpython.com/python-lists-tuples/ .

Tuples

So okay, now over to the tuples. Tuples are actually quite identical to lists, but they differ on two things:
– To define a tuple, you need to put the content in parenthesis instead of the square brackets for a list. Attention! To call individual elements of a list you still need to use square bracktes (see below).
– Tuples are immutable in comparison to lists.

All the other characteristics are exactly the same for tuples and lists.

So when a tuple and when a list?

When to use a list is quite obvious. You need to use a list when it is mandatory to make changes or add things in the list. So for example in WSQ07, we had to ask ten different numbers from our program and after that we had to sum all those numbers. My solution was to ask ten different numbers and add them one for one in a list. This would not have been possible with a tuple, because you can’t add numbers in a tuple.

So when to use a tuple?
– If you already know in beforehand that the content of a list/tuple is not meant to change, it can be useful to use a tuple. In that way, you make sure that the content is definitely not going to change.
– The program execution is faster with tuples instead of lists, but you only mention that with really big data.
– In one of the following blogs we are going to discuss dictionaries. In dictionaries require components that are not possible to change. So in that case you can only use tuples for the content of dictionaries.

Hopefully you gained a lot of new information out of this blog and good luck with bringing it in practice!

Which repetition should you use in Python?

There exist different kind of repetition that you can use in python; you can use loops (if, for or while) and recursion, I already explained them in my previous blogs. In this blog I am going to cover the subject of when using what kind of repetition.

First the loops. You use the if loop if you want to execute something if the condition is met. So that one is quite obvious when to use. Then you have the for loop and that is a definite loop that is repeating as long as the restricted condition in the for is met. The last loop is the while loop and that is a indefinite loop that keeps running as long as the condition is met.

Than the other category is the recursion. With recursion you are all the time calling the function again in its own body. With recursion you are making more use of the function then of variables with an amount.

Example on how to use them all:

In this video you can see it as well playing if you prefer that.

Hopefully you understand the differences between loops and recursion after this blog and thanks for reading!

Recursion in Python

Till now, if I wanted to repeat things I always used if, while and for loops as explained in my previous blogs. But that is not the only way to repeat things, another solution is to use recursion while programming in Python. With recursion you are dividing the problem in smaller problems that will be solved by repeating the function in itself. So what you are actually doing is dividing the big problem in smaller problems. This site has a beautiful explanation about how the dividing works in a really easy understanding way. The site uses Santa Claus and how the present will be delivered at the houses as example. So if you don’t fully understand what recursion is please check that explanation.

Okay, let’s bring it in practice in Python now. The function needs to keep recalling and repeating himself till an certain condition is met. That is the reason why a recursive function always consists of two parts: the base case and the recursive case. The base case is the condition that needs to be met to stop recurring. It is import to have that, otherwise you get an infinitive code.

Okay now I am going to explain it further on the basis of an example. First you need to define a function, the function that I defined is called lessons_recursive and the input of the function is leftovers. My base case says that the recursion has to stop if there are no leftovers left. That becomes my if and returns of the condition is met. As long as the condition isn’t met, I want to print that there are still leftovers and I want it to display the amount of leftovers. I use an else and print(“still leftovers”, leftover). After that it is important to make a statement to decrease the leftovers. It is very important to use that because otherwise your recursion becomes indefinite. At the end you have to give input for the leftovers and call the function. In a whole you get this result:

I can understand that you are wondering now what the advantage of the recursion is relative to for example an if or else loop. They actually both have a slightly different purpose. The purpose of a loop is just to repeat things till the condition is met. The purpose of a recursion is meant to break down a big task in smaller tasks. If you want to know more about the differences, you can read my next blog about it.

Hopefully you understand some more about using recursion in your Python file. If you still don’t fully understand it, is this video where Joe James brings recursion into practic with factorials. Thanks for reading my blog!

Using for loop in Python

There are two different kind of loops in Python that you can distinguish. The first is the indefinite loop. That is the while loop, that I already explained in my previous blog. This is an indefinite loop, because it goes on till it meets the condition that is given in that loop. The other kind is the one that I am going to explain in this blog, the for loop. This is a definite loop, because you say in the condition the times that the codes needs to walk through the loop, so you fix the amount before.

Okay I am going to start with giving you one of the most easy examples:

Behind the for statement you add the times that it has to walk through the loop. You need to pay attention, because it is until the second value that you give (3), not to up to and including. So in this case you have a x with value 0, 1 and two, as you can see in the output. Underneath the for, you need to put what you want to print that times. In this case We’re on time and the value of x.

Just like you can nest the while loop, it is also possible to nest the for loop. Behind the first for you put the amount of times that it needs to walk trough the whole loop. For example:

So here it walks two times trough the whole loop with five times trough the second for and five times trough the third for.

I hope you enjoyed and understood this blog about using a for loop in Python!

Using while in Python

In this blog, I am going to explain some more about the use of while in your Python code. You can use while if the condition that you have needs to state as long as. The actual meaning of while is namely as long as. I am going to explain it with the easiest example.

The first you need is a variable with a value. In this example I see i=5. After this we can go use while if you want for example the value i to become bigger with 1 till it reaches 20. If you want to follow what happens with i, you can print i in the while loop. You can do that as follow by using a while loop:

It is also possible to put a break in you while loop. You achieve that by putting an if with the value it needs to break in your while. Like this (the value breaks at 9):

It is also possible to make a break of one value in you iteration. You do that with command continue. You need to shift i=i+1 to above the if and fore the rest, you only need to change break to continue. The iteration skips the value of i that you say after the if:

Hopefully you enjoyed this blog and learned some more about using while in your Python script.

Using nesting in Python

In my previous blog, I explained a lot about using if, elif and else in Python. In this blog I am going further on that subject and we are going to use nesting in these conditions.
Nesting is actually using conditions in conditions. We are going to use the code that we wrote in my previous blog and we are going to nest in that code, so bringing conditions in the already made conditions. You can find that full code here.

So we are going to add some conditions in the first condition (if). First we are going to erase the print(smaller than 5). Now we are going to add some conditions, we are going to split the conditions in positive, zero and negative. So first we are going to add an if conditions, because you always start with if and we say if the input is bigger than 0 (positive). After that, we are going to make an elif condition and we state if the input is equal to zero. The last one is again the else condition, here we don’t state anything anymore. Because, it is always negative if it doesn’t satisfy the other two conditions. So finally we get:

As you can see is only the first statement divided. The other ones are still on themselves. Sometimes it is easier to use nested loops if you want to make a division in one of the subcategories that you have. If you are writing a very long and complex code, it becomes also possible that it is less time consuming to calculate. Your code will namely not always take the inner loop into account if the outer loop condition, that triggers the inner loop, is not satisfied.

Hopefully you enjoyed reading this blog and see you with my next blog again!

Using conditions in Python

In this blog I am going to tell something about working with the conditions that Python has.

There are three different conditions in Python: if, else and elif. I am going to explain them all by using them in a code. For exercise 1 that we previously made to calculate what the date of the next day is if you enter the date of today. I used a lot of if, elif and else statements. Here you can see that example (it is not completely finished, because the leap year is not been processed in the code).

I am going to explain the use if, elif and else also with a basic example. First you need some input to work with in your condition. So for this example let’s say a=int(input).
To use if, you just type if and after that a space and your condition, end with a colon. Let’s say: if a<5: After you condition you need to press enter and say what you want if the condition is true, let’s say: print(‘smaller than 5’). And then you get:

As you can see, if the input is smaller than 5 (4), it prints smaller than 5, if you give as input 6, it is not printing anything.

In the next example I am going to demonstrate the use of elif. You can use elif in the case you want the code to do something else in the case it doesn’t satisfy with the if that you made. We are going to use elif in the cases the input is between or the same as 5 and 10. You say elif with a space behind it and then you need to make your condition. Ours is in this case: a>=5 and a<=10 , behind it you need to end with a colon again. After that you need to enter and type what you want you code to do. We want to print between ar the same as 5 and 10. You get as result:

You see that if the input is lower than (3), it still prints smaller than 5, if the input is the same or between 5 and 10 (5 and 8) it prints between or the same as 5 and 10 and if the input is bigger than 10 it still prints nothing. As you can see, if you want your code to check multiple conditions you can divide them by using and, then it has to satisfy with both conditions. In the same way is it also possible to use or, then it has to satisfy with only one of the conditions.

If you want your program to print something else in all the other cases, you can use else. You always put else under if and elif. So in our code we can use else if the input is bigger then 10. To use else, you just type else and you put a colon behind it. On the next line you put what you want your code to do if the input doesn’t satisfy with the if and elif conditions. An example:

You can see in the example above that it still give the same output as before we added the else, only if the input is bigger than 10 (12) it gives the output: doesn’t satisfy with the conditions above.

A couple of thinks you always need to be think of and be aware of:
– Always end your if, else or elif statement with colons.
– Make sure that you are once tabbed when you are typing what the code needs to do if it satisfies the condition. If you are typing all the way to the lift (same level as the if is staying), it is not going to work.
– Do not use an end as you have to do with some others programs.

Hopefully you learned again a lot of this blog, thanks for reading!


Using modules in Python

Modules are useful to split your program/code in different files. In that way it becomes easier to use or adapt different parts separately.
Just the definition of a module is a file that consist Python code. In this blog I am going to explain you the basic things about using modules while programming in Python. The simplest way to make a module in Python is making a .py file. Actually I already explained the use of modules in my blog about using functions in Python from textline 15. In that blog I used the module math, that is a in-built module in Python. The only way you can use that module in the Python script that you are writing is using: import math. After importing that module, you are able to use the whole content of that module. You can see the whole content with explanationof the module math at this site. See this blog from textline 15 for the exact explanation about how to use import module.

You can also import only one function or variable from a module. In that case you need to use: import variable/function from module. In the folowing example I used the variable d out of the module with the name mastery08a to import in Mastery08. In this video you can see what I typed and how it worked.

Another usefull thing to do with your module if the name is too long is renaming it. You can rename you module in the script where you are typing in at that moment by using: import modulename as shortermodulename. In the folowing example I renamed mastery08a to mas8a:

If you want information about using modules in your Python file, you can see my blog about writing them and this site consist more easy and good ordered information about this subject. Hopefully you learned a lot again!

Creating modules in Python

As I explained in the blog about using modules in Python, is it very practical to use modules in Python to edit separate parts of your code. Besides that gives it also a better overview in your script and I already explained in previous blogs how important a good overview over your code is.

I already explained in the blog about creating functions in Python how you need to create and use a module in Python. To see this explanation, watch this blog. In the video in that blog, you can see how to use the module in the current script that you are writing.

Hopefully you still liked this blog despite it was almost all repetition from previous blogs. If you need more information about how to use own made modules, what you can do with them and how to use built-in modules, you can always take a look at my previous blog about this subject.

Ontwerp een vergelijkbare site met WordPress.com
Aan de slag