Blogs Blogs

Python Blogs

Newest Most Active

Hi Guys,  I'm new to Wyzant, so I thought I'd give a quick python demo..Diving right in..    Python is an object oriented (OO) language however doesn't enforce this paradigm... Pop open a terminal/shell and type the word python..     Users/Mike$ python  <enter>  C:/  Python <enter>   >>> # If python is installed you will see three angle brackets like so... You're can now simply start writing python code..  >>> # This is a comment in python  -->prefixed by hashtag or the pound symbol   >>>  >>> x = 5    >>>x  5    >>> x + 2  7    >>> x * 2  10    >>> x ** 2   # x squared   25    >>> x / 2    # python 2.x  --> 3 .  python 3.x  --... read more

Did you Know: We can use generators/coroutines in place of multi-threading? Most of us don't think twice about the simple for loop or how it works. Why should we? Is there anything behind it? How does it work? Let's take a look behind the scenes. Python's for loop is really a for each loop (for each element in collection). msg = "The quick brown fox jumps over the lazy dog" If we want to traverse the string above to produce each letter one by one, we simply treat this string like any other sequence in python. for letter in msg:     print letter 'T h e q u i c k b r o w n .... lazy dog... Outcomes our string one letter at a time to the standard output. So what is the for loop actually doing for us? Just as you would assume, as if we were to consume an iterator manually. iterator = iter(msg) T h e ...iterator... read more

''' Did you know?   A few hack, cracks and obfuscated examples, which may actually lead to less confusion and a greater knowledge of why things work and how. However, these examples have a purpose, and should not be taken, in any way as correct or just another way to achieve a common goal. They are purposely, in some cases, convoluted.   I. String, things, hacking, and unpacking '''   import string s = ‘The quick brown fox jumps over the lazy dog”   The string above (s) is called a pangram. This means it contains every letter of the alphabet at least once. Let’s  Prove that’s true.   Goal: To extract the alphabet in lexicographical form from the pangram below.   alphabet = string.ascii_lowercase # → ‘abcdefghijklmnopqrstuvwxyz’   #Algorithm 1. We begin by calling method lower() on our string to create all lowercase letters (A small amount of normalization).   s... read more

It’s 5pm on Sunday evening and you decide it’s time to break out your 1st Java assignment, which is due later that evening at 12am. No big deal, you have plenty of time! What can’t you do in seven hours? I mean that’s like at least 40 games of Halo. You stall another hour (playing Halo) until six 0clock at which point you decide you better get started just in case. You glanced at the problem earlier in the week, no biggie. A couple of inputs, some basic processing, some formatted output, and maybe the professor threw in some easy twist. Two maybe three hours tops, you’ll be counting sheep by ten.   The clock strike’s ten; you have 25 IM windows open (3 hopefuls). You’ve Googled the same thing 25 times, you have more red squiggly lines than if you had written a letter in Spanish inside MS. Word, your code doesn’t compile,  and it looks like this…   public class Chaos {       //default constructor public Chaos()    ... read more

There is very little emphasis these days on teaching programming, in spite of the fact that technology is becoming more and more a dominant aspect of our lives. Perhaps this is because many programmers are self-taught, used to working alone on projects, and therefore the assumption is that students will learn programming "as they go" or "on their own". This is unfortunate because I think that this aversion to traditional instruction and the preference for "self-taught" programmers leaves some people who want to learn in the dust.   I have lately become interested in rectifying this problem. A few of my clients have discussed the option of learning programming through tutoring sessions with me. I think that if I had been able to avail myself of such an option when I was first learning to program, I might have had a much easier time in learning how to properly use computers as the powerful tools that they are. I believe, however, that... read more

1. Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print. #! /usr/bin/env python3 # ex1 - words # All work and no play makes Jack a dull boy. word1 = "All" word2 = "work" word3 = "and" word4 = "no" word5 = "play" word6 = "makes" word7 = "Jack" word8 = "a" word9 = "dull" word10 = "boy." print(word1, word2, word3, word4, word5, word6, word7, word8, word9, word10) 2. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6. #! /usr/bin/env python3 print("6 * 1 - 2 = ", 6 * 1 - 2) print("6 * (1 - 2) = ", 6 * (1 - 2)) 3. Place a comment before a line of code that previously worked, and record what happens when... read more

Chapter 2 of How to Think Like a Computer Scientist begins actually exploring Python and its features.   The chapter discusses variables, expressions, and statements. It starts out discussing data types and using the type() function to determine the type of some literals, like strings, integers, and floats. It also discusses using different kind of string delimiters.   Variables have a name and can be created by using the name you want to give the variable, the assignment token (=), and a value, like so:   lunch = "sandwich" weight = 150   Variable names have to begin with a letter or underscore (_) and can't be one of Python's reserved keywords (   Statements are instructions that python can perform. Examples given are while, for, import and others.   Expressions are combinations... read more

In preparation for offering Python as a subject I'm planning on reviewing and refreshing my basic Python programming skills using the online text "How to Think Like a Computer Scientist", located at:   As I progress through the chapters I plan on writing a brief summary and reflection on the topics covered. I also plan on posting my answers to selected problems from the text.   Python is an exciting, powerful, but easily learned programming language, with a large selection of libraries for achieving many common tasks from file i/o, networking, and various mathematical functions, to cross platform gui development, application scripting and automation, and other useful and advanced tasks.   Obligatory XKCD:    

Python Blogs RSS feed