Unfortunately this weeks post will a bit like Peoria's and 10k's, in which I pretty much didn't have the time to come up with any new developments in my journey to learn python. I have commitments and a few other class projects I need to address, so 20-time had to be put on the back burner a little bit. Rest assured, there will be new content next week.
One of the big problems of working on this 20-time project is ironically a matter of time. While I do have about 20% of class time to dedicate to my project, it only equates to a measly 48 minutes a week, which is minuscule compared to the hours I normally need to dedicate to this project in order to properly learn important concepts and ultimately write about them. This process spans over at least 2 days, usually Thursday and Friday due to commitments and work that occupies most of the week, with a required weekly blog post published by 12:00 PM on Fridays. This means that when I would post new content, the subject matter is very new to not only you, but myself. I would rather post when I felt that my content was correct and error-free, but that's completely out of my hands. This is the reason why Peoria, 10k, and this post exists.
Anyway's, in some of my spare time, I did manage to mess around with how I learn python by using a site called codecademy, a free website that can teach you how to program in multiple languages, including python. For the most part, codecademy is a good way to rapidly learn python, coupling an interactive hands on experience with dedicated literature to the subject, rather than the hard way of learning through a book, which is what I have been doing. Learning through the book is time consuming and can really only be done through dedicated sessions, as I have to be tied to a computer that can run IDLE, but learning through an online source allows for me to learn anywhere as long as I have a WiFi connection and a device with a usable keyboard. I do have some concerns with it though, especially with learning at a fast pace, which I've learned in the past isn't always a good idea, especially with a topic like programming. You need to understand everything your taking in, as they will become very relevant once you begin to use those skills. I've also had problems with some of the lessons being a bit cryptic, but with some tinkering and exploring their forums, you can eventually get through it. All in all, codecademy is great for reviewing or learning new things. Here is a link if you want to take a look for yourself.
This week didn't bring much to the table. I've so far enjoyed writing this blog, to the point that I may continue doing it towards or after the end of 20-time, so there is a possibility I may talk about different topics and avoid these types of useless posts in future. See you guys next week.
Friday, April 28, 2017
Friday, April 21, 2017
If Then, Else?
Last week I said that mathematical operations were the bread and butter of programming, but now, logical operators have become the jelly that's slapped onto it.
This week, as you could probably guess, I started to learn about logic operators! Logic operators are essentially the if-then or if-else statements commonly seen in the majority of programming languages, and are used to evaluate whether a set of conditions are true within a program. These are significant as it is literally used in every program imaginable, and are the basis for modern computers. They're based on the principals of Boolean Algebra, which is a version of algebra were the values of variables are treated as either the true and/or false. Now this was the jumping off point for my knowledge of Ti-Basic and Applesoft BASIC, so please note that some of this information is iffy as I've never had much experience with them.
Now in python an If-Then statement would look something like this;
potato = input("potato")
if potato == "tomato":
print("potato tomato")
In this statement, if the variable (in if then statements this variable is known as a Sentry Variable) potato is equal to the value tomato, the computer will print the argument "potato tomato". If potato doesn't equate to "tomato", the computer will ignore the statement. If we wanted to, say, have the if-then statement do something if potato isn't "tomato", we can use the else clause:
potato = input("potato")
if potato == "tomato":
print("potato tomato")
else:
print("we need a certain red vegetable")
Now if potato doesn't equate to "tomato", the computer would print "we need a certain red vegetable". If we wanted to have the computer perform actions when potato equates to other values, we can use the elif (else-if) clause:
potato = input("potato")
if potato == "tomato":
print("potato tomato")
elif potato == "tornado":
print("potato tornado")
else:
print("we need a certain red vegetable or storm-spout thing")
With elif, if potato equates to tornado, the computer will print "potato tornado". If potato doesn't exactly equate to "tomato" or "tornado", the else clause will act as a catch-all for any other values.
If-then statements are great for user input for specific variables, like passwords. The only problem is if we mess up by entering "tamato" rather than "tomato", we can't re-input any new values for potato. The solution is to put the program into a loop, in this case a while loop. A while loop is essentially an if-then/else/else-if statement with if replaced by while, causing the program to enter a loop until certain conditions are met. If we wanted a program that will loop until "tomato" is entered by adding the while loop, it would look exactly like this:
potato = " "
while potato != "tomato":
potato = input("we need a certain red vegetable")
print("potato tomato")
The program will now loop until potato is tomato. If potato doesn't equal to tomato, the program will ask the user to enter a different value until tomato is specified. A problem with while loops can occur when the program has a slight flaw in it:
potato = " "
while potato != "tomato":
potato = print("we need a certain red vegetable")
print("potato tomato")
Output:
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
Did you see the problem? In this case, I accidentally typed a print function rather than a input function, inadvertently causing an infinite loop, printing "we need a certain red vegetable" over and over forever until we manually end it.
We can change how the condition of the sentry variable by using different comparison operators. These are the equal to (=), not equal to (≠), greater than (>), less than (<), greater than or equal to (⋝), and finally the less than or equal to (⋜) symbols that everyone fussed over in elementary school, except they are now are expressed as ==, !=, >, <, >=, <= in python respectively.
Among the logic operators, I also learned about how to generate random numbers using the random module (modules, by the way, is essentially code represented by a certain word). By using this module, we gain the randint() and randrange() functions. The randint() function produces random numbers that are between two specified values (so randint(5,10) would produce a number between 5 and 10), while the randrange() function will produce integers from a specified group of numbers, similar to randint(), but will generate any number between the specified number and 1 (so randrange(12) would generate any number between 1 and 12).
The odd thing about modules though, unlike functions, is that they need to be imported in order to be used. They aren't present by default, so they have to be loaded in using an import statement. The functions in modules also need to be called by using Dot notation, which is a fancy way of saying that you need to type the module's name and the functions name with a period in between.
*Updated. Included more information on logic operators, random integer generators and modules. Also fixed grammatical errors.
bibliography:
Python Programming for the absolute beginner, Third Edition by Michael Dawson
This week, as you could probably guess, I started to learn about logic operators! Logic operators are essentially the if-then or if-else statements commonly seen in the majority of programming languages, and are used to evaluate whether a set of conditions are true within a program. These are significant as it is literally used in every program imaginable, and are the basis for modern computers. They're based on the principals of Boolean Algebra, which is a version of algebra were the values of variables are treated as either the true and/or false. Now this was the jumping off point for my knowledge of Ti-Basic and Applesoft BASIC, so please note that some of this information is iffy as I've never had much experience with them.
Now in python an If-Then statement would look something like this;
potato = input("potato")
if potato == "tomato":
print("potato tomato")
In this statement, if the variable (in if then statements this variable is known as a Sentry Variable) potato is equal to the value tomato, the computer will print the argument "potato tomato". If potato doesn't equate to "tomato", the computer will ignore the statement. If we wanted to, say, have the if-then statement do something if potato isn't "tomato", we can use the else clause:
potato = input("potato")
if potato == "tomato":
print("potato tomato")
else:
print("we need a certain red vegetable")
Now if potato doesn't equate to "tomato", the computer would print "we need a certain red vegetable". If we wanted to have the computer perform actions when potato equates to other values, we can use the elif (else-if) clause:
potato = input("potato")
if potato == "tomato":
print("potato tomato")
elif potato == "tornado":
print("potato tornado")
else:
print("we need a certain red vegetable or storm-spout thing")
With elif, if potato equates to tornado, the computer will print "potato tornado". If potato doesn't exactly equate to "tomato" or "tornado", the else clause will act as a catch-all for any other values.
If-then statements are great for user input for specific variables, like passwords. The only problem is if we mess up by entering "tamato" rather than "tomato", we can't re-input any new values for potato. The solution is to put the program into a loop, in this case a while loop. A while loop is essentially an if-then/else/else-if statement with if replaced by while, causing the program to enter a loop until certain conditions are met. If we wanted a program that will loop until "tomato" is entered by adding the while loop, it would look exactly like this:
potato = " "
while potato != "tomato":
potato = input("we need a certain red vegetable")
print("potato tomato")
The program will now loop until potato is tomato. If potato doesn't equal to tomato, the program will ask the user to enter a different value until tomato is specified. A problem with while loops can occur when the program has a slight flaw in it:
potato = " "
while potato != "tomato":
potato = print("we need a certain red vegetable")
print("potato tomato")
Output:
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
Did you see the problem? In this case, I accidentally typed a print function rather than a input function, inadvertently causing an infinite loop, printing "we need a certain red vegetable" over and over forever until we manually end it.
We can change how the condition of the sentry variable by using different comparison operators. These are the equal to (=), not equal to (≠), greater than (>), less than (<), greater than or equal to (⋝), and finally the less than or equal to (⋜) symbols that everyone fussed over in elementary school, except they are now are expressed as ==, !=, >, <, >=, <= in python respectively.
Among the logic operators, I also learned about how to generate random numbers using the random module (modules, by the way, is essentially code represented by a certain word). By using this module, we gain the randint() and randrange() functions. The randint() function produces random numbers that are between two specified values (so randint(5,10) would produce a number between 5 and 10), while the randrange() function will produce integers from a specified group of numbers, similar to randint(), but will generate any number between the specified number and 1 (so randrange(12) would generate any number between 1 and 12).
The odd thing about modules though, unlike functions, is that they need to be imported in order to be used. They aren't present by default, so they have to be loaded in using an import statement. The functions in modules also need to be called by using Dot notation, which is a fancy way of saying that you need to type the module's name and the functions name with a period in between.
*Updated. Included more information on logic operators, random integer generators and modules. Also fixed grammatical errors.
bibliography:
Python Programming for the absolute beginner, Third Edition by Michael Dawson
Friday, April 14, 2017
Back in Business with Math and Variables!
For the first time in 3 weeks, I'm typing in Python again! Currently, I've finished chapter 2 of my programming book, which dealt with types, variables, and simple i/o and now I will be going on to chapter 3, which from the title teaches about "Branching, While Loops, and Program Planning". But so far from what I've learned from the past week, is the bread and butter of programming: Mathematics. Specifically how to program simple mathematical equations, such as the x+y=z variety. From my experience of learning Ti-BASIC and Applesoft, this is the point when programming begins to be useful and applicable, as we essentially recreate that tiny calculator that you get in first grade.
First of all we have the "+", "-", "/","//","%", and "*" operators to play around with. Writing an equation using these new operators in python is similar to writing it down and solving it on paper, but now the computer does all the thinking for you. For example, you can type in 5+5 and the computer will calculate the equation and determine that the answer is 10. The only problem is that the computer doesn't tell you the answer; you have to tell it to tell you the answer after it's solved it. This means that you have to use a print function, like you would in BASIC. So, what you end up typing in Print(5+5) and then the computer will give you the answer.
An interesting thing about Python is that there are now Augmented Assignment Operators, which shorten down simple mathematical equations by attaching the "=" sign to the end of an operator . For example, x = x +5 becomes x+=5. This can help shorten and simplify larger equations that may make use of something else I've learned: variables! A variable by the way is a way to label and access information, and these things make programming a lot easier. By assigning a name for a specific value or a series of strings, you can use less space and run your program more efficiently. So if our program from above used variables, it would look something like this:
answer = 5 + 5
print(answer)
Another thing I learned, is that there are 2 different types of math in python and programming in general: integer and floating point. Integers are whole numbers, which do not have a fractional part (numbers like 1, 27, 61, -100, 0, etc), while floating point numbers are numbers with a decimal point (numbers like 2.375, -99.1, 1.0, etc). Both are used for different purposes. Integer is used when we want a nicely rounded answer (the "//" operator is specifically used for this purpose), while floating point is used for accurate "true" answers. We can also convert existing values into Integers, floating points, or simple strings using the "float()", "Int()", and "Str()" functions respectively.
Here's a program that demonstrates everything I've learned so far:
# Challenge Program "Tipper"
# Demonstrates key programming concepts
# Calculates the necessary amount of a purchase for a 15% and 20% Tip
# Nathan Czaja, 4/14/2017
bill = int(input("what is your Bills total?"))
tip15 = bill*0.15
tip20 = bill*0.20
billtip15 = int(bill+tip15)
billtip20 = int(bill+tip20)
print("\a", "Total entered: $", bill, \
"\n15% Tip: $", tip15, \
"\nBill + 15% Tip: $", billtip15, \
"\n\n20% Tip: $", tip20, \
"\nBill + 20% Tip: $", billtip20)
input("\nPress enter to exit")
These new tools are extremely helpful in STEM (Science Technology Engineering and Mathematics) applications, where equations run rampant and constant calculation becomes tiresome. Using Ohms Law (V = I *R) and the quadratic formula ( (-b+/-√(b^2 (4)ac))/2a) over and over agian can be repetitive and strenuous, so being able to use a machine to do the dirty work for you is a godsend, and is one of the reasons why computers came into being in the first place.
First of all we have the "+", "-", "/","//","%", and "*" operators to play around with. Writing an equation using these new operators in python is similar to writing it down and solving it on paper, but now the computer does all the thinking for you. For example, you can type in 5+5 and the computer will calculate the equation and determine that the answer is 10. The only problem is that the computer doesn't tell you the answer; you have to tell it to tell you the answer after it's solved it. This means that you have to use a print function, like you would in BASIC. So, what you end up typing in Print(5+5) and then the computer will give you the answer.
An interesting thing about Python is that there are now Augmented Assignment Operators, which shorten down simple mathematical equations by attaching the "=" sign to the end of an operator . For example, x = x +5 becomes x+=5. This can help shorten and simplify larger equations that may make use of something else I've learned: variables! A variable by the way is a way to label and access information, and these things make programming a lot easier. By assigning a name for a specific value or a series of strings, you can use less space and run your program more efficiently. So if our program from above used variables, it would look something like this:
answer = 5 + 5
print(answer)
Another thing I learned, is that there are 2 different types of math in python and programming in general: integer and floating point. Integers are whole numbers, which do not have a fractional part (numbers like 1, 27, 61, -100, 0, etc), while floating point numbers are numbers with a decimal point (numbers like 2.375, -99.1, 1.0, etc). Both are used for different purposes. Integer is used when we want a nicely rounded answer (the "//" operator is specifically used for this purpose), while floating point is used for accurate "true" answers. We can also convert existing values into Integers, floating points, or simple strings using the "float()", "Int()", and "Str()" functions respectively.
Here's a program that demonstrates everything I've learned so far:
# Challenge Program "Tipper"
# Demonstrates key programming concepts
# Calculates the necessary amount of a purchase for a 15% and 20% Tip
# Nathan Czaja, 4/14/2017
bill = int(input("what is your Bills total?"))
tip15 = bill*0.15
tip20 = bill*0.20
billtip15 = int(bill+tip15)
billtip20 = int(bill+tip20)
print("\a", "Total entered: $", bill, \
"\n15% Tip: $", tip15, \
"\nBill + 15% Tip: $", billtip15, \
"\n\n20% Tip: $", tip20, \
"\nBill + 20% Tip: $", billtip20)
input("\nPress enter to exit")
What this program does is calculate the amount of money needed for a 15% or a 20% tip. The operation of this program is pretty simple. For example, for variable bill, I can enter $200 dollars. The "int()" function turns the value entered into a usable integer, rather than a string that does nothing. Next, it gets multiplied with 15% and 20% to calculate the relative tips. The tips are then added to the price of the bill, to give a total owed. Finally, the next print() function displays the information calculated to the user.
The running program would look something like this:
what is your Bills total?200
Total entered: $ 200
15% Tip: $ 30.0
Bill + 15% Tip: $ 230
20% Tip: $ 40.0
Bill + 20% Tip: $ 240
Press enter to exit
>>>
These new tools are extremely helpful in STEM (Science Technology Engineering and Mathematics) applications, where equations run rampant and constant calculation becomes tiresome. Using Ohms Law (V = I *R) and the quadratic formula ( (-b+/-√(b^2 (4)ac))/2a) over and over agian can be repetitive and strenuous, so being able to use a machine to do the dirty work for you is a godsend, and is one of the reasons why computers came into being in the first place.
Friday, April 7, 2017
An Excuse for 10K
Hello. Normally this would have been about what I've learned about python, but this isn't the case. Since my last post from 2 weeks ago, I have been too busy to really learn anything about python. And this week is a special case. Like my first ever blog post, my 20-time project has been interrupted by a robotics competition, only this time it's on home turf.
The Minnesota 10,000 Lakes Regional, or as it is informally called by everyone as "10k", started on Thursday and at the time of this blog post, is currently ongoing. However for me, it's been going on since Monday night. For 2 days straight, I worked with my robotics team to get our robot up, running, and packed up for 10k. I also helped out on Wednesday night, which sucked up most of my time in the first half of the week. But then came Thursday, the opening day. In order to make up most of our teams time, we rushed out at 7:00 in the morning to get to our pits at Minnesota State University, where 10k was hosted. We began to work on our robot immediately, putting together and testing it in practice matches for nearly 14 hours straight. And then today, Friday, we began to compete in the qualification matches. Now is the time our hard work meant something. I mostly helped with general fabrication, but today I temporarily became part of our drive team, being the guy setting out gears for our robotic to pick up. It was pretty fun! Much better than scouting out matches, which involved logging in every movement of every robot on the field.
Tonight, team 3184 Blaze Robotics has so far managed to get to 15th place, with the possibility of going to quarterfinals tomorrow. I am exhausted from working so much, so I apologize for not having a on topic blog post. I also apologize for the bad grammar, but it's 11:30 and I need to get this done. Next Friday though, I promise that we'll be finally on topic.
*4/21/17 fixed grammatical errors
The Minnesota 10,000 Lakes Regional, or as it is informally called by everyone as "10k", started on Thursday and at the time of this blog post, is currently ongoing. However for me, it's been going on since Monday night. For 2 days straight, I worked with my robotics team to get our robot up, running, and packed up for 10k. I also helped out on Wednesday night, which sucked up most of my time in the first half of the week. But then came Thursday, the opening day. In order to make up most of our teams time, we rushed out at 7:00 in the morning to get to our pits at Minnesota State University, where 10k was hosted. We began to work on our robot immediately, putting together and testing it in practice matches for nearly 14 hours straight. And then today, Friday, we began to compete in the qualification matches. Now is the time our hard work meant something. I mostly helped with general fabrication, but today I temporarily became part of our drive team, being the guy setting out gears for our robotic to pick up. It was pretty fun! Much better than scouting out matches, which involved logging in every movement of every robot on the field.
Tonight, team 3184 Blaze Robotics has so far managed to get to 15th place, with the possibility of going to quarterfinals tomorrow. I am exhausted from working so much, so I apologize for not having a on topic blog post. I also apologize for the bad grammar, but it's 11:30 and I need to get this done. Next Friday though, I promise that we'll be finally on topic.
*4/21/17 fixed grammatical errors
Subscribe to:
Posts (Atom)