Search

Carolina's Blog Site

I use this blog to share my experiences in learning how to program, among other things.

Course review.

Hello everyone!

So I am done with my first semester at el Tec, now it’s just finals! WOW, I can’t believe it went by so quickly!!!

Anyway, one of my favorite classes this semester was fundamentals of programming TC101 with To Ken Bauer, so here’s a short review of the course:

I remember our first day, when Ken made us stand on our desks and yell ‘it’s okay to fail’ and told us all about his abolish grading policy and his flipped learning classroom style. It was all sooo new to me!!! I think this learning concept is very different from other teachers’ methods. Consequently, it has its pro’s and con’s.

Everyone learns at a different pace, however, most classes make learning homogenous and don’t encourage individuality, so people feel like they’re always ahead or always behind. This style lets you progress at your very own pace and way. It offers flexibility, and it encourages learning for the sake of learning, not for the sake of earning a grade or accomplishing a homework. Another thing I loved is that it discourages grading, which is like the ultimate individuality destroyer, because every person is different, so you cannot compare them, especially not with such a simplistic method. It makes me so mad that schools do this. It’s simple really, you just can’t compare apples and oranges. Period.

However, there’s also disadvantages to this kind of learning. Even though the freedom it gives you to learn/study can help you grow and discover what works for you, it can also let you not work at all. If you have enough discipline, you can make yourself learn, ask for help and learn from others. Ken is always willing to help and so are your classmates. However, if you don’t, you can end up leaving everything till last minute and learn pretty much nothing. You have to push and discipline yourself. This is very challenging, but it’s good practice because out in the real world you are going to face this same challenge.

So it’s really up to you how you take advantage of Ken’s class. You are the only one responsible for your learning progress, no one is going to be after you, constantly telling you what to do. Personally, I learned a lot in this class, about myself and about programming, and I loved that we helped each other learn as a group. However, at times I did feel lost and confused regarding the class material. Like I said, pros and cons. If you’re up for the challenge, I would definitely recommend this class.

Thank you so much to Ken and to all my classmates for this awesome experience. I learned so much from all of you.

 

Don’t forget the tools.

This is a list of all the tools and resources that helped me learn Python3 in Ken’s class. I hope they’re helpful!!!

great for sharing, interacting and communicating:

  • twitter
  • slack
  • github

 

Validating user input

So when you ask the user for input, you need it to give you a valid input, something you can do whatever it is you wanted to do with it. For example, let’s say you ask for the user’s age because you wanna add something to it. Well, the user might write bdjsfkfs, you never know! So you must prepare accordingly.

First of all, be sure to specify when asking the user for input. Do not assume they know what you want from them. Have the program say things like “only integers, or rounded to the nearest whole number or in military time please.”

pink sweet minions despicable me agnes
source

Now, when asking for input, it will always be given as a string, and you won’t be able to do math operations with it.

capture

One thing you must do is transform the user input into a number by using the int function. That way Python will automatically turn it into an integer and will be able to do math operations with it. Example:

age = int(input("Please enter your age: "))

Another great thing you can use in Python is the try/except statement, like this:

def valid_user_input(x):
    try:          #what you want it to do
        return int(x) > 2
    except ValueError:     #if an error happens,
        print ("please type an integer")     #do this
        return None

 

Robber’s language

Challenge: write a function translate that will translate a text into “rövarspråket” (Swedish for “robber’s language”). That is, double every consonant and place an occurrence of "o" in between.

Thanks Luis Palomino!

https://github.com/cpvp20/robberslanguage

Why global variables are evil

So, I’ve heard that programmers should always avoid global variables, and I never really understood why. I think this article explains it really well:

4.2a — Why global variables are evil

Hardcore Programmers
source

Max of 3 values

Challenge: create a program that takes three numbers as arguments and returns the largest of them.

At first I got all confused and started making unnecessary loops. It was very frustrating.

confused joey joey tribbiani matt leblanc im so confused

Then Ken reminded me that when creating a program, first you must think about it logically, forget about the sintax: what does your program need to do? how would a human do that? how would you explain it in pen and paper?

That made things a lot easier. IMPORTANT ADVICE TO REMEMBER.

Image result for sudden realization meme

So then, basically what I did was assume that the first integer given by the user was the biggest one, created a variable for it, and then I compared it with the other values and changed it if necessary.

Here’s my code, hope it’s helpful: https://github.com/cpvp20/max_of_three

 

sources:

http://giphy.com/gifs/money-help-wwyb-vsZF2hC9cH0Mo

http://knowyourmeme.com/memes/sudden-clarity-clarence

New challenges coming up…

So Ken posted this great link that has all kinds of challenges, and you should really check it out!! http://www.ling.gu.se/~lager/python_exercises.html

I’m gonna try to do all of them.

challenge

This is the one about the len function, a function that computes the length of a given list or string. I know that Python has the len() function built in, but it’s a good exercise. Here’s my code: https://github.com/cpvp20/len-function

sources:http://giphy.com/gifs/challenge-I7wD1UNqmcv6g

Bubble gum bubble bubble bubble gum.

So one of Ken challenges required arranging the numbers in a list in ascending order. To do this I used the bubble sort method, which makes multiple passes through a list; each pass through the list places the next largest value in its proper place by comparing adjacent numbers and exchanging those that are out of order. Here’s a great explanation: http://interactivepython.org/runestone/static/pythonds/SortSearch/TheBubbleSort.html

code:

The history and future of virtual reality

Madam, I’m Adam. and other palindromes.

Hey so… palindromes,

 Wat -  Wat  old lady wat

So I didn’t know this before last week, but a palindrome is a word or phrase that reads the same backward or forward, such as kayak or racecar.

Anyways, Angel and I made a program during Ken’s class that takes user input, splits it into words, and feeds back which ones are palindromes and which ones are not, and it works YAY!

dancing the fresh prince of bel air happy dance fresh prince of bel air carlton

Since there’s no built in  reverse function in Python strings, we created a function that reverses a string by slicing it. If a negative value is used for the step in slicing a list, the slice is done backwards. Using [::-1] as a slice is a common and idiomatic way to reverse a list. More about this at http://stackoverflow.com/questions/931092/reverse-a-string-in-python.

So yeah, check out our code at https://github.com/cpvp20/palindrome/blob/master/palindrome.py

sources:

http://www.quickmeme.com/meme/3u0jqj

TACOCAT

http://giphy.com/gifs/dancing-fresh-prince-of-bel-air-carlton-pa37AAGzKXoek

Word Frequency Challenge

So, the other day Ken gave us a challenge: Using a file full of words, create a program that will count the number of times each word is repeated (break the text into words and count the frequencies of them).

He told us this sorta program can be used for analyzing encrypted data. A program can count, for example, what the most repeated letter in a file is, and then that can be used to figure out what language it is written in (since most languages have a different most used letter), if that makes any sense.

Anyway, I thought that was pretty cool.

So this took me awhile. It actually covers a lot of topics we’ve seen: working with files, dictionaries, functions, if-else statements, user input, etc. Check it out! https://github.com/cpvp20/frequency_challenge/blob/master/frequency.py

Files and files and files.

Opening Files

You can use Python to read and write the contents of files. Before a file can be edited, it must be opened, using the open function.

myfile = open(“filename.txt”)

The first argument of the open function is the path to the file (however, if the file is in the same directory as the program, you can specify only its name).

The second argument is the mode used to open a file.
“r” means open in read mode, which is the default.
“w” means write mode, for rewriting the contents of a file.
“a” means append mode, for adding new content to the end of the file.

Adding “b” to a mode opens it in binary mode, which is used for non-text files (like image and sound files).

Image result for meme binary
Btw, if you don’t recognize this, you really need to watch Pulp Fiction.

Once a file has been opened and used, you should close it.
This is done with the close method of the file object.

file.close()

Reading Files

The contents of a file that has been opened in text mode can be read using the read method. Here are some other methods:

>>> f.read()
'This is the entire file.\n'
>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
Or you can create a for loop:
>>> for line in f:
...     print(line, end='')

Other stuff

To write to files you use the write method, which writes a string to the file.

You should know, when a file is opened in write mode, the file’s existing content is deleted.

Image result for what why meme

Another way of working with files is using with statements. This creates a temporary variable (often called f), which is only accessible in the indented block of the with statement.with open(“filename.txt”) as f:

print(f.read())
The file is automatically closed at the end of the with statement, even if exceptions occur within it; this can save you a lot of problems.

Hope this was helpful!!

sources:

https://docs.python.org/3/tutorial/inputoutput.html

https://www.memecenter.com/fun/2363511/i-amp-039-m-losing-my-voice/comments

Blog at WordPress.com.

Up ↑