Saturday, July 11, 2015

"for" loops

"for" loops in Python iterate over a given range. You can iterate through a string, list, dictionary, or over a range of values. Here is an example of each.

Iterating through values:
for i in range(10):
    print i
What would you expect the output to be? The output would be the numbers 0 through 9, each on a separate line. Why 0 through 9 and not 1 through 10? Well most programming languages start counting at 0. If you had an array that held 4 values called nums then nums[0] would be the first value and nums[3] would hold the last value. This is an important concept in programming and can cause bugs and other issues if not addressed properly in your code. With a little practice you will be able to grasp this way of thinking fairly quickly. If you did want to iterate from 1 to 10 your for statement would be "for i in range(1,11):"

Iterating through a string:
for letter in "Python is cool":
    print letter
This is a great way to break down strings in to individual characters. It will start with the first and end with the last. Open your compiler and experiment with using for loops this way. Once you get the hang of it you will find it really is handy for dealing with strings.

Iterating through a list:

cards = ["ACE", "QUEEN", "KING", "JACK"]

for card in cards:
    print "The card is: " + card


As you can see the for loop is a powerful way to execute a block of code several times. Next we will learn how "while" and "do" loops work in Python. Stay tuned!

Friday, July 10, 2015

Here are some online resources for learning Python. Some are interactive courses while others are just text tutorials.

1. Codecademy: Codecademy is an excellent interactive course for learning Python. Several other languages and architectures are available as well.
2. Tutorials Point: This is an excellent tutorial for Python.
3. Learn Python In 10 Minutes Wonderful, short, and straight to the point tutorial. Assumes you have a basic understanding of programming.

Tomorrow I will write a post about variables and loops. Time to get your hands dirty!

Tuesday, July 7, 2015

Downloading and installing Python

Okay, so you think you are ready to download Python? Downloading and installing Python is very simple. All Mac OSX operating systems since 10.4 come with Python installed. The versions since 10.5 are fairly complete, including both IDLE and NumPy. Most Linux systems also come with a Python installation. If you are on Windows you should download Python from Python.org. But first, which version to choose? That is a tough question. Personally I prefer version 2.7. Python 3.X versions have many differences and are not backwards compatible with 2.X. If you wish to learn from my blog please download version 2.7.10. Everyday I will update this blog with tons of information, and leave you with a "homework" assignment. I will do everything in my power to help teach you the in's and out's of Python programming...all for free! If you feel I have helped you please leave a donation. I am doing all of this in my spare time whilst working a full time job as a carpenter and raising a 2 year old...so any donation would be extremely appreciated!

Python installs to a directory with the version number inclued, ex. Python version 2.7 will install at C:\Python27\. If you need further instructions check out this page on howtogeek. It is a very thorough and has pictures to guide you in your journey. Python comes installed with an editor called Idle. IDLE is located here: C:\Python27\Lib\idlelib\idle.bat ...if you have version 3.1 then it will be located in C:\Python31\Lib\idlelib\idle.bat.

Your first Python program:
In the IDLE editor shell --> Click File, then New File.
Type in the follow: print "Hello World!!!"


Then press the F5 key to run. It will ask you to save the file. Name it helloworld.py and save it in the Python folder. Then you will see the results of your program. Congratulations! You have just made your first Python "program". This program, as you could have guess, just displays the words "Hello World!!!".

Now that you are up and running we are prepped to learn some fundamental concepts. Next we will delve in to lists, strings, and loops! Stay tuned!

Welcome!

Welcome! The purpose of this blog is to explore many different aspects of the Python programming language, as well as other programming concepts as well. Every post will give a detailed account of a useful Python concept, including examples. I am currently preparing to start the examples tomorrow, so please stay tuned! Feel free to ask questions and give feedback. Thank you!


If you feel you have learned something please consider donating by using the widgets on the bottom of the page. Thank you!