While loops

--Originally published at Sierra's Blog

While loops are used to tell python  I want to do this until this happens this is how you can use it:

while expression:

statement

 

for example, I made a program that guesses the number in which you’re thinking based on questions. This program uses all the stuffs that we’ve seen till now, try to understand it, it will help you a lot because this way you’ll see the way I program and maybe you’ll start thinking in other possible ways to come up with the same result:

while.png

The functions of the while loop is to keep repeating the code until the expression becomes FALSE and since the while expression is: while the Min number isn’t equal as the Max number keep asking the user, the program will only stop until it finds the number that you were thinking.

Here’s a video tutorial from Khan Academy explaining how to use the while loops:

 


Libraries/Modules

--Originally published at Sierra's Blog

Libraries in Python will save you tons of time, they’re already done functions that you must import to your program to be able to use.

How do we use them?

there are two ways to use them, the first one is to write import and then the module that you want to use, for example, the math module can be used for typing the value of pi, obtain sin(x), cos(x), etc..

after you import a library you can call it writing the name of the module, a dot, and then, the thing inside the module that you want to use, for example:

libraries1

in this example python is gonna print 3.141592653589793

 

 

i’ll give you a link to a video tutorial made by rcasanueva:

 

and here’s a list of python’s libraries for you to check or use (Click on the image):

Sin título

 

 

 


Functions in python

--Originally published at Sierra's Blog

Functions in Python are used to avoid being repeating the same code on multiple ocassions. It saves time, it needs less effort from your part, in a nutshell, it is useful.

How can we use it? 

The syntax looks like this:

def the_name_of_the_function  (parameters):

whatever that you want the function to do

function1

As an example I made a mix with some of the things that we’ve already learned to make a really simple “game” were the user needs to make a decision:

funcitions1

we start by creating 2 functions without parameters (because we won’t need them in this example)

funcitions2

then we add the code that’s going to make the program ask the user for a decision, which car does he want to use?

funcitions3

Then we add the conditionals that’s going to tell python  what’s what he needs to do if a certain thing happen.

 

Thanks for watching


Conditional “if”

--Originally published at Sierra's Blog

The conditional if could be translated to the natural language as “if this is true then do this and if it isn’t true then do this other thing”.

I’ll teach this with examples:

Aif1

the program does this by this order:

1.- var1 is a variable, this variable is empty at the beginning of the program.

2.- because var1 is a variable that involves an user input it will hold whatever you type after the question.

3.- if your input was yes var1 will be saved ass a string that holds: “yes” so, if var1 == “yes” then python is going to type: that’s great, I can teach you. But if you typed any other word but “yes” then python will do the “else” statement which is: Then go away!

this is basically how does the if conditional works.

A larger way of this can be done by using the word elif before the else statement, this way you can make the Conditional if as large as you want to, here’s an example:

Aif2

 

 

 


Revolución

--Originally published at Sierra's Blog

Revolución
Slawomir Mrozek

En mi habitación la cama estaba aquí, el armario allá y en medio la mesa.
Hasta que esto me aburrió. Puse entonces la cama allá y el armario aquí.
Durante un tiempo me sentí animado por la novedad. Pero el aburrimiento acabó por volver.
Llegué a la conclusión de que el origen del aburrimiento era la mesa, o mejor dicho, su situación central e inmutable.
Trasladé la mesa allá y la cama en medio. El resultado fue inconformista.
La novedad volvió a animarme, y mientras duró me conformé con la incomodidad inconformista que había causado. Pues sucedió que no podía dormir con la cara vuelta a la pared, lo que siempre había sido mi posición favorita.
Pero al cabo de cierto tiempo, la novedad dejó de ser tal y no quedó más que la incomodidad. Así que puse la cama aquí y el armario en medio.
Esta vez el cambio fue radical. Ya que un armario en medio de una habitación es más que inconformista. Es vanguardista.
Pero al cabo de cierto tiempo… Ah, si no fuera por “ese cierto tiempo”. Para ser breve, el armario en medio también dejó de parecerme algo nuevo y extraordinario.
Era necesario llevar a cabo una ruptura, tomar una decisión terminante. Si dentro de unos límites determinados no es posible ningún cambio verdadero, entonces hay que traspasar dichos límites. Cuando el inconformismo no es suficiente, cuando la vanguardia es ineficaz, hay que hacer una revolución.
Decidí dormir en el armario. Cualquiera que haya intentado dormir en un armario, de pie, sabrá que semejante incomodidad no permite dormir en absoluto, por no hablar de la hinchazón de pies y de los dolores de columna.
Sí, esa era la decisión correcta. Un éxito, una victoria total. Ya que esta vez, “cierto tiempo” también

The Fifth Second Chance ~ George Anastaplo
Continue reading "Revolución"

Input/Output

--Originally published at Sierra's Blog

Input:

Input is the way you can make Python ask you something it’s how the program interacts with the user.

Input is used to accept the data from the user.

How does it work?

Variable + = + input + (“text” )

  • input()     – The code that forces Python to accept the data from the user
  • Variable   – The name you’re assigning to the input.
  • (“text”)   – The text that will make the user understands what you’re asking for.

Examples:

Inputandoutput1

And the outcome is:

Inputandoutput2

Inputandoutput3

Inputandoutput4

Output:

output is to send a emission, from python to the user, the basic way is using the print statement.

 


Variable Types

--Originally published at Sierra's Blog

Variable Types function is to hold the information that your program needs to successfully work, the data you type is stored in a memory.

What are they for? 

they are used to store numbers or characters that could be used during the program.

How do I assign a value to a variable?

To assign a variable you must use the (=) sign, and it works this way:

Variabletypes1

and the outcome is:Variabletypes2

 Data types:

There variables that you assign in python are saved in an orderly manner, so, data types works as shelves in a library.

Python has 5 standard data types: Numerical, string, list, tuple, dictionary.

Numerical Types:

numerical types are divided in four subcategories:

Int: Integer numbers.

Long: Long integers

Float: Numbers with decimals.

Complex: Complex numbers.

Note: in python 3 long and int numbers merged into just int but before python 3 Long integers numbers used to have a L at the end of the number which indicated that it wasn’t an int.

Variabletypes

String: 

Strings are gonna divide whatever you type between (”) signs into character, counting from 0 to the last character number.

[] is the operator of the string, * prints the string “x” amount of times, + is the concatenator.

so if I write str [0:10] i’m saying: Hey python! please type the first eleven characters of the string.

Variabletypes3

and the outcome is:

Variabletypes4

List:

A list contains different data types, you can access to a list stored value using the slice operator “[]” and “[:]”.

For example:

Variabletypes5

and the outcome is:

Variabletypes6

Tuples:

tuples are similar to the list, they work the same but with some differences like that the tuples values are surrounded by (), for example:

Variabletypes7

So the outcome would be:

Variabletypes7

Dictionaries:

Dictionaries consists in a key-value match, you can join the key

Variabletypes9
Variabletypes10
Continue reading "Variable Types"

#Comments in Python

--Originally published at Sierra's Blog

Comments in python are for you to explain humans what’s the function of the code you wrote.

Dear Dad...Python can’t read those messages, it will simpy ignore them. It’s a very useful tool that everyone should use, because it could save a lot of time of your co-worker by not having to try to understand what the code does.

Comments1

 

Ways to annotate a comment:

There are two ways to annotate a comment in Python and the only difference between them is the extension.

# –  is used for a single line comment, commonly used to indicate what a part of the code does, to use it you just need to type #, it’s like saying: Hey python! ignore everything that comes after the #.

Comments2.jpg

“”” – is used for a comment that needs more than one line, it works this way:

Comments3

ADVICE:

Keep your messages updated, you don’t want a comment that contradicts what does the code say, it’s confusing and could make your co-worker waste time.

Never alter the case of identifiers.

Use inline comments only when necessary, don’t use comments if it’s obvious:

x = 2                               # now x is worth 2.