Buscar

Fernando's blog

Programming class

Categoría

Masteries

Open and read files

This was a very challegnig thing i’ve got to get through in  the semester.

To learn how to open and read files i had to read a lot of times the same thing of the «How to think like a computer scientist»

There are special words to do this how ever the esscentials does not need you to import any module which is good for us.

First of all we have to tell python we want to open a file, to do this we must type set a variable and set the open to it like this:

variable = open(«name of the file.txt», «r»)

It is very important that we put our paraeters in this case within quot marks, as you see up you have to type an ‘r’ after the name of the text which indicates that is reading.

Now we have it open we have to read it.

To read the text we can choose between read line by line orto read all of them.

1 line at a time: variable = previous_variable.readline()

all of them: variable = previous_variable.readlines()

From here you just haveto get creative and do the stuff you want. And just dont remember that always at the end you must close your text with:

variable.close

An example:

archivo = open(‘bananas.txt’, ‘r’)

one = archivo.readline()

print (one)

close.archivo

 

Hope you enjoy it!

 

 

 

Sent work via Github

In the following video i show you how to use the basics of Github and how you can send your work so other people can see your code.

Show you i installed linux

The following video will show youi have installed linux in my personal computer:

Recursion for repetitive algorithms

Sometimes we will be coding a fucntion that need to be more than one time. So we will be facing a problem since mayve we would be able to solve our problem by making our algorithm longer but at the end thats not how we should learn to program. We should learn how to be efficient and fast, so we must learn what recursion means which is just what we need in cases like this.

A recursion allow a function to be written inside itself so it can run more than one time. The function would be running itself.

A lot of information about recursion states that knowing how to create a program that gets you the factorial number is an awesome begining to understand recursion.

The code would be like this.

def factorial(a):

if a == 1:

return 1

return (a*factorial(a-1))

We can see how at the last line the programm compute the function inside the function. The programm would do something like

5! = 4 * 3 * 2 * 1

and this will make it faster and more efficient

Nesting conditionals

So far we’ve seen how to use loops, while and for loops. And we’ve learned that they are very useful in almost any program we want to do. But there will be a time when our programms become complex when we will want to evualte a condition if another turn out to be true, an example:

if variable < 200:

if variable == 150:

do something

elif variable == 100:

do something

else:

do something

else variable > 200:

do something

 

We can see how in here we evalate if our variable was in range below 200 and then if it had exactly the value of 150 and then of 100, if non of there were true we also state something.

This is what nested conditionals work for.

 

 

 

 

 

 

How to create and how to call modules

When you are writing a code and you make some variables or functions, you can’t use them once you get out of the file. In order to fix this, python3 gives us the option to use modules. Modules are very useful, for example, when we’re make an extremely big program we tend to separate the code in more than 1 files so its easier to debug or to improve later. So maybe we have a piece of code that includes variables and functions we wan’t to use in other pieces of code and it’ll have a lot of disadvantajes to copy and paste in each file the same code. So this is where modules come, we set a piece of code as a module and we call it in every programm so we dont have to copy-paste everything.

Lets take the math module as an example. When we want to get a square root of a number we have to call the module first, and we call it like this:

import math

once we’ve done that we can call the function:

sqrt.math()

As i said at the beginning we can create modules too. To create a module we just mae a python file, and to call the functions inside of it we just type at the beginning of the file:

import xxxxx

We can see how when we import modules we created must not put the last ‘.py’ because it wont run.

This is how you call and create modules.

Lists

In the next video i’ll explain what are lists and how to use them in python3

Demonstrate use of linux for quizzes/exams

The next video shows that i have sufficient knowledge of linux in order to complete exams in class.https://www.youtube.com/watch?v=eYTDZgD9MDM

How to use strings

The next video explains deeply whats a string and how to use them.

Blog de WordPress.com.

Subir ↑