Struggling with Python?

--Originally published at Programming

Hey! Before I continue making posts, I need to talk about something important. I know some of you are not new to programming and you might find this course relatively easy. If that’s the case then this post is not for you, please go away. For those of you who are struggling with Python I’d like to encourage you to visit this Introduction to Computer Science course taught by the MIT (Massachusetts Institute of Technology).

https://prod-edx-mktg-edit.edx.org/course/introduction-computer-science-mitx-6-00-1x-8

1.png

 


Dictionaries in Python

--Originally published at Programming

Today we’ll be talking about the Dictionaries in Python. A Dictionary and a List share a lot of things in common, the only difference is that you can get the data out of a Dictionary using almost anything, on the other hand, when using a list, the only way to get the data was by using the value’s position. In other words, a Dictionary is like a database for storing and organizing data.

A Dictionary let’s you use anything, not just numbers, to get the elements of the Dictionary and to modify them. Basically a Dictionary associates one thing to another. Let’s see how a Dictionary is created by associating values to strings.1.png

2.png

As you can see, unlike the list, here we are using strings to get the values we want from the dictionaryExample. But keep in mind that you are allowed to use also numbers just like in a list. But what if we want to delete elements from the dictionary? You need to use the del keyword like this:

3.png

4.png


List/Tuples

--Originally published at Programming

Introduction

Don’t worry. This topic is not going to be as hard and abstract as the last one, today we are going to go back to something simple. We are going to learn about variables! But not like the normal kind of variables… Let’s get started. A variable is a reserved space of memory that stores information, which can be changed at any time. If you want to store a long list of information that doesn’t change over time or a long list of information that changes over time. How can you do it? Well here are the answers to your problems:

Lists

Just like its name, it contains a list of elements. Each one of the elements is numbered (has an index), starting from zero. You are allowed to remove items and add values to the list by using their position index on the list. Basically lists are one of the most important concepts in Python that you’ll be using a lot. Think of it as a variable on steroids.

1.png

Tuples

Tuples are just like lists, the only difference is that you can’t change their values. The values that you give the tuple the first time are the values the tuple is going to have for the rest of the program’s life. Just like the list, each value is numbered starting from 0. Unlike lists, the tuples must have parentheses surrounding their values instead of brackets. 2.png

*Note: The list examples were taken from a YouTube video https://youtu.be/1yUn-ydsgKk?list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_ You should check out his channel.


Recursion

--Originally published at Programming

Definition

It’s time to start taking on more advanced topics. Recursion is a way to code a problem, in which a function calls itself several times withing its body. In mathematical terms a recursive function is a function that is defined in terms of itself. If a function fulfills the definition of recursion, it is then called a recursive function.

Terminate a recursive function

A recursive function needs to be terminated in a program. A recursive function is terminated when the solution of the problem is reduced and reaches a base case. A base case is defined as the case when the problem doesn’t require further recursion in order to be solved.

Example

The factorial of a number would be a recursive problem. A simple way to make a program to get the factorial of a number n is on the left, and on the right we can find the same problem using recursion.

2.png1.png

 

 

 

Let’s track how the recursive function works by adding print functions to the previous function definition.

3.png

4.png

If you want to learn deeper about this topic I recommend you to read the following article that explains how to use recursion. http://openbookproject.net/thinkcs/python/english3e/recursion.html

 


For loops

--Originally published at Programming

Iterate over a sequence of items

Unlike other programming languages like C or java, the for loop doesn’t always iterate over an arithmetic progression of numbers. What Python does in for statements is that it iterates over the items of a sequence (a list or a String). Let’s take a look at an example.

1.png

We have a list that contains 4 different strings so in order to print all of them it is necessary to create a for loop, where the variable i is going to iterate over all the elements inside the list list. So the first time the loop runs, the variable i is going to be the first element of the list and is going to print the string “ham”. The second time the loop runs it will print the string “Cheese” and so forth until it reaches the last element of the list.

Iterate over a sequence of numbers

When you need to iterate over a sequence of numbers, you can use the built-in function range(). The parameter inside range() is the number of times you will be iterating. If you type range(10) the variable will iterate from 0 to 9. If you type range (1,10) it’ll iterate from 1 to 9 and if you type range(0,11,2) the variable will iterate from 0 to 10 with steps of 2.

2.png

3.png


While Loops

--Originally published at Programming

Simple While Loop

Today we are going to talk about while loops. A while loop is a special kind of loop that keeps looping until the given condition equals false. The condition may be an expression. As long as the condition is true, the program will execute the statements within the while loop. When the condition is false, the program passes to the line immediately after the while loop. All statements within the while loop must be indented.

1.png

2.png

Else Statement with While

In Python, you can use the else-statement with the while loop. It is executed when the condition of the loop becomes False.

