While…

--Originally published at PYTHON 3

The whiles are useful when we need to make a loop in our code, by this I mean that after we are done with a set of instructions maybe we want to repeat the same instructions one more time, and another and another and another….., until a statement tells it to stop or even the user wants to make it stop.

The while differentiates from the for because we use when we don’t know how many times the code will execute, with the for we tell the program how many times to repeat the loop.

screenshot-from-2016-09-09-09-10-58

Here I am telling the program to generate a random number from 0 to 10, I am also creating an int variable x =1,

so if my random number is less or equal than 20, my random number will be added the x=1, and after that I am printing all the numbers till it gets equal to 20.

This is a while loop because I don’t know my initial number, maybe my initial random is 0 or 10, and one may need more loops to reach 20 than the another, that’s why I use a while, because I don’t know how many times to repeat the instructions until it reach the goal.

screenshot-from-2016-09-09-09-21-37

screenshot-from-2016-09-09-09-21-53

I learned while loops from here…? Check it out

http://www.python-course.eu/python3_loops.php

 

 


What if…? or elif…? or else?

--Originally published at PYTHON 3

The if help us to check conditions, and we usually use them with inputs.

 

For example…

age = int(input(“enter your age: “ ))

I am making a variable called age which is an int/input.

 

if age >= 18:

(Instructions)

elif age < 18:  

        (Instructions)

else:

       (“Error, what are you typing?”)

 

After typing your age, the code is telling if that number is equal or greater than 18 it will run a set of instructions, any instructions the coder wants.

But if that condition doesn’t complete, we have our elif, which is our plan B (that will also run another set of instructions)

But if any of those statements aren’t true then we can use or else, which is like our plan C/ backup plan (which runs another set of instructions if all of the ones before aren’t true)

9ba2dc39-0aa4-40c5-bfbb-a84af7825364.gif

It’s that easy, yet so beautiful and amazing.

 

Here’s a python code I made using ifs, elifs, and else

Screenshot from 2016-09-05 21-59-02

Screenshot from 2016-09-05 21-59-31

Screenshot from 2016-09-05 21-58-00

I tried like 30 times and never got the right number, that’s bad luck.

Check out the page that helped me learn about ifs?

http://www.ibiblio.org/g2swap/byteofpython/read/if-statement.html

 

 


Import Modules

--Originally published at PYTHON 3

What is a module?

A module is an object that lets you organize your Python code.

A file consisting of python code, this can define functions classes and also variables, and it also can include runnable code.

Any python file “something.py” can be use as a module by executing the next code in another or brand new python file.

Import module1[, module2[, …..Module(n)]

In other words, a module lets us import directions/ functions / defined functions from another python file, and instead of writing the same commands on our new Python file, we just need to put our directions for example:

In the last blog I learned how to create and call functions, so in my file “creatingandcallingfunctions.py” I already have myinstruction /function“repeat()”

which prints a lot of lines with numbers, so in my new file “import.py” I just need to import.

Import creatingandcallingfuncitons

##It is very important that the files you are going to import are in the same directory as your new file, because that’s the way it works.

But the problem in this is that you may also import other modules which have a different function but with the same name, so you have to define from what file you are going to import that function

import creatingandcallingfuntions

creatingandcallingfunctions.repeat()

creatingandcallingfuncitons.repeat()

Screenshot from 2016-09-01 10-10-27

In my file “creatingandcallingfuntions.py” I already had instructions, which were printing 2 sets of numbers, and in this new file i[m importing this instructions and also giving the directions to import another 2 sets of numbers, so in total there should 4 sets of numbers.

Screenshot from 2016-09-01 10-11-53

 

Here is the link were I learned about modules:

http://www.tutorialspoint.com/python/python_modules.htm


Functions, not = to f(x)

--Originally published at PYTHON 3

Before creating and calling functions in Python 3, first we have to know what is a function in Python 3? So….

What is a function in Python 3?

A function is a code, (reusable code) that let us make a single related action. For example, the most common function I may think of is “print()” which is also an output, but Python let us create our very own functions/commands/methods.

So now we now what a function is, but now, how do we create a function?

To create a function first of all your are going to NEED the KEYWORD “def” (an abbreviation of the word define) which is indispensable to create a function, ’cause like the name says it you are going to define it, the next thing you need is the name of your function, so name it anyway you like, but I recommend its a name that reminds you what that function does or at at least is supposed to do lol. And at the end you add parentheses “()”, and inside this parentheses there should be the parameters/arguments of that function.

Here’s a more graphical example of how to create a function:

“def” + “nameofthefunction” + “(parameters/arguments)”

Screenshot from 2016-08-29 11-04-41

That is my creatingandcallingfunctions.py file and when I run it on the terminal it is suppose to print 14 lines of series of numbers.

Screenshot from 2016-08-29 11-08-25

and we are done?