Creation and use of dictionaries in Python

--Originally published at Start in the world of the #TC101

A Dictionary  is a way to store data just like a list, but instead of using only numbers to get the data, you can use almost anything. This lets you treat a dictionary like it’s a database for storing and organizing data any kind of data. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Example:

states = {
“Oregon”: “OR”,
“Florida”: “FL”,
“California”: “CA”,
“New York”: “NY”,
“Michigan”: “MI”
}
print (“Michigan’s abbreviation is: “), print (states[“Michigan”])
print (“Florida’s abbreviation is: “), print (states[“Florida”])
print (“Oregon’s abbreviation is: “), print (states [“Oregon”])
print (“California’s abbreviation is: “), print (states [“California”])
print (“New York’s abbreviation is: “), print (states [“New York”])

↓  ↓  ↓  ↓  ↓  ↓  ↓

Karina-Ramirez:desktop Karina$ python3 dictionary.py

Michigan’s abbreviation is: MI

Florida’s abbreviation is: FL

Oregon’s abbreviation is: OR

California’s abbreviation is: CA

New York’s abbreviation is: NY


Creating functions

--Originally published at Start in the world of the #TC101

Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.

To create a function you have to start with def follow by parenthesis (), inside this parenthesis goes the parameters necessary, if needed of course, after the parenthesis goes a colon (:) and then to close it, use the return function follow by the value statement, if you don’t want it to return anything, just leave a blank space, the system will take it like None.

Reference: http://www.learnpython.org/en/Functions

 


Importing and using modules/libraries

--Originally published at Start in the world of the #TC101

In Tutorialspoint tell us about A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.

Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.

Creating a module is easy. All you have to do is create a new Python file and define the functions that will belong to that module, and once you wanted to use the library you just created or downloaded from the internet, the first step would be to import it. In the file where you will use it, type the word import followed by the name of the module.

 


Importing and using modules/libraries

--Originally published at Start in the world of the #TC101

In Tutorialspoint tell us about A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.

Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.

Creating a module is easy. All you have to do is create a new Python file and define the functions that will belong to that module, and once you wanted to use the library you just created or downloaded from the internet, the first step would be to import it. In the file where you will use it, type the word import followed by the name of the module.

 


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

 


Use of recursion for repetitive algorithms

--Originally published at Start in the world of the #TC101

Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition fulfils the condition of recursion, we call this function a recursive function.

Termination condition:
A recursive function has to terminate to be used in a program. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not met in the calls.

Example:

 

 

Source: http://www.python-course.eu/recursive_functions.php


When to use what type of repetition in a program

--Originally published at Start in the world of the #TC101

To keep a computer doing useful work we need repetition, looping back over the same block of code again and again.

  • For Loop

The for loop that is used to iterate over elements of a sequence, it is often used when you have a piece of code which you want to repeat “n” number of time

  • While Loop

The while loop tells the computer to do something as long as the condition is met it’s construct consists of a block of code and a condition.

  • Nested Loops

In some script you may want to use nested loops. A nested loop is a loop inside a loop.

  • Break

To break out from a loop, you can use the keyword “break”.

  • Continue

The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.

Source: http://www.pythonforbeginners.com/loops/for-while-and-nested-loops-in-python (You can find here some examples)


Creation and use of Lists/Tuples

--Originally published at Start in the world of the #TC101

Two of the most commonly used built-in data types in Python are the list and the tuple.

Lists and tuples are part of the group of sequence data types—in other words, lists and tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type, including the nothing type defined by the None keyword.

The big difference between lists and tuples is that lists are mutable, however tuples are immutable. Meaning once tuples are created, objects can’t be added or removed, and the order cannot be changed. However, certain objects in tuples can be changed, as we’ll see later.

Creating a Python List or Tuple

Creating a list or tuple is easy, here are some empty ones:

 

*To create non-empty lists or tuples, values are separated by comma

*To create a tuple with only one value, add a trailing comma to the value.

Example:

 

Source: http://pythoncentral.io/python-lists-and-tuples/