Ranges

--Originally published at Just A Turtle Coding.

Ranges are used a lot in for loops, but understanding them is key to knowing how to program.

A range basically tells Python from which number to which number it should read.

As long as the variable is a number, you can use it in a range.

for x in range (8)
print(x)

it would print 0,1,2,3,4,5,6,7,8 since Python starts from 0.

You could use another notation to tell Python how to read the range.

for x in range (A,B,C)

where A is the start number, B is the end number, and C is the period in which Python should read it. If you want a number to go negatively, you have to use C value as -1. It should look like this: 
for x in range (0,-10,-2)
print(x)


0,-2,-4,-6,-8,-10


Here’s more info about the topic: 

https://www.youtube.com/watch?v=fhCAt0s27no

Recursive functions

--Originally published at Just A Turtle Coding.

So a recursion function is when a function is called inside the same function. Using them can reduce your code from 100 lines to maybe a 20-line code. 

Something that’s recursive in its own definition is a factorial function. When using recursion functions, it’s really easy to program a factorial. 

You can learn more from this topic in: http://stackoverflow.com/questions/4502429/recursive-factorial-function

How to read love.txt

--Originally published at Just A Turtle Coding.

To use saved variables you have to use an open function.

In: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python you could have a better explanation of how to to this, since it’s a pretty complicated topic.

Files can be either binary or text and the way to open them is: file_object = open(filename, mode) where file_object is the variable to put the file object.

Love lasts forever, variables don’t

--Originally published at Just A Turtle Coding.

Owner taught me that variables can be like love… Love can either last for one moment, or forever… and so can variables, but for variables to last over time you have to save them.

How to save variables you might be wondering:

file = open(“newfile.txt”, “w”)
file.write(“hello world in the new file ”) 
file.write(“and another line ”) 
file.close()

These files could use bytes or even gygabytes. There’s lots of ways for reading them tho… but that’s for another post.

Validating entries :D

--Originally published at Just A Turtle Coding.

When validating user entries, you have to know what you’re looking for. When a function uses a specific type of variables, it might crash if the user inputs something that the function can’t use. To apply this, you can call a try function.

For more info, you can consult: http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response 

while True:
    try:
        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #better try again... Return to the start of the loop
        continue
    else:
        #age was successfully parsed!
        #we're ready to exit the loop.
        break
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

Ranges

--Originally published at Just A Turtle Coding.

Ranges are used a lot in for loops, but understanding them is key to knowing how to program.

A range basically tells Python from which number to which number it should read.

As long as the variable is a number, you can use it in a range.

for x in range (8)
print(x)

it would print 0,1,2,3,4,5,6,7,8 since Python starts from 0.

You could use another notation to tell Python how to read the range.

for x in range (A,B,C)

where A is the start number, B is the end number, and C is the period in which Python should read it. If you want a number to go negatively, you have to use C value as -1. It should look like this: 
for x in range (0,-10,-2)
print(x)


0,-2,-4,-6,-8,-10


Here’s more info about the topic: 

https://www.youtube.com/watch?v=fhCAt0s27no

Recursive functions

--Originally published at Just A Turtle Coding.

So a recursion function is when a function is called inside the same function. Using them can reduce your code from 100 lines to maybe a 20-line code. 

Something that’s recursive in its own definition is a factorial function. When using recursion functions, it’s really easy to program a factorial. 

You can learn more from this topic in: http://stackoverflow.com/questions/4502429/recursive-factorial-function

How to read love.txt

--Originally published at Just A Turtle Coding.

To use saved variables you have to use an open function.

In: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python you could have a better explanation of how to to this, since it’s a pretty complicated topic.

Files can be either binary or text and the way to open them is: file_object = open(filename, mode) where file_object is the variable to put the file object.

Love lasts forever, variables don’t

--Originally published at Just A Turtle Coding.

Owner taught me that variables can be like love… Love can either last for one moment, or forever… and so can variables, but for variables to last over time you have to save them.

How to save variables you might be wondering:

file = open(“newfile.txt”, “w”)
file.write(“hello world in the new file ”) 
file.write(“and another line ”) 
file.close()

These files could use bytes or even gygabytes. There’s lots of ways for reading them tho… but that’s for another post.