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:
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.
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.
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:
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.
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
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:
Hey so… palindromes,
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!
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
http://giphy.com/gifs/dancing-fresh-prince-of-bel-air-carlton-pa37AAGzKXoek
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
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).
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.
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
Great shots and amazing synchronized dancing. Very creative.
Super cool music video depicting the Cold War.
Screen cast on how to use modules:
Hope it’s helpful!
Hey kids, been busy for awhile, but I’m back.
So, today I’m gonna talk about modules (aka libraries). Modules are files containing Python code (functions and statements). They’re pretty great since they can save you a lot of time and space.
- There’s the modules you create yourself. To do this, you simply write code and then save it with the suffix .py. Then you can import this new module into another program! (using the import function). For more details, watch https://www.youtube.com/watch?v=sKYiQLRe254
- There’s also the modules that are already part of the Python standard library, and you simply import them.
- Lastly, there’s those you install from external sources and then import. For more details, watch https://www.youtube.com/watch?v=UKXx4e9PotI
Not all that clear yet? Worry not, I will make a screen cast soon about how to use modules.
gif from https://giphy.com/gifs/friends-matt-leblanc-im-back-FrlXUwX4Gidiw