Creation and use of dictionaries in Python

--Originally published at Py(t)hon

This is the last post of the mastery topics, so let’s give everything to it. Dictionaries, they are just another type of data just like lists and tuples. They are indexed by keys, which can be any immutable type, or tuples if they don’t contain any mutable object.

You can create them with: {}, there is also a function dict() inside you put all the elements that you want inside the dictionary.Here is an example of a dictionary:

tel = {'jack': 4098, 'sape': 4139}

Here is a cool video:

That’s all of the semester, i have completed my mastery topics #PUG#Tec#TC101#TheEnd#Dictionaries

 


Reading and writing of text files

--Originally published at Py(t)hon

Computers can also read and write, for them to do so we have to first create a .txt file and we do this by typing file = open(“file name”, “action”).

The next thing that we can do are the next ones:

  • “w” this is use to write in a .txt file
  • “r” this is use to read
  • “a” open the file for apending

Here is an example:

txt1

That’s all #Pug#ISC#Tec#NoMore#Python#TextFiles


Validated user input (ensure correct/expected data entry)

--Originally published at Py(t)hon

Most of the time we want our program to be able to interact with the user, to ask him questions and him returning input, well, it is very important to validate the input the user is giving so our program doesn’t crash. For example we have a program were the user has to input a number, how we validate that that input will be indeed a number, we can use the try…except….else block, here is how:

valid-1

That’s all #Pug#Valid#Input#ISC#Tec#TC101#NoMore

 


Creation and use of strings

--Originally published at Py(t)hon

This is very easy to learn, first to create a string what you should do is enclose all the characters between quotes like this Ex: str1= “Hello”, this is a string already.

You can play with the string and updated, change it, add more, print it completely, print it partially etc.

str1

Here is a video of strings:

That’s all #Pug#Tec#TC101#Strings#TheEnd

 


Lists & Tuples

--Originally published at Py(t)hon

It is time for us to learn about lists and tuples, this is a very simple topic. Both are sequence type data, referring to the way they behave, as a sequence. The elements that go inside the list and the tuple can be different kind, a string, a number, even another list or tuple.

The difference between them is that the list is between [ ] and can be modify and the tuple is between parenthesis () and can’t be modify.

An example:

[10, 20, 30, 40, 50]
["spam", "bungee", "swallow"]
(2, 4, 6, 8)
("two", "four", "six", "eight")
[("cheese", "queso"), ("red", "rojo"), ("school", "escuela")]

Here is a video for better understanding:

That’s all? #Pug#Lists&Tuples #ISC#TC101#Tec


Use of recursion for repetitive algorithms

--Originally published at Py(t)hon

This time we are going to learn about recursion, what is recursion? Recursion is a method where the solution to a problem is based on solving smaller instances of the same problem or in other words  is a way of programming or coding a problem, in which a function calls itself one or more times in its body.

An example is the function of the factorial:

fact-1

Click here for more examples.

Here is a video for better understanding:

#Pug#TC101#Tec#ISC#Recursion#NoMore


Loops, loops, loops …

--Originally published at Py(t)hon

Is time for us to learn about loops, the loops help us to repeat a piece of code several times instead of writing the code all over again. There are two kinds a while loop and a for loop, the while loop  tests the condition before executing the loop body, while a given condition is TRUE, it will repeat a statement or a group of statements. On the other hand, we have the for loop that executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

Here is an example of for loop:

loop-1

and for while loop:loop-2

That will be all here is a video for better understanding:

#Pug#TC101#Tec#ISC#Loops#While#For

 


Nesting of conditional statements

--Originally published at Py(t)hon

You may think there is no more you must know of the conditional statement, well… You are wrong, there is another knowledge call nesting that must master. Perhaps you didn’t know that in a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct.

There may be a situation when you want to check for another condition after a condition resolves to true, this is what is call nesting, a conditional inside another conditional.

Here is an example:

nesting-1

Here is a youtube video:

That’s all #Pug #Nesting #If #Python #ISC #Tec #TC101