NO STRINGS ATTACHED

--Originally published at Coding The Future

Photo by Everton Vila

Hey everyone! It's been so long since my last post, but trust me, there's reasons for my absence and I am back with exiting stuff.

To begin today's topic, I have another of my quick stories... Lately, my life has been overtaken by discernment confusion on what I want to do, what I must do, and what is expected of me. I don't think a lot people go through this kind of crisis as often as I do, but I think we've all been there at some point in our lives. How is this relevant? Again, it really isn't, but I wish I could just try things sometimes without any strings attached.

Oh, well. That was deep. But now let's focus on strings, yay!

What are strings?

As you may know from my previous posts on types, one of the most interesting and common types in programming languages are strings.

Strings are a literally a chain of characters of any type. So any text-based variable is a string. Strings can include numbers, but these are not treated like numbers, rather like labels. So if you declare two strings like "2" and "4" and try to multiply them, you will get an error, because you can't multiply text! Get it? String is just plain text.

Declaring strings

Declaring strings in Python could not be easier. All you need to do is declare a variable and assign it a value within quotation marks. Take a look:

firstName = "Emanuel"

Now, we can do all sorts of things with this string! We can print it, we can concatenate it (join it with another string), split it, you name it!

How can they be used?

As I just mentioned, strings can be pretty powerful. I know I said you Continue reading "NO STRINGS ATTACHED"

FORever ALONE

--Originally published at Coding The Future

Image via GIPHY

Today, I will share the very sad story of my relationship life: I am, and forever will be a forever alone. But that's ok. To be honest, I don't mind the single life. Love's just complicated for me.

But anyways, you are probably wondering why am I telling you this, which is probably useless. Well it kind of is useless, but today's topic, For Loops, got me thinking.

So, let's dive into today's topic: For Loops. Remember while loops? Well for loops are not so difference. Only difference is that this time, we know exactly how many times the loop needs to run.

Here's an example: Let's say we wanted to ask a user to enter their password, and we need the user to enter it twice. Since we know that the loop will run exactly twice, the most appropriate way to accomplish this is a for loop. Let's take a look at the following program:

password = ["pass1","pass2"]
i=0;

for counter in range(0,2):
⠀⠀password[counter] = input("Please enter your password: ")
⠀⠀i= i+1

if (password[0] == password[1]):
⠀⠀print("Both passwords match")
else:
⠀⠀print("Passwords do not match")

As you can see, the first two lines simply declare a list and an integer. The following lines is where the magic happens. As you can see, I started with the keyword for, and declared a counter variable called counter, followed by the key words in range and the range from which the loop will run. As you can see, this range is in brackets, and it says (0,2). Check the footnote to know why this means it will run twice.1 I end off with a colon.

The code bellow just asks the user for input, stores it in a list, and at the end of the loop Continue reading "FORever ALONE"

ARE YOU A LIBRARY MOUSE?

--Originally published at Coding The Future

Image by Dmitrij Paskevic

Hello everyone! Guess what? Partials are over! That means that today we're back on track... And today's topic is libraries, a.k.a. modules in Python.

Let's begin by defining modules. TutorialsPoint defines a module as a logical way to "organize your Python code. Grouping related code into a module makes the code easier to understand and use".1 In short, a module is a separate file where you define functions related to a specific action and then import it to your code. The best thing about it all is that it makes coding less confusing and functions re-usable across several programs!

Let's get started!

Creating a module

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.2 For example, if you wanted to create a module containing all the functions for Tic Tac Toe, you would create a module named tictactoe and define all the functions inside.

Using a module

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. Let's say we wanted to import the tictactoe.py module we imaginarily created above:

import tictactoe

That imports the module, but in order to use the functions inside it, you need to call the function by using the name of the module (dot) name of the function. Let's say I wanted to use the newGame function within the tictactoe module:

tictactoe.newGame()

That's it for today... I know, that was easy! Happy Mexican Independence Day! ??

@emamex98

--

FOR ALL THOSE PROCRASTINATORS

--Originally published at Coding The Future

If you are reading this right now, you were probably procrastinating, and just started studying for tomorrow's test, right?

Well, as always, I've got you covered. Here's a quick video summary of what you need to know for the first partial. I hope things are not too strange.

Good luck!

SPREADING THE WORD – An initiative to teach kids to code

--Originally published at Coding The Future

Hey everyone! I know it's been a while since I last posted, but guess what? I'm in the middle of first-term partials.

Partials in almost any university mean long hours of reviewing content and doing exercises over and over until mastering every possible math problem.

However, in the mist of exam-time chaos, ideas seem to come to me more often than usual, but obviously I cannot currently work on these genius ideas due to obvious reasons. So today, I decided to take a brief break from stuDYING and from Python to talk to you about something that really interests me: teaching kids how to code.

Coding is basically an essential ability now a days, and according to CODE.org only 1 in every 4 schools teach computer science, in the US alone! So, if we are speaking about the whole world, we are obviously talking about less than 1 in every 4, especially in developing countries. Why are kids learning biology, geography, or even math, and not programming, if they are all equally relevant?

Source: CODE.org

For this reason (and because my professor @Ken_Bauer was also talking about it in class) I have pledged to help kids in my community learn how to code.

Sometime right after exam time is over, I will be contacting my sister's elementary school to propose them a free coding course in the first week of December. I have chosen my sister's elementary for a particular reason: it's an girls-only school. It is no lie there is a lack of women in the tech industry, and that begins from a very early phase, because schools usually encourage girls to pursue other types of careers, especially in conservative countries like mine.

The awesomest part about it, is that I've got mayor companies backing me up Continue reading "SPREADING THE WORD – An initiative to teach kids to code"

I WILL LOVE YOU WHILE [condition]:

--Originally published at Coding The Future

Image via GIPHY

I know. I've gotta stop with the song references. But today's topic reminds me of Whitney Houston's "Love You". That song is so beautiful, so inspiring, but it's all FALSE! Well, kind of... Why? Because nothing lasts forever. Well, maybe true love does. But in code, the chances you'll encounter a loop that never ends are super rare.

Today we'll be talking about while loops, which helps us run a piece of code while a certain condition is met.

For example, let's say we wanted to print a countdown. Instead of writing a single line for each number, we would just go to while loops!

While loops are super simple in Python. Begin with the keyword while and then write the condition –without brackets! The following lines, which are obviously indented, is where you put the code that'll loop. Take a look:

count = 0
while (count > 0): print ('The count is:' + count) count = count - 1

As you can see, the loop will run while count is grater than 0, and will stop running once count reaches 0. The count is decreased by 1

For your condition, you can use counters like the example above, or you could use a boolean value by nesting if-statements inside of your while loop. You've gotta be careful though, because as I said, you could get stuck into an infinite loop, which could break your program.

That's pretty much it for today. Until next time!
@emamex98

WHAT IF? Exploring If-statements

--Originally published at Coding The Future

Photo by Matthew Smiths

Many times in life we are faced with crossroads and have to make a decision. In coding, things work similarly. Sometimes, actions that the program triggers depend on what the user chooses or inputs. This is where if-statements come in handy.

An if-statement triggers different actions depending on a certain condition. By using if-statements, you can tell a computer to do this if a certain condition is met, and do something else if it isn't.

In Python 3, declaring an if-statement is quite simple. All you have to do is to type the keyword if followed by a condition, and close with a colon. Here's a quick example:

if logWeight > 23: print("Your suitcase exeeds the maximum weight allowed.")

As you can see, in the first line, I start by declaring an if, and then I state the condition, and close with a colon. The following indented lines specify the code that will be triggered if the condition is met. In natural language, it reads "if the suitcase's weight is greater than 23kg, print a warning message".

Let's say, however, you wanted to trigger a different action if the condition wasn't met. For example, if the suitcase's weight was less than 23kg. Then all you need to do is add an else to your statement. Take a look:

if logWeight > 23: print("Your suitcase exeeds the maximum weight allowed.")
else: print("Thank you for your business.")

As you can see, all I did was to add new line (non-indented though) with the keyword else, and told the program that if the weight was anything but greater than 23, then print a message thanking the the customer.

But let's say, you wanted to get really specific, and have different actions for different conditions. Instead

Continue reading "WHAT IF? Exploring If-statements"

LET’S MAKE COOKIES! An intro to Functions

--Originally published at Coding The Future

Photo by Jade Wulfraat

When I think of functions in any programming languages, I think of them as the reusable tools you use when baking cookies, for example. Here's why...

Every time you make cookies, you don't buy a new bowl, a new mixer, a new oven, and a new tray, or do you? If you do, you're just... something else. However, must of us reuse the same materials. There's obviously the ingredients that are single-use only, but most of the stuff can be reused and that is a good thing, because that way there's no need to buy a new tray every time we want to bake cookies.

Translating this into programming, if we are going to be using the same code several times, there's no point in writing it several times. It only makes sense to write it once, and use it (or call it as many times as we need to.

So basically, a function is "a block of organized, reusable code that is used to perform a single, related action" 1 that improves the efficiency and speed of your code. Remember that less lines of code means also better readability.

Defining a Function

When defining a function, start by typing the keyword def (I'm guessing it's short for define), followed by a unique name for the function, and then include brackets where any input parameters will also be declared (think of input parameters as the single-use ingredients that can change depending on what you want to achieve). Close the line with a colon.

In the following lines, which are always indented, is where the magic happens. You can include a explanation what the function does in the first line in quotation marks, although this is optional. This will not affect your function, and is often referred Continue reading "LET’S MAKE COOKIES! An intro to Functions"

PYTHON SENSEI: The Zen of Python

--Originally published at Coding The Future

Well hello there, fellow Python learners! Today, the spirit of one of the great masters of the Python language has inspired me to initiate you into the Python lifestyle. Just like in any discipline or lifestyle, in the Python language, there is a set of basic principles.

This great master, who I previously mentioned is Tim Peters, who in the year 2004 CE found the way to perfection in the Python language. He wrote:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one –and preferably only one– obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

These, my friends, are the ideology on which the Python culture rests. Here's a brief explanation of each:

  • Beautiful is better than ugly: Write code that humans can easily read.
  • Explicit is better than implicit: Use boolean types and avoid implicit loops or functions.
  • Simple is better than complex: If it can be done in a simpler way, do it that way.
  • Complex is better than complicated: If something requires more complexity in order to be more efficient, then take it to that level.
  • Flat is better than Continue reading "PYTHON SENSEI: The Zen of Python"

PYTHON SENSEI: The Zen of Python

--Originally published at Coding The Future

Well hello there, fellow Python learners! Today, the spirit of one of the great masters of the Python language has inspired me to initiate you into the Python lifestyle. Just like in any discipline or lifestyle, in the Python language, there is a set of basic principles.

This great master, who I previously mentioned is Tim Peters, who in the year 2004 CE found the way to perfection in the Python language. He wrote:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one –and preferably only one– obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

These, my friends, are the ideology on which the Python culture rests. Here's a brief explanation of each:

  • Beautiful is better than ugly: Write code that humans can easily read.
  • Explicit is better than implicit: Use boolean types and avoid implicit loops or functions.
  • Simple is better than complex: If it can be done in a simpler way, do it that way.
  • Complex is better than complicated: If something requires more complexity in order to be more efficient, then take it to that level.
  • Flat is better than Continue reading "PYTHON SENSEI: The Zen of Python"