My own dictionary

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.

Writing and reading

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»

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.

Use of recursion for repetitive algorithms

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.

More info?

Sorry, Ken says that I need more posts

The last post was about the conditionals «if» and «elif», and if you are smart you can see that in the post I forget one conditional that is as important as «if» and that is the conditional «else«.

The two last make that the program make a specific instructions if the answer is the same as the statement. So why is else different?

Else is the essential part of the conditionals that not is the most important but never can lack. Else makes that if the answer that the program receives is not the same as the statements of the conditionals, the program don’t stop and can continue running with the code inside else

I know that you want an example:

screen-shot-2016-10-28-at-3-54-51-pm

«If» I had 17 years old…

Do you want to learn about the conditionals  «if» and «elif» you’re in the right place. Normally we use this kind of conditionals to give an instruction to the program what it would do if one answer is like the statement.

«if» and «elif» are the same thing, the unique differences is that «if» is above the «elif» when you’re programming, «if» is the first conditional that you write in your program and after this the next conditionals are with «elif«.

Example:screen-shot-2016-10-28-at-3-29-15-pmscreen-shot-2016-10-28-at-3-30-42-pm

if you want more examples you can see my others blog, almost in all I have examples with code and that code has conditionals, so…

WHAT ARE YOU WAITING TO SEE MY OTHER POSTS?

Loops loops loops

Okay in this post I am going to show you how it loop «while» works. I have a old post that has another example of the loop «while» if you want to see it, click in more loops or for a different example in frutilupis.

The loop «while» is a kind of loop that allows repeat all the instructions of the code that are inside it. The way to work of this loop is that when the conditional is true, all the instructions can run.

Let’s see an example:

screen-shot-2016-10-28-at-2-35-13-pm

As you can see if the answer of the input «shoot» is yes you get in in the loop and when you are inside it, the program show you a message and a question if you want to stay inside or go out so if you say «yes» all the time, the loop will never end.

screen-shot-2016-10-28-at-2-40-34-pm
As the program runs