4.png

4.png


Conditional statements

--Originally published at Programming

If-Statement

It’s time for us to talk about the conditional statements. First we are going to talk about the if statement. the if statement is a way to make your computer program make a very simple decision. An if-statement is followed by an indented block of code that is run if and only if the expression is true. Say we want to make a program that asks for a person’s age and if his/her age is equal or greater than 18 we print a message to the screen.

2.png

Expressions can be tested in several different ways. Here are some of the most common ones:

1.png

*Note: When comparing two strings or numbers you can use the keyword is to check if they are the same.

Else-Statement

If the expression within the if statement is false, then the program will look for an else statement, which is the statement that will be run whenever the if statement is false. Working with the same example, we want to tell the people whose age is not greater or equal than 18 that they are too young.

4.png

Elif-Statements

The elif statement allows you to check multiple expressions and as soon as the condition evaluates to true, the program will execute a block of code. The elif statement is optional and can be written several times after an if statement. Let’s make an example where the user enters his Grade and the computer prints it using the American Grading System.

5.png

Nested Conditional Statements

Sometimes in a program, you are going to need to check another conditional after the previous condition is resolved to True. When that happens, you are going to need to use nested if statements. Let’s take a look at an example. We are going to make a program that prints to the console the bigger

1.png
2.png
Continue reading "Conditional statements"

Importing, using and creating libraries in Python

--Originally published at Programming

Importing and using Module/Library

A module (also called library) is a file containing Python definitions and statements. Python has many modules we can import. For example, when we need to use the trigonometrical sin() or cos() functions or we need to use the constant number pi we can import the module math using the import statement to use that module’s functions. So if we want to import the math module we would have to do something like this.

1.png

Inside the module math there are many different functions that can be used, whenever you want to use one of them you just need to type the library followed by a dot and the name of the function you want to use. See the example below where we print to the console the result of sin(3)

*Notice that all trigonometrical functions take only radians as parameters and NOT degrees

1.png

Creating your own libraries

Now that you know how to import libraries in Python it’s time for us to take the next step and start making our own libraries, which will contain functions created by ourselves. Let’s say you are working on a project that requires several functions to get the area of some geometrical figures. Either you can create those functions within the file you are working on or you can have a separate file containing the functions. Let’s take a look at how it’s done. On the upper left side of your eclipse screen you have your modules (.py files), folders and packages as shown in the picture below.

2.png

Imagine that you are working with the Exercices2.py module (Sorry for the bad spelling). Now you are going to create another module within the folder FirstPartial. For the purpose of this example, I’m going to name the module mathAreas.py

3.png

And now I’m

5.png
6.png
Continue reading "Importing, using and creating libraries in Python"

Creating and Calling Functions

--Originally published at Programming

Introduction to functions

In simple words, a function  is a block of reusable code to perform a single action that can be called whenever the program requires it. Python already has many built-in functions ready for you to use, such as print() input() etc… But you can also create your own functions. When you know your program is going to use certain lines of code several times the best thing to do is to create a function and then call it when needed.

Declaring a function

To declare a function you need to begin with the keyword def followed by the function name of your election and parentheses (). Should your function need parameters, they should be placed between these parentheses. A colon (:) is required at the end of the parentheses. Every line of code inside the if statement must be indented.If you want your function to return a value, you must add the return statement followed by the return value at the end of your function. Now let’s take a look at a simple example.

1.png

Calling a function

Once your function is declared, you can execute your function by calling it from another function or from the Python prompt. Here is what you need to do in order to call it. 3.png

The result of running the program will print to the console the number 3

Example

Now that we know the basics of declaring a function and calling it, it is time to make a simple program that can show you when it may be necessary to implement functions in your programs.

Let’s say we want to make a program that calculates the area of a circle. Considering that the formula for Area is A =  πr2  let’s make a program first without using functions. The program

2.png
4.png
5.png
Continue reading "Creating and Calling Functions"

Basic Output and Input

--Originally published at Programming

Basic Output

Today we are going to start printing text to the console. I’m going to start by showing you a simple Hello World program.

3.png

Basically we used the Python built-in function “print()” which takes a parameter that will be displayed to the console. Another example is the next one:

4.png

Unlike the first example, here we didn’t give the function a string as a parameter. Instead we provided a multiplication between integers that will be displayed by the console as the number 4.

5.png

User Input

What if we want the user to give us his/her age? First of all we need a variable to store what the user is going to provide. If we are going to ask the user for his age, we first need to know what the type of variable we are going to use. Since a person’s age must be an integer, the type of variable we are going to use will be an integer. Therefore:

1.png

If we run this program the console is going to look like this:

2.png

Great! Now you are ready to start playing with input and output.