(snake sounds)

For Devember I would like to gain a passing knowledge of Python. I am a Sys. admin student who has very little experience programming. (semester of programming in ADA five years ago) I hope to refresh the basics and build a game or maybe a Mad Libs generator. Suggestions on a good beginner project and resources for learning python would be more than welcome.

-Zen

6 Likes

Alright Ive decided to use Atom for no particular reason what-so-ever. If I should be using something else let me know. This is all knew to me. I set up a github account and did the little tutorial. Next I set out for a warm plate of tendies and sat down to begin reading.

-Zen

4 Likes

Today I made it through variable types on tutorials point and was able to complete the first three exercises on hackinscience. I may need to roll back python on because the tutorial is using 2.4.3 and I have py3 on my machine. So far it hasn’t been a problem.

2 Likes

You should be fine with Python 3. Minus a few differences python 3 is a super set of Python 3. Also, you can write the code in Python 2 and then run python2to3 and it will make any necessary changes. There is also a python3to2 program as well.

1 Like

Another day, another dollar. Covered basic operators and a little flow control today. I was able to complete questions 4 and 5 on hackinscience. It felt weird not having to define an exit statement. I will link to my git hub once I have something to show.

-Zen

2 Likes

Today I covered if statements, and loops. I looked more closely at numbers in python and some of the math module. I was able to complete 6, 7, and 8 on hackinscience. Mid terms are heating up. Time to study.

-Zen

2 Likes

Today, I looked more closely at strings and the built-in string functions. I played around with more of the math built-ins and I was able to complete problems 9 and 10 on hackinscience. While looking at other students solutions to problem 10, I started to wonder what makes some code better and other code worse. Is the shortest code better? Is it speed, or readability? Is it personal opinion? Problem 10 asked us to print the sum of even numbers between a range of 0 to 100. Below are three solutions and I am wondering which is better or does it not really matter all that much?

c = 0
for i in range (1,51):
c+=(i*2)
print c

total = 0
for i in range(0,101):
if i%2 == 0:
total = total + i
print(total)

print(sum(range(0, 101, 2)))

-Zen

1 Like

This is a really great question to be thinking of this early in learning to program. I’m a fan of the metrics used here:

  • Simplicity
  • Readability
  • Modularity
  • Layering
  • Design
  • Efficiency
  • Elegance
  • Clarity

Note that doing the earlier items in the list will impact the later items to great benefit. Most ‘good code v. bad code’ discussions focus on Efficiency or Elegance, which are difficult to even approach without the previous items being fulfilled.

4 Likes

this one

and damn that was sexy

I love one-liners

1 Like

For that example I’d do the one liner also, but mostly just to preserve brain-space for any other stuff which might be going on in the code.

I would say that solution 1 is more efficient than solution 2.

Solution 2 is more accurate, and if you tried to fuzz the input data, it would produce the most consistent results. Also it does not seem to rely on language short hand and language built in functions, so it may be more portable than Solutions 1 and 3.

Solution 3 is easiest to read and if the built in functions are actually optimized, then it would be more efficient than Solutions 1 and 2. With that said, the implementations of the built in functions may be more secure and hold up better against fuzzing.

**Edit
And also congrats on thinking about these concepts as you go. Those things tend to be what separate Coders from Software Engineers and Computer Scientists.

3 Likes

Thank you to everyone who responded. It has been a couple days since I’ve been able to work on this project because my mother-in-law invaded my office for the weekend. Over the weekend I looked at basic data structures and today I dug a little deeper into lists in python. I was able to answer number 11 on hackinscience, although not in a single line :wink:. I have been thinking in my head about the algorithm I am going to need to write, and I hope to have something down on paper by the end of the week.

2 Likes

Today, I spent some time looking at tuples in more detail and finished answer 12 on hackinscience. I made my first attempt at writing out an algorithm for my mad lib gen. It can be found here if you interested. I always found break a large problem into a smaller manageable chunks to be the hardest part of writing programs. Hopefully that is something I get better at the more I work at it.

-Zen

2 Likes

Can someone explain to me why this returns ‘want’ and not ‘shrubbery’? I’ve been messing with it for a while and can’t seem to find an answer.

import string   #string module

def longest_word (text):
    """returns the longest word from a string"""
    return max(text.split ())

if __name__ == "__main__":
    print(longest_word("We want a shrubbery"))
1 Like

If I remember correctly, the max () function returns the highest ASCII value for strings. So it basically sorts alphabetically not by length of string.


Can try adding this to your longest_word function to see the values.

for i in range (len (text)):
	print (text[i] + " = " + str (ord (text[i])))
2 Likes

Thank you!! That helped a bunch. :cowboy_hat_face:

2 Likes

Today I completed problems 13 and 14 on hackinscience and spent sometime reading about markdown. Thanks to some help I ended up learning a great deal about comparer functions, and that strings are actually objects with multiple properties. By default the comparer function I was using max(), for strings, sorts by appearance in the ASCII table. If I want the function max() to sort by length instead I need to pass it key=len.

2 Likes

Today, I read about dictionaries in python and completed problem 15 on hackinscience. Who knew dates and times could be so confusing. I also became aware the print() automagically inserts spaces, which is nice to know.

-Zen

2 Likes

Today I made some updates on my algo and did some work with nested loops.

-Zen

1 Like

You are really cruising along. By 2021, you will definitely has some real understanding and technical know how to break into the Comp. Sci. and Software Engineering fields. Bravo Zulu.

1 Like