My own dictionary

--Originally published at Welcome to the Heaven

The dictionaries in Python are very similar to a list. The dictionaries can be directly defined or can start in blank and over time you add some values and words or delete some.

The dictionaries write inside keys {} and the values and words with two points:

Example = {"my":12,"first":24,"dictionary":36}

So now with this variable you have already a dictionary and you can call one, two, three or any number of items that you want or change, delete or add one.

down = {“example”:picture}

screen-shot-2016-11-28-at-12-42-56-pmIn the picture you can see how I create a dictionary in white and after I add some values to it.

We can have access to specific items in the dictionary, as you can see in the second print we do it.

If you don’t want to have a KeyError in some key that you do not know if has a value you can use .get that returns None(if the key does not have it), the example of this is the last two prints.

More information? and you want my source?

If you want another different post of the same topic. Is from the blog of one friend.


([start],stop,[step])

--Originally published at Welcome to the Heaven

In this post you will learn how the function range acts. Is very common use this function in a loop when you want to the loop perform an action N number of times, also we can see it when we want to make operations with numbers or print specific values of a list/tuple.

Let’s see a picture with some examples, an after that I will explain you how it works at each case.

screen-shot-2016-11-27-at-2-35-18-am

As you can see in the title of this post ([start],stop,[step]), describes the way that act the first example. The range function always start form 0, so 0=first value. Start from 0 and the next number is going to be two numbers after.

The second one and third one are the same, but I put it just because these are the two ways to write a range, but if you want to be more professional you must use the second one.

The last one is the same as the first but with a difference in print. end=” ” makes that the function doesn’t go to the next line.

And if you want to see another example with the power of two range loops:

screen-shot-2016-11-27-at-2-37-06-am

With this little code we can do this:

screen-shot-2016-11-27-at-2-40-49-amscreen-shot-2016-11-27-at-2-40-58-am

Source.


Writing and reading

--Originally published at Welcome to the Heaven

Writing and reading comments is pretty easy, we only need create a text file, open and select the mode that we will use.

Open()

First we need to create a variable with the code: example:

text = open(file_name,mode)

Mode

Exist four kinds of modes: “r”, “w”, “a” and “r+”.

“r”->Only for reading

“w”->Writing(if you  have already a file with the same name will be erased)

“a”->Open the file to append any type of data

“r+”->Reading and Writing

Example

Writing

I am going to use “shot”
as a name, and if you see the picture I don’t have any file with this name. So if you write the code of the next picture, your program is going to create a new file with the name that you want and with the
text that you write. screen-shot-2016-11-26-at-11-29-46-pmscreen-shot-2016-11-26-at-11-34-37-pm

When you run your program in the terminal nothing is going to change, but if you search the file you will find it. Remember, if you have already a file with the same name will be erased.screen-shot-2016-11-26-at-11-39-21-pm

Reading

I am going to use the file shot for the example.screen-shot-2016-11-26-at-11-45-26-pm

If you don’t write “()” at the final of file.read, when you run the program will print you the same as the first time.

More information and the source.


blog post = “Strings”

--Originally published at Welcome to the Heaven

The strings are the most common type. Beside is pretty easy to create and to use. We can use the strings in the way that we want. Let’s see some examples of how we can do it.

screen-shot-2016-11-26-at-8-45-24-pmAs you can see, create a string is much easier than create any type and we can do a lot a things with it. In the third example we create another string with the last string and we can do “More”. For this reason the strings are awesome.

BUT, WAIT!!! I have not finished. I am going to show you 3 tables that can help you a lot in the future.

screen-shot-2016-11-26-at-8-54-11-pmscreen-shot-2016-11-26-at-8-53-43-pm

And when you want to use the string format operator “%” the have a lot of options for you:

screen-shot-2016-11-26-at-8-57-54-pmscreen-shot-2016-11-26-at-8-57-47-pm

These tables are not mine!! If you want to see more information you can make click at any table or in the next word->More information.


Tuples

--Originally published at Welcome to the Heaven

In python we use a tuple when we want to make a sequences of elements that are going to be inmutable and we normally use it when we didn’t want to make changes in the elements and make the program faster. And the way to create that is with () no with square brackets, they are for lists.

 

Examples?

screen-shot-2016-10-29-at-12-07-48-am


Lists

--Originally published at Welcome to the Heaven

A list is a data style that have different kind of elements inside it. The principal characteristic is that the lists are mutable, in other words, you can change, add, remove and have access to the elements inside it, for this reason the lists are so important for a programer.

We can create a simple list with the name of the list and square brackets[] and write the elements inside it.

You want an example and I know it, so…screen-shot-2016-10-28-at-11-19-38-pmscreen-shot-2016-10-28-at-11-19-48-pmIn the first example we use a list that has predefined values and this list help us to print all the lower numbers inside it, if they have a lower value.screen-shot-2016-10-28-at-11-25-14-pm

And in the second we create a list with the values that the user is going to give us in the input. As you can see the input is only for integer values, so the user will write the number that he wants and the program is going to give us all the numbers that can divide the number and the result will be an integer, to make it possible we use mod in the program.

screen-shot-2016-10-28-at-11-34-48-pm


Use of recursion for repetitive algorithms

--Originally published at Welcome to the Heaven

Sometimes we will need a program that give us a specific result and normally we want to write a code that makes that posible, but we finish with a large code that is hard to understand and maybe can’t run, so for this reason we need use the recursion for repetitive algorithms. This makes that the program use the a function into a function and can run a million of times, if is necessary.

Okay the most classical example of the recursion is this…screen-shot-2016-10-28-at-10-02-01-pmThis is the real power of the recursion